Completed
Pull Request — 1.x (#286)
by Alexander
02:43
created

LazyAdvisorAccessor   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 0%
Metric Value
wmc 4
lcom 0
cbo 2
dl 0
loc 55
ccs 0
cts 22
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __get() 0 20 3
1
<?php
2
/*
3
 * Go! AOP framework
4
 *
5
 * @copyright Copyright 2014, Lisachenko Alexander <[email protected]>
6
 *
7
 * This source file is subject to the license that is bundled
8
 * with this source code in the file LICENSE.
9
 */
10
11
namespace Go\Core;
12
13
use Go\Aop\Advice;
14
use Go\Aop\Advisor;
15
16
/**
17
 * Provides an interface for loading of advisors from the container
18
 */
19
class LazyAdvisorAccessor
20
{
21
    /**
22
     * @var AspectContainer|Container
23
     */
24
    protected $container;
25
26
    /**
27
     * Aspect loader instance
28
     *
29
     * @var AspectLoader
30
     */
31
    protected $loader;
32
33
    /**
34
     * Accessor constructor
35
     *
36
     * @param AspectContainer $container
37
     * @param AspectLoader $loader
38
     */
39
    public function __construct(AspectContainer $container, AspectLoader $loader)
40
    {
41
        $this->container = $container;
42
        $this->loader    = $loader;
43
    }
44
45
    /**
46
     * Magic accessor
47
     *
48
     * @param string $name Key name
49
     *
50
     * @throws \InvalidArgumentException if referenced value is not an advisor
51
     * @return Advice
52
     */
53
    public function __get($name)
54
    {
55
        if ($this->container->has($name)) {
56
            $advisor = $this->container->get($name);
57
        } else {
58
            list(, $advisorName) = explode('.', $name);
59
            list($aspect)        = explode('->', $advisorName);
60
            $aspectInstance      = $this->container->getAspect($aspect);
61
            $this->loader->loadAndRegister($aspectInstance);
62
63
            $advisor = $this->container->get($name);
64
        }
65
66
        if (!$advisor instanceof Advisor) {
67
            throw new \InvalidArgumentException("Reference {$name} is not an advisor");
68
        }
69
        $this->$name = $advisor->getAdvice();
70
71
        return $this->$name;
72
    }
73
}
74