Passed
Pull Request — master (#662)
by Maxim
09:11 queued 01:07
created

QueueableDetector::getQueue()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

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