Completed
Push — master ( 313f20...c23fe9 )
by Tim
9s
created

AbstractAction   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 176
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 58.33%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 1
dl 0
loc 176
ccs 21
cts 36
cp 0.5833
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 21 4
A setCreateProcessor() 0 4 1
A getCreateProcessor() 0 4 1
A setDeleteProcessor() 0 4 1
A getDeleteProcessor() 0 4 1
A setUpdateProcessor() 0 4 1
A getUpdateProcessor() 0 4 1
A persist() 0 9 1
A create() 0 4 1
A delete() 0 4 1
A update() 0 4 1
1
<?php
2
3
/**
4
 * TechDivision\Import\Actions\AbstractAction
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\Actions;
22
23
use TechDivision\Import\Utils\EntityStatus;
24
use TechDivision\Import\Actions\Processors\ProcessorInterface;
25
26
/**
27
 * An abstract action implementation.
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
abstract class AbstractAction implements ActionInterface
36
{
37
38
    /**
39
     * The create processor instance.
40
     *
41
     * @var \TechDivision\Import\Actions\Processors\ProcessorInterface
42
     */
43
    protected $createProcessor;
44
45
    /**
46
     * The delete processor instance.
47
     *
48
     * @var \TechDivision\Import\Actions\Processors\ProcessorInterface
49
     */
50
    protected $deleteProcessor;
51
52
    /**
53
     * The update processor instance.
54
     *
55
     * @var \TechDivision\Import\Actions\Processors\ProcessorInterface
56
     */
57
    protected $updateProcessor;
58
59
    /**
60
     * Initialize the instance with the passed processors.
61
     *
62
     * @param \TechDivision\Import\Actions\Processors\ProcessorInterface|null $createProcessor The create processor instance
63
     * @param \TechDivision\Import\Actions\Processors\ProcessorInterface|null $updateProcessor The update processor instance
64
     * @param \TechDivision\Import\Actions\Processors\ProcessorInterface|null $deleteProcessor The delete processor instance
65
     */
66 4
    public function __construct(
67
        ProcessorInterface $createProcessor = null,
68
        ProcessorInterface $updateProcessor = null,
69
        ProcessorInterface $deleteProcessor = null
70
    ) {
71
72
        // query whether or not a create processor has been passed
73 4
        if ($createProcessor instanceof ProcessorInterface) {
74
            $this->setCreateProcessor($createProcessor);
75
        }
76
77
        // query whether or not a update processor has been passed
78 4
        if ($updateProcessor instanceof ProcessorInterface) {
79
            $this->setUpdateProcessor($updateProcessor);
80
        }
81
82
        // query whether or not a delete processor has been passed
83 4
        if ($deleteProcessor instanceof ProcessorInterface) {
84
            $this->setDeleteProcessor($deleteProcessor);
85
        }
86 4
    }
87
88
    /**
89
     * Set's the create processor instance.
90
     *
91
     * @param \TechDivision\Import\Actions\Processors\ProcessorInterface $createProcessor The create processor instance to use
92
     *
93
     * @return void
94
     */
95 1
    public function setCreateProcessor(ProcessorInterface $createProcessor)
96
    {
97 1
        $this->createProcessor = $createProcessor;
98 1
    }
99
100
    /**
101
     * Return's the create processor instance.
102
     *
103
     * @return \TechDivision\Import\Actions\Processors\ProcessorInterface The create processor instance
104
     */
105 1
    public function getCreateProcessor()
106
    {
107 1
        return $this->createProcessor;
108
    }
109
110
    /**
111
     * Set's the delete processor instance.
112
     *
113
     * @param \TechDivision\Import\Actions\Processors\ProcessorInterface $deleteProcessor The delete processor instance to use
114
     *
115
     * @return void
116
     */
117 1
    public function setDeleteProcessor(ProcessorInterface $deleteProcessor)
118
    {
119 1
        $this->deleteProcessor = $deleteProcessor;
120 1
    }
121
122
    /**
123
     * Return's the delete processor instance.
124
     *
125
     * @return \TechDivision\Import\Actions\Processors\ProcessorInterface The delete processor instance
126
     */
127 1
    public function getDeleteProcessor()
128
    {
129 1
        return $this->deleteProcessor;
130
    }
131
132
    /**
133
     * Set's the update processor instance.
134
     *
135
     * @param \TechDivision\Import\Actions\Processors\ProcessorInterface $updateProcessor The update processor instance to use
136
     *
137
     * @return void
138
     */
139
    public function setUpdateProcessor(ProcessorInterface $updateProcessor)
140
    {
141
        $this->updateProcessor = $updateProcessor;
142
    }
143
144
    /**
145
     * Return's the update processor instance.
146
     *
147
     * @return \TechDivision\Import\Actions\Processors\ProcessorInterface The update processor instance
148
     */
149
    public function getUpdateProcessor()
150
    {
151
        return $this->updateProcessor;
152
    }
153
154
    /**
155
     * Helper method that create/update the passed entity, depending on
156
     * the entity's status.
157
     *
158
     * @param array $row The entity data to create/update
159
     *
160
     * @return void
161
     */
162
    public function persist(array $row)
163
    {
164
165
        // load the method name
166
        $methodName = $row[EntityStatus::MEMBER_NAME];
167
168
        // invoke the method
169
        $this->$methodName($row);
170
    }
171
172
    /**
173
     * Creates's the entity with the passed attributes.
174
     *
175
     * @param array       $row  The attributes of the entity to create
176
     * @param string|null $name The name of the prepared statement that has to be executed
177
     *
178
     * @return void
179
     */
180 1
    public function create($row, $name = null)
181
    {
182 1
        $this->getCreateProcessor()->execute($row, $name);
183 1
    }
184
185
    /**
186
     * Delete's the entity with the passed attributes.
187
     *
188
     * @param array       $row  The attributes of the entity to delete
189
     * @param string|null $name The name of the prepared statement that has to be executed
190
     *
191
     * @return void
192
     */
193 1
    public function delete($row, $name = null)
194
    {
195 1
        $this->getDeleteProcessor()->execute($row, $name);
196 1
    }
197
198
    /**
199
     * Update's the entity with the passed attributes.
200
     *
201
     * @param array       $row  The attributes of the entity to update
202
     * @param string|null $name The name of the prepared statement that has to be executed
203
     *
204
     * @return void
205
     */
206
    public function update($row, $name = null)
207
    {
208
        $this->getUpdateProcessor()->execute($row, $name);
209
    }
210
}
211