Completed
Push — master ( b5c840...3d5461 )
by Marcus
04:46
created

GenericCompositeObserver::createObserver()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 0
cts 9
cp 0
rs 9.7998
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
25
/**
26
 * A generic observer implementation that implements the composit pattern to bundle
27
 * the necessary observers of a special use case to simplify configuration.
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 GenericCompositeObserver implements ObserverInterface, ObserverFactoryInterface
36
{
37
38
    /**
39
     * The composite's subject instance.
40
     *
41
     * @var \TechDivision\Import\Subjects\SubjectInterface
42
     */
43
    protected $subject;
44
45
    /**
46
     * The actual row that will be processed by the composite's observers.
47
     *
48
     * @var array
49
     */
50
    protected $row = array();
51
52
    /**
53
     * The array with the composite's observers.
54
     *
55
     * @var \TechDivision\Import\Observers\ObserverInterface[]
56
     */
57
    protected $observers = array();
58
59
    /**
60
     * Will be invoked by the observer visitor when a factory has been defined to create the observer instance.
61
     *
62
     * @param \TechDivision\Import\Subjects\SubjectInterface $subject The subject instance
63
     *
64
     * @return \TechDivision\Import\Observers\ObserverInterface The observer instance
65
     */
66
    public function createObserver(SubjectInterface $subject)
67
    {
68
69
        // iterate over the observers to figure out which of them implements the factory intereface
70
        foreach ($this->observers as $key => $observer) {
71
            // query whether or not a factory has been specified
72
            if ($observer instanceof ObserverFactoryInterface) {
73
                $this->observers[$key] = $observer->createObserver($subject);
74
            }
75
        }
76
77
        // finally, return the composite observer instance
78
        return $this;
79
    }
80
81
    /**
82
     * Will be invoked by the action on the events the listener has been registered for.
83
     *
84
     * @param \TechDivision\Import\Subjects\SubjectInterface $subject The subject instance
85
     *
86
     * @return array The modified row
87
     */
88
    public function handle(SubjectInterface $subject)
89
    {
90
91
        // initialize the subject
92
        $this->setSubject($subject);
93
94
        // load the observers
95
        $observers = $this->getObservers();
96
97
        // process the observers
98
        foreach ($observers as $observer) {
99
            // query whether or not we have to skip the row
100
            if ($subject->rowHasToBeSkipped()) {
101
                // log a debug message with the actual line nr/file information
102
                $subject->getSystemLogger()->debug(
103
                    $subject->appendExceptionSuffix(
104
                        sprintf(
105
                            'Skip processing operation "%s" after observer "%s"',
106
                            $subject->getFullOperationName(),
107
                            $subject->getConfiguration()->getId()
108
                        )
109
                    )
110
                );
111
112
                // skip the row
113
                break;
114
            }
115
116
            // if not, set the subject and process the observer
117
            $subject->setRow($observer->handle($subject));
118
        }
119
120
        // returns the subject's row
121
        return $subject->getRow();
122
    }
123
124
    /**
125
     * Return's the observer's subject instance.
126
     *
127
     * @return \TechDivision\Import\Subjects\SubjectInterface The observer's subject instance
128
     */
129
    public function getSubject()
130
    {
131
        return $this->subject;
132
    }
133
134
    /**
135
     * Adds the passed observer to the composites array with observers.
136
     *
137
     * @param \TechDivision\Import\Observers\ObserverInterface $observer The observer to add
138
     *
139
     * @return void
140
     */
141
    public function addObserver(ObserverInterface $observer)
142
    {
143
        $this->observers[] = $observer;
144
    }
145
146
    /**
147
     * Set's the obeserver's subject instance to initialize the observer with.
148
     *
149
     * @param \TechDivision\Import\Subjects\SubjectInterface $subject The observer's subject
150
     *
151
     * @return void
152
     */
153
    protected function setSubject(SubjectInterface $subject)
154
    {
155
        $this->subject = $subject;
156
    }
157
158
    /**
159
     * Returns the array with the composite's observers.
160
     *
161
     * @return \TechDivision\Import\Observers\ObserverInterface[]
162
     */
163
    protected function getObservers()
164
    {
165
        return $this->observers;
166
    }
167
}
168