Code Duplication    Length = 65-65 lines in 2 locations

src/Callbacks/CallbackVisitor.php 1 location

@@ 35-99 (lines=65) @@
32
 * @link      https://github.com/techdivision/import
33
 * @link      http://www.techdivision.com
34
 */
35
class CallbackVisitor
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
    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 callbacks
65
        foreach ($subject->getCallbackMappings() as $type => $callbacks) {
66
            $this->prepareCallbacks($subject, $callbacks, $type);
67
        }
68
    }
69
70
    /**
71
     * Prepare the callbacks defined in the system configuration.
72
     *
73
     * @param \TechDivision\Import\Subjects\SubjectInterface $subject   The subject to prepare the callbacks for
74
     * @param array                                          $callbacks The array with the callbacks
75
     * @param string                                         $type      The actual callback type
76
     *
77
     * @return void
78
     */
79
    public function prepareCallbacks(SubjectInterface $subject, array $callbacks, $type = null)
80
    {
81
82
        // iterate over the array with callbacks and prepare them
83
        foreach ($callbacks as $key => $callback) {
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($callback)) {
91
                $this->prepareCallbacks($subject, $callback, $type);
92
            } else {
93
                $callbackInstance = $this->container->get($callback);
94
                $callbackInstance->setSubject($subject);
95
                $subject->registerCallback($callbackInstance, $type);
96
            }
97
        }
98
    }
99
}
100

src/Observers/ObserverVisitor.php 1 location

@@ 35-99 (lines=65) @@
32
 * @link      https://github.com/techdivision/import
33
 * @link      http://www.techdivision.com
34
 */
35
class ObserverVisitor
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
    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