Completed
Push — 8.x ( 824af6 )
by Tim
09:11
created

GenericCompositeObserver   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 0
loc 108
c 0
b 0
f 0
ccs 0
cts 36
cp 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 32 3
A getSubject() 0 4 1
A addObserver() 0 4 1
A setSubject() 0 4 1
A getObservers() 0 4 1
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
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 action on the events the listener has been registered for.
61
     *
62
     * @param \TechDivision\Import\Subjects\SubjectInterface $subject The subject instance
63
     *
64
     * @return array The modified row
65
     */
66
    public function handle(SubjectInterface $subject)
67
    {
68
69
        // initialize the subject
70
        $this->setSubject($subject);
71
72
        // process the observers
73
        foreach ($this->getObservers() as $observer) {
74
            // query whether or not we have to skip the row
75
            if ($subject->rowHasToBeSkipped()) {
76
                // log a debug message with the actual line nr/file information
77
                $subject->getSystemLogger()->warning(
78
                    $subject->appendExceptionSuffix(
79
                        sprintf(
80
                            'Skip processing operation "%s" after observer "%s"',
81
                            $subject->getOperationName(),
82
                            $subject->getConfiguration()->getId()
83
                        )
84
                    )
85
                );
86
87
                // skip the row
88
                break;
89
            }
90
91
            // if not, set the subject and process the observer
92
            $subject->setRow($observer->handle($subject));
93
        }
94
95
        // returns the subject's row
96
        return $subject->getRow();
97
    }
98
99
    /**
100
     * Return's the observer's subject instance.
101
     *
102
     * @return \TechDivision\Import\Subjects\SubjectInterface The observer's subject instance
103
     */
104
    public function getSubject()
105
    {
106
        return $this->subject;
107
    }
108
109
    /**
110
     * Adds the passed observer to the composites array with observers.
111
     *
112
     * @param \TechDivision\Import\Observers\ObserverInterface $observer The observer to add
113
     *
114
     * @return void
115
     */
116
    public function addObserver(ObserverInterface $observer)
117
    {
118
        $this->observers[] = $observer;
119
    }
120
121
    /**
122
     * Set's the obeserver's subject instance to initialize the observer with.
123
     *
124
     * @param \TechDivision\Import\Subjects\SubjectInterface $subject The observer's subject
125
     *
126
     * @return void
127
     */
128
    protected function setSubject(SubjectInterface $subject)
129
    {
130
        $this->subject = $subject;
131
    }
132
133
    /**
134
     * Returns the array with the composite's observers.
135
     *
136
     * @return \TechDivision\Import\Observers\ObserverInterface[]
137
     */
138
    protected function getObservers()
139
    {
140
        return $this->observers;
141
    }
142
}
143