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

QueueableTrait::findQueueable()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 13
ccs 6
cts 7
cp 0.8571
crap 3.0261
rs 10
c 1
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