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

QueueableDetector   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 40
ccs 15
cts 15
cp 1
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getQueue() 0 14 4
A __construct() 0 3 1
A isQueueable() 0 9 2
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