Completed
Pull Request — master (#59)
by Tim
11:55
created

ObserverVisitor::prepareObservers()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 20
Code Lines 10

Duplication

Lines 20
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 20
loc 20
c 0
b 0
f 0
ccs 0
cts 9
cp 0
rs 9.2
cc 4
eloc 10
nc 5
nop 3
crap 20
1
<?php
2
3
/**
4
 * TechDivision\Import\Observers\ObserverVisitor
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2016 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/techdivision/import
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Observers;
22
23
use TechDivision\Import\Subjects\SubjectInterface;
24
use Symfony\Component\DependencyInjection\TaggedContainerInterface;
25
26
/**
27
 * Visitor implementation for a subject's observers.
28
 *
29
 * @author    Tim Wagner <[email protected]>
30
 * @copyright 2016 TechDivision GmbH <[email protected]>
31
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
32
 * @link      https://github.com/techdivision/import
33
 * @link      http://www.techdivision.com
34
 */
35 View Code Duplication
class ObserverVisitor
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
36
{
37
38
    /**
39
     * The DI container builder instance.
40
     *
41
     * @var \Symfony\Component\DependencyInjection\TaggedContainerInterface
42
     */
43
    protected $container;
44
45
    /**
0 ignored issues
show
Coding Style introduced by
Doc comment for parameter "$container" missing
Loading history...
46
     * The constructor to initialize the instance.
47
     *
48
     * @param \Symfony\Component\DependencyInjection\TaggedContainerInterface The container instance
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter name
Loading history...
49
     */
50
    public function __construct(TaggedContainerInterface $container)
51
    {
52
        $this->container = $container;
53
    }
54
55
    /**
56
     * Visitor implementation that initializes the observers of the passed subject.
57
     *
58
     * @param \TechDivision\Import\Subjects\SubjectInterface $subject The subject to initialize the observers for
59
     *
60
     * @return void
61
     */
62
    public function visit(SubjectInterface $subject)
63
    {
64
        // prepare the observers
65
        foreach ($subject->getConfiguration()->getObservers() as $observers) {
66
            $this->prepareObservers($subject, $observers);
67
        }
68
    }
69
70
    /**
71
     * Prepare the observers defined in the system configuration.
72
     *
73
     * @param \TechDivision\Import\Subjects\SubjectInterface $subject   The subject to prepare the observers for
74
     * @param array                                          $observers The array with the observers
75
     * @param string                                         $type      The actual observer type
76
     *
77
     * @return void
78
     */
79
    protected function prepareObservers(SubjectInterface $subject, array $observers, $type = null)
80
    {
81
82
        // iterate over the array with observers and prepare them
83
        foreach ($observers as $key => $observer) {
84
            // we have to initialize the type only on the first level
85
            if ($type == null) {
86
                $type = $key;
87
            }
88
89
            // query whether or not we've an subarry or not
90
            if (is_array($observer)) {
91
                $this->prepareObservers($subject, $observer, $type);
92
            } else {
93
                $observerInstance = $this->container->get($observer);
94
                $observerInstance->setSubject($subject);
95
                $subject->registerObserver($observerInstance, $type);
96
            }
97
        }
98
    }
99
}
100