Passed
Pull Request — master (#662)
by Maxim
08:13
created

QueueableTrait::createQueueable()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 2
nop 2
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Queue\Attribute;
6
7
use Spiral\Attributes\Factory;
8
use Spiral\Queue\QueueableInterface;
9
10
trait QueueableTrait
11
{
12
    /**
13
     * @psalm-param class-string|object $class
14
     */
15 6
    public function findQueueable($object): ?Queueable
16
    {
17 6
        $reflection = new \ReflectionClass($object);
18
19 6
        if ($reflection->isInterface()) {
20
            return null;
21
        }
22
23 6
        if ($reflection->implementsInterface(QueueableInterface::class)) {
24 3
            return $this->createQueueable($reflection, $object);
25
        }
26
27 3
        return (new Factory())->create()->firstClassMetadata($reflection, Queueable::class);
28
    }
29
30
    /**
31
     * @psalm-param class-string|object $object
32
     */
33 3
    private function createQueueable(\ReflectionClass $reflection, $object): Queueable
34
    {
35 3
        $queueable = new Queueable();
36
37 3
        if (\is_object($object) && $reflection->hasMethod('getQueue')) {
38 2
            $queueable->queue = $reflection->getMethod('getQueue')->invoke($object);
39
        }
40
41 3
        return $queueable;
42
    }
43
}
44