Completed
Push — master ( d07a45...da6ba5 )
by
unknown
02:06
created

src/Callbacks/CallbackVisitor.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * TechDivision\Import\Callbacks\CallbackVisitor
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\Callbacks;
22
23
use TechDivision\Import\Subjects\SubjectInterface;
24
use Symfony\Component\DependencyInjection\TaggedContainerInterface;
25
26
/**
27
 * Visitor implementation for a subject's callbacks.
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 CallbackVisitor implements CallbackVisitorInterface
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
65
        // load the callback mappings
66
        $callbackMappings = $subject->getCallbackMappings();
67
68
        // prepare the callbacks
69
        foreach ($callbackMappings as $type => $callbacks) {
70
            $this->prepareCallbacks($subject, $callbacks, $type);
71
        }
72
    }
73
74
    /**
75
     * Prepare the callbacks defined in the system configuration.
76
     *
77
     * @param \TechDivision\Import\Subjects\SubjectInterface $subject   The subject to prepare the callbacks for
78
     * @param array                                          $callbacks The array with the callbacks
79
     * @param string                                         $type      The actual callback type
80
     *
81
     * @return void
82
     */
83 View Code Duplication
    protected function prepareCallbacks(SubjectInterface $subject, array $callbacks, $type = null)
0 ignored issues
show
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 callbacks and prepare them
87
        foreach ($callbacks as $key => $callback) {
88
            // we have to initialize the type only on the first level
89
            if ($type == null) {
90
                $type = $key;
91
            }
92
93
            // query whether or not we've an subarry or not
94
            if (is_array($callback)) {
95
                $this->prepareCallbacks($subject, $callback, $type);
96
            } else {
97
                // create the instance of the callback/factory
98
                $instance = $this->container->get($callback);
99
                // query whether or not a factory has been specified
100
                if ($instance instanceof CallbackFactoryInterface) {
101
                    $subject->registerCallback($instance->createCallback($subject), $type);
102
                } elseif ($instance instanceof CallbackInterface) {
103
                    $subject->registerCallback($instance, $type);
104
                } else {
105
                    throw new \InvalidArgumentException(
106
                        sprintf(
107
                            'Instance of "%s" doesn\'t implement interface "%s" or "%s"',
108
                            $callback,
109
                            CallbackFactoryInterface::class,
110
                            CallbackInterface::class
111
                        )
112
                    );
113
                }
114
            }
115
        }
116
    }
117
}
118