Completed
Push — 14.x ( 855d13...227841 )
by Tim
02:29
created

ObserverVisitor   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 83
Duplicated Lines 40.96 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 76.92%

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 4
dl 34
loc 83
ccs 20
cts 26
cp 0.7692
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A visit() 0 11 2
B prepareObservers() 34 34 6

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
class ObserverVisitor implements ObserverVisitorInterface
36
{
37
38
    /**
39
     * The DI container builder instance.
40
     *
41
     * @var \Symfony\Component\DependencyInjection\TaggedContainerInterface
42
     */
43
    protected $container;
44
45
    /**
46
     * The constructor to initialize the instance.
47
     *
48
     * @param \Symfony\Component\DependencyInjection\TaggedContainerInterface $container The container instance
49
     */
50 1
    public function __construct(TaggedContainerInterface $container)
51
    {
52 1
        $this->container = $container;
53 1
    }
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 1
    public function visit(SubjectInterface $subject)
63
    {
64
65
        // load the observers from the configuration
66 1
        $availableObservers = $subject->getConfiguration()->getObservers();
67
68
        // prepare the observers
69 1
        foreach ($availableObservers as $observers) {
70 1
            $this->prepareObservers($subject, $observers);
71
        }
72 1
    }
73
74
    /**
75
     * Prepare the observers defined in the system configuration.
76
     *
77
     * @param \TechDivision\Import\Subjects\SubjectInterface $subject   The subject to prepare the observers for
78
     * @param array                                          $observers The array with the observers
79
     * @param string                                         $type      The actual observer type
80
     *
81
     * @return void
82
     */
83 1 View Code Duplication
    protected function prepareObservers(SubjectInterface $subject, array $observers, $type = null)
0 ignored issues
show
Duplication introduced by
This method 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...
84
    {
85
86
        // iterate over the array with observers and prepare them
87 1
        foreach ($observers as $key => $observer) {
88
            // we have to initialize the type only on the first level
89 1
            if ($type == null) {
90 1
                $type = $key;
91
            }
92
93
            // query whether or not we've an subarry or not
94 1
            if (is_array($observer)) {
95 1
                $this->prepareObservers($subject, $observer, $type);
96
            } else {
97
                // create the instance of the observer/factory
98 1
                $instance = $this->container->get($observer);
99
                // query whether or not a factory has been specified
100 1
                if ($instance instanceof ObserverFactoryInterface) {
101
                    $subject->registerObserver($instance->createObserver($subject), $type);
102 1
                } elseif ($instance instanceof ObserverInterface) {
103 1
                    $subject->registerObserver($instance, $type);
104
                } else {
105
                    throw new \InvalidArgumentException(
106
                        sprintf(
107
                            'Instance of "%s" doesn\'t implement interface "%s" or "%s"',
108
                            $observer,
109
                            ObserverFactoryInterface::class,
110 1
                            ObserverInterface::class
111
                        )
112
                    );
113
                }
114
            }
115
        }
116 1
    }
117
}
118