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

QueueableTrait   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 91.67%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
dl 0
loc 32
ccs 11
cts 12
cp 0.9167
rs 10
c 1
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A createQueueable() 0 9 3
A findQueueable() 0 13 3
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