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

LazyAdvisorAccessor   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 62.5%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 2
dl 0
loc 47
ccs 10
cts 16
cp 0.625
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\Core;
14
15
use Go\Aop\Advice;
16
use Go\Aop\Advisor;
17
use InvalidArgumentException;
18
19
/**
20
 * Provides an interface for loading of advisors from the container
21
 */
22
class LazyAdvisorAccessor
23
{
24
    /**
25
     * Instance of aspect container
26
     */
27
    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...
28
29
    /**
30
     * Aspect loader instance
31
     */
32
    protected AspectLoader $loader;
33
34
    /**
35
     * Accessor constructor
36 1
     */
37
    public function __construct(AspectContainer $container, AspectLoader $loader)
38 1
    {
39 1
        $this->container = $container;
40 1
        $this->loader    = $loader;
41
    }
42
43
    /**
44
     * Magic advice accessor
45
     *
46
     * @throws InvalidArgumentException if referenced value is not an advisor
47 1
     */
48
    public function __get(string $name): Advice
49 1
    {
50 1
        if ($this->container->has($name)) {
51
            $advisor = $this->container->get($name);
52
        } else {
53
            list(, $advisorName) = explode('.', $name);
54
            list($aspect)        = explode('->', $advisorName);
55
            $aspectInstance      = $this->container->getAspect($aspect);
56
            $this->loader->loadAndRegister($aspectInstance);
57
58
            $advisor = $this->container->get($name);
59
        }
60 1
61
        if (!$advisor instanceof Advisor) {
62
            throw new InvalidArgumentException("Reference {$name} is not an advisor");
63 1
        }
64
        $this->$name = $advisor->getAdvice();
65 1
66
        return $this->$name;
67
    }
68
}
69