Completed
Push — 2.x ( 1d2844...e1ee47 )
by Akihito
02:02 queued 22s
created

Bind::annotatedMethodMatchBind()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
ccs 6
cts 6
cp 1
cc 3
nc 3
nop 3
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\Aop;
6
7
final class Bind implements BindInterface
8
{
9
    /**
10
     * @var array
11
     */
12
    private $bindings = [];
13
14
    /**
15
     * @var MethodMatch
16
     */
17
    private $methodMatch;
18
19
    /**
20
     * @throws \Doctrine\Common\Annotations\AnnotationException
21
     */
22
    public function __construct()
23
    {
24
        $this->methodMatch = new MethodMatch($this);
25 40
    }
26
27 40
    public function __sleep()
28 40
    {
29
        return ['bindings'];
30
    }
31
32
    /**
33 31
     * {@inheritdoc}
34
     *
35 31
     * @throws \ReflectionException
36 31
     */
37
    public function bind(string $class, array $pointcuts) : BindInterface
38 31
    {
39
        $pointcuts = $this->getAnnotationPointcuts($pointcuts);
40
        assert(class_exists($class));
41
        $class = new \ReflectionClass($class);
42
        $methods = $class->getMethods(ReflectionMethod::IS_PUBLIC);
43
        foreach ($methods as $method) {
44 37
            ($this->methodMatch)($class, $method, $pointcuts);
45
        }
46 37
47 3
        return $this;
48 3
    }
49
50
    /**
51 37
     * {@inheritdoc}
52
     */
53
    public function bindInterceptors(string $method, array $interceptors) : BindInterface
54
    {
55
        $this->bindings[$method] = ! array_key_exists($method, $this->bindings) ? $interceptors : array_merge(
56
            $this->bindings[$method],
57 34
            $interceptors
58
        );
59 34
60
        return $this;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65 18
     */
66
    public function getBindings() : array
67 18
    {
68
        return $this->bindings;
69 18
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function toString($salt) : string
75
    {
76
        unset($salt);
77 31
78
        return strtr(rtrim(base64_encode(pack('H*', sprintf('%u', crc32(serialize($this->bindings))))), '='), '+/', '-_');
79 31
    }
80 31
81 31
    /**
82 2
     * @param Pointcut[] $pointcuts
83
     *
84 31
     * @return Pointcut[]
85
     */
86
    public function getAnnotationPointcuts(array &$pointcuts) : array
87 31
    {
88
        $keyPointcuts = [];
89
        foreach ($pointcuts as $key => $pointcut) {
90 31
            if ($pointcut->methodMatcher instanceof AnnotatedMatcher) {
91
                $key = $pointcut->methodMatcher->annotation;
92 31
            }
93 31
            $keyPointcuts[$key] = $pointcut;
94 31
        }
95
96 31
        return $keyPointcuts;
97
    }
98
}
99