Listener::__construct()   B
last analyzed

Complexity

Conditions 8
Paths 9

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
eloc 13
c 0
b 0
f 0
dl 0
loc 21
ccs 0
cts 14
cp 0
rs 8.4444
cc 8
nc 9
nop 3
crap 72
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Biurad opensource projects.
7
 *
8
 * PHP version 7.2 and above required
9
 *
10
 * @author    Divine Niiquaye Ibok <[email protected]>
11
 * @copyright 2019 Biurad Group (https://biurad.com/)
12
 * @license   https://opensource.org/licenses/BSD-3-Clause License
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 */
17
18
namespace Biurad\Events\Annotation;
19
20
/**
21
 *  Annotation class for @Listener().
22
 *
23
 * @Annotation
24
 * @Target({"CLASS", "METHOD"})
25
 */
26
#[\Attribute(\Attribute::IS_REPEATABLE | \Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD)]
27
final class Listener
28
{
29
    /** @var string */
30
    private $event;
31
32
    /** @var int */
33
    private $priority;
34
35
    /**
36
     * @param @param array<string,mixed>|string $data
37
     * @param string $event
38
     * @param int $priority
39
     */
40
    public function __construct($data = null, string $event = null, int $priority = 0)
41
    {
42
        if (is_array($data) && isset($data['value'])) {
43
            $data['event'] = $data['value'];
44
            unset($data['value']);
45
        } elseif (\is_string($data)) {
46
            $data = ['event' => $data];
47
        }
48
49
        $this->event = $data['event'] ?? $event;
50
        $this->priority = $data['priority'] ?? $priority;
51
52
        if (empty($this->event) || !\is_string($this->event)) {
53
            throw new \InvalidArgumentException(\sprintf(
54
                '@Listener.event must %s.',
55
                empty($this->event) ? 'be not an empty string' : 'contain only a string'
56
            ));
57
        }
58
59
        if (!is_integer($this->priority)) {
60
            throw new \InvalidArgumentException('@Listener.priority must contain only an integer');
61
        }
62
    }
63
64
    /**
65
     * Get the event's priority
66
     *
67
     * @return int
68
     */
69
    public function getPriority(): int
70
    {
71
        return $this->priority;
72
    }
73
74
    /**
75
     * Get the event listener
76
     *
77
     * @return string
78
     */
79
    public function getEvent(): string
80
    {
81
        return $this->event;
82
    }
83
}
84