Completed
Pull Request — master (#463)
by Alexander
30:17 queued 05:15
created

PointcutBuilder   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 81
ccs 0
cts 41
cp 0
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types = 1);
4
/*
5
 * Go! AOP framework
6
 *
7
 * @copyright Copyright 2014, Lisachenko Alexander <[email protected]>
8
 *
9
 * This source file is subject to the license that is bundled
10
 * with this source code in the file LICENSE.
11
 */
12
13
namespace Go\Aop\Support;
14
15
use Closure;
16
use Go\Aop\Advice;
17
use Go\Aop\Framework\AfterInterceptor;
18
use Go\Aop\Framework\AfterThrowingInterceptor;
19
use Go\Aop\Framework\AroundInterceptor;
20
use Go\Aop\Framework\BeforeInterceptor;
21
use Go\Aop\Framework\DeclareErrorInterceptor;
22
use Go\Core\AspectContainer;
23
24
/**
25
 * Pointcut builder provides simple DSL for declaring pointcuts in plain PHP code
26
 */
27
class PointcutBuilder
28
{
29
    /**
30
     * Instance of aspect container
31
     */
32
    protected AspectContainer $container;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
33
34
    /**
35
     * Default constructor for the builder
36
     */
37
    public function __construct(AspectContainer $container)
38
    {
39
        $this->container = $container;
40
    }
41
42
    /**
43
     * Declares the "Before" hook for specific pointcut expression
44
     */
45
    public function before(string $pointcutExpression, Closure $adviceToInvoke): void
46
    {
47
        $interceptor = new BeforeInterceptor($adviceToInvoke, 0, $pointcutExpression);
48
        $this->registerAdviceInContainer($pointcutExpression, $interceptor);
49
    }
50
51
    /**
52
     * Declares the "After" hook for specific pointcut expression
53
     */
54
    public function after(string $pointcutExpression, Closure $adviceToInvoke): void
55
    {
56
        $interceptor = new AfterInterceptor($adviceToInvoke, 0, $pointcutExpression);
57
        $this->registerAdviceInContainer($pointcutExpression, $interceptor);
58
    }
59
60
    /**
61
     * Declares the "AfterThrowing" hook for specific pointcut expression
62
     */
63
    public function afterThrowing(string $pointcutExpression, Closure $adviceToInvoke): void
64
    {
65
        $interceptor = new AfterThrowingInterceptor($adviceToInvoke, 0, $pointcutExpression);
66
        $this->registerAdviceInContainer($pointcutExpression, $interceptor);
67
    }
68
69
    /**
70
     * Declares the "Around" hook for specific pointcut expression
71
     */
72
    public function around(string $pointcutExpression, Closure $adviceToInvoke): void
73
    {
74
        $interceptor = new AroundInterceptor($adviceToInvoke, 0, $pointcutExpression);
75
        $this->registerAdviceInContainer($pointcutExpression, $interceptor);
76
    }
77
78
    /**
79
     * Declares the error message for specific pointcut expression with concrete error level
80
     */
81
    public function declareError(string $pointcutExpression, string $message, int $errorLevel = E_USER_ERROR): void
82
    {
83
        $interceptor = new DeclareErrorInterceptor($message, $errorLevel, $pointcutExpression);
84
        $this->registerAdviceInContainer($pointcutExpression, $interceptor);
85
    }
86
87
    /**
88
     * General method to register advices
89
     */
90
    private function registerAdviceInContainer(string $pointcutExpression, Advice $adviceToInvoke): void
91
    {
92
        $this->container->registerAdvisor(
93
            new LazyPointcutAdvisor($this->container, $pointcutExpression, $adviceToInvoke),
94
            $this->getPointcutId($pointcutExpression)
95
        );
96
    }
97
98
    /**
99
     * Returns an unique name for given pointcut expression
100
     */
101
    private function getPointcutId(string $pointcutExpression): string
102
    {
103
        static $index = 0;
104
105
        return preg_replace('/\W+/', '_', $pointcutExpression) . '.' . $index++;
106
    }
107
}
108