Skip to content

Commit

Permalink
Merge pull request #202 from tenancy/fix-queue-identification
Browse files Browse the repository at this point in the history
Fixes serialization issues in Queue Identification
  • Loading branch information
ArlonAntonius committed Sep 30, 2020
2 parents 35260b6 + 0fa7d49 commit c52406d
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 27 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG-1.x.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Release Notes for 1.x

## [Unreleased](https://github.com/tenancy/tenancy/compare/v1.1.1...1.x)
## [Unreleased](https://github.com/tenancy/tenancy/compare/v1.1.2...1.x)

## [1.1.2 (2020-09-20)](https://github.com/tenancy/tenancy/compare/v1.1.1...v1.1.2)
- FIXED: Queue identification serialization ([#202](https://github.com/tenancy/tenancy/pull/202))

## [1.1.1 (2020-09-20)](https://github.com/tenancy/tenancy/compare/v1.1.0...v1.1.1)
- FIXED: Laravel 8 support for tenancy/framework ([#200](https://github.com/tenancy/tenancy/pull/200))
Expand Down
32 changes: 6 additions & 26 deletions src/Identification/Queue/Events/Processing.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Illuminate\Queue\SerializesAndRestoresModelIdentifiers;
use Illuminate\Support\Arr;
use Tenancy\Identification\Contracts\Tenant;
use Tenancy\Identification\Drivers\Queue\Jobs\Job as TenancyJob;

class Processing
{
Expand Down Expand Up @@ -63,13 +64,9 @@ public function __construct(JobProcessing $event)
$job = $this->unserializeToJob($command);
}

$tenant = $this->getJobProperty($job, 'tenant');
$tenant_key = $this->getJobProperty($job, 'tenant_key');
$tenant_identifier = $this->getJobProperty($job, 'tenant_identifier');

if ($tenant) {
$tenant = $this->restoreModel($tenant);
}
$tenant = $job->getTenant();
$tenant_key = $job->getTenantKey();
$tenant_identifier = $job->getTenantIdentifier();

$this->tenant = $tenant ?? null;
$this->tenant_key = $tenant_key ?? $payload['tenant_key'] ?? null;
Expand All @@ -87,25 +84,8 @@ public function __construct(JobProcessing $event)
*/
private function unserializeToJob(string $object)
{
$stdClassObj = preg_replace('/^O:\d+:"[^"]++"/', 'O:'.strlen('stdClass').':"stdClass"', $object);

return unserialize($stdClassObj);
}

/**
* Returns a property from an unserialized job if it exists on the object.
*
* @param object $job
* @param string $key
*
* @return mixed
*/
private function getJobProperty(object $job, string $key)
{
if (property_exists($job, $key)) {
return $job->{$key};
}
$stdClassObj = preg_replace('/^O:\d+:"[^"]++"/', 'O:'.strlen(TenancyJob::class).':"'.TenancyJob::class.'"', $object);

return null;
return unserialize($stdClassObj, ['allowed_classes' => [TenancyJob::class]]);
}
}
79 changes: 79 additions & 0 deletions src/Identification/Queue/Jobs/Job.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

declare(strict_types=1);

/*
* This file is part of the tenancy/tenancy package.
*
* Copyright Tenancy for Laravel
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @see https://tenancy.dev
* @see https://github.com/tenancy
*/

namespace Tenancy\Identification\Drivers\Queue\Jobs;

use Illuminate\Contracts\Database\ModelIdentifier;
use Illuminate\Queue\SerializesAndRestoresModelIdentifiers;
use ReflectionClass;

class Job
{
use SerializesAndRestoresModelIdentifiers;

protected $tenant;

protected $tenant_identifier;

protected $tenant_key;

public function getTenant()
{
return $this->restoreValue($this->tenant);
}

public function getTenantIdentifier()
{
return $this->tenant_identifier;
}

public function getTenantKey()
{
return $this->tenant_key;
}

public function __unserialize(array $values)
{
$properties = (new ReflectionClass($this))->getProperties();

foreach ($properties as $property) {
if (!in_array($property->getName(), ['tenant', 'tenant_identifier', 'tenant_key'])) {
continue;
}

$name = $property->getName();

if (!array_key_exists($name, $values)) {
continue;
}

$property->setAccessible(true);

$property->setValue($this, $this->restoreValue($values[$name]));
}
}

protected function restoreValue($value)
{
$value = unserialize(serialize($value));

if ($value instanceof ModelIdentifier) {
return $this->restoreModel($value);
}

return $value;
}
}

0 comments on commit c52406d

Please sign in to comment.