Completed
Push — master ( 3a08fe...aee93b )
by Tim
18:05 queued 16:13
created

GenericCompositeObserver::tearDown()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 9
cp 0
rs 9.8333
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 12
1
<?php
2
3
/**
4
 * TechDivision\Import\Observers\GenericCompositeObserver
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 TechDivision\Import\Interfaces\HookAwareInterface;
25
26
/**
27
 * A generic observer implementation that implements the composit pattern to bundle
28
 * the necessary observers of a special use case to simplify configuration.
29
 *
30
 * @author    Tim Wagner <[email protected]>
31
 * @copyright 2016 TechDivision GmbH <[email protected]>
32
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
33
 * @link      https://github.com/techdivision/import
34
 * @link      http://www.techdivision.com
35
 */
36
class GenericCompositeObserver implements ObserverInterface, ObserverFactoryInterface
37
{
38
39
    /**
40
     * The composite's subject instance.
41
     *
42
     * @var \TechDivision\Import\Subjects\SubjectInterface
43
     */
44
    protected $subject;
45
46
    /**
47
     * The actual row that will be processed by the composite's observers.
48
     *
49
     * @var array
50
     */
51
    protected $row = array();
52
53
    /**
54
     * The array with the composite's observers.
55
     *
56
     * @var \TechDivision\Import\Observers\ObserverInterface[]
57
     */
58
    protected $observers = array();
59
60
    /**
61
     * Will be invoked by the observer visitor when a factory has been defined to create the observer instance.
62
     *
63
     * @param \TechDivision\Import\Subjects\SubjectInterface $subject The subject instance
64
     *
65
     * @return \TechDivision\Import\Observers\ObserverInterface The observer instance
66
     */
67
    public function createObserver(SubjectInterface $subject)
68
    {
69
70
        // iterate over the observers to figure out which of them implements the factory intereface
71
        foreach ($this->observers as $key => $observer) {
72
            // query whether or not a factory has been specified
73
            if ($observer instanceof ObserverFactoryInterface) {
74
                $this->observers[$key] = $observer->createObserver($subject);
75
            }
76
        }
77
78
        // finally, return the composite observer instance
79
        return $this;
80
    }
81
82
    /**
83
     * Will be invoked by the action on the events the listener has been registered for.
84
     *
85
     * @param \TechDivision\Import\Subjects\SubjectInterface $subject The subject instance
86
     *
87
     * @return array The modified row
88
     */
89
    public function handle(SubjectInterface $subject)
90
    {
91
92
        // initialize the subject
93
        $this->setSubject($subject);
94
95
        // load the observers
96
        $observers = $this->getObservers();
97
98
        // process the observers
99
        foreach ($observers as $observer) {
100
            // query whether or not we have to skip the row
101
            if ($subject->rowHasToBeSkipped()) {
102
                // log a debug message with the actual line nr/file information
103
                $subject->getSystemLogger()->debug(
104
                    $subject->appendExceptionSuffix(
105
                        sprintf(
106
                            'Skip processing operation "%s" after observer "%s"',
107
                            $subject->getFullOperationName(),
108
                            $subject->getConfiguration()->getId()
109
                        )
110
                    )
111
                );
112
113
                // skip the row
114
                break;
115
            }
116
117
            // if not, set the subject and process the observer
118
            $subject->setRow($observer->handle($subject));
119
        }
120
121
        // returns the subject's row
122
        return $subject->getRow();
123
    }
124
125
    /**
126
     * Return's the observer's subject instance.
127
     *
128
     * @return \TechDivision\Import\Subjects\SubjectInterface The observer's subject instance
129
     */
130
    public function getSubject()
131
    {
132
        return $this->subject;
133
    }
134
135
    /**
136
     * Adds the passed observer to the composites array with observers.
137
     *
138
     * @param \TechDivision\Import\Observers\ObserverInterface $observer The observer to add
139
     *
140
     * @return void
141
     */
142
    public function addObserver(ObserverInterface $observer)
143
    {
144
        $this->observers[] = $observer;
145
    }
146
147
    /**
148
     * Set's the obeserver's subject instance to initialize the observer with.
149
     *
150
     * @param \TechDivision\Import\Subjects\SubjectInterface $subject The observer's subject
151
     *
152
     * @return void
153
     */
154
    protected function setSubject(SubjectInterface $subject)
155
    {
156
        $this->subject = $subject;
157
    }
158
159
    /**
160
     * Returns the array with the composite's observers.
161
     *
162
     * @return \TechDivision\Import\Observers\ObserverInterface[]
163
     */
164
    protected function getObservers()
165
    {
166
        return $this->observers;
167
    }
168
169
    /**
170
     * Intializes the previously loaded global data for exactly one bunch.
171
     *
172
     * @param string $serial The serial of the actual import
173
     *
174
     * @return void
175
     */
176
    public function setUp($serial)
177
    {
178
179
        // load the composite's observers
180
        $observers = $this->getObservers();
181
182
        // set-up all hook aware observers
183
        foreach ($observers as $observer) {
184
            if ($observer instanceof HookAwareInterface) {
185
                $observer->setUp($serial);
186
            }
187
        }
188
    }
189
190
    /**
191
     * Clean up the global data after importing the variants.
192
     *
193
     * @param string $serial The serial of the actual import
194
     *
195
     * @return void
196
     */
197
    public function tearDown($serial)
198
    {
199
200
        // load the composite's observers
201
        $observers = $this->getObservers();
202
203
        // tear down all hook aware observers
204
        foreach ($observers as $observer) {
205
            if ($observer instanceof HookAwareInterface) {
206
                $observer->tearDown($serial);
207
            }
208
        }
209
    }
210
}
211