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

AbstractAction::__construct()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4.5923

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 6
cts 9
cp 0.6667
rs 9.52
c 0
b 0
f 0
cc 4
nc 8
nop 4
crap 4.5923
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 primary key name to use.
40
     *
41
     * @var string
42
     */
43
    protected $primaryKeyMemberName;
44
45
    /**
46
     * The create processor instance.
47
     *
48
     * @var \TechDivision\Import\Actions\Processors\ProcessorInterface
49
     */
50
    protected $createProcessor;
51
52
    /**
53
     * The delete processor instance.
54
     *
55
     * @var \TechDivision\Import\Actions\Processors\ProcessorInterface
56
     */
57
    protected $deleteProcessor;
58
59
    /**
60
     * The update processor instance.
61
     *
62
     * @var \TechDivision\Import\Actions\Processors\ProcessorInterface
63
     */
64
    protected $updateProcessor;
65
66
    /**
67
     * Initialize the instance with the passed processors.
68
     *
69
     * @param \TechDivision\Import\Actions\Processors\ProcessorInterface|null $createProcessor      The create processor instance
70
     * @param \TechDivision\Import\Actions\Processors\ProcessorInterface|null $updateProcessor      The update processor instance
71
     * @param \TechDivision\Import\Actions\Processors\ProcessorInterface|null $deleteProcessor      The delete processor instance
72
     * @param string|null                                                     $primaryKeyMemberName The primary key member name
73
     */
74 6
    public function __construct(
75
        ProcessorInterface $createProcessor = null,
76
        ProcessorInterface $updateProcessor = null,
77
        ProcessorInterface $deleteProcessor = null,
78
        $primaryKeyMemberName = null
79
    ) {
80
81
        // query whether or not a create processor has been passed
82 6
        if ($createProcessor instanceof ProcessorInterface) {
83
            $this->setCreateProcessor($createProcessor);
84
        }
85
86
        // query whether or not a update processor has been passed
87 6
        if ($updateProcessor instanceof ProcessorInterface) {
88
            $this->setUpdateProcessor($updateProcessor);
89
        }
90
91
        // query whether or not a delete processor has been passed
92 6
        if ($deleteProcessor instanceof ProcessorInterface) {
93
            $this->setDeleteProcessor($deleteProcessor);
94
        }
95
96
        // initialize the primary key member name
97 6
        $this->primaryKeyMemberName = $primaryKeyMemberName;
98 6
    }
99
100
    /**
101
     * Return's the primary key member name of the entity to persist.
102
     *
103
     * @return string The primary key member name
104
     */
105 4
    public function getPrimaryKeyMemberName()
106
    {
107 4
        return $this->primaryKeyMemberName;
108
    }
109
110
    /**
111
     * Set's the create processor instance.
112
     *
113
     * @param \TechDivision\Import\Actions\Processors\ProcessorInterface $createProcessor The create processor instance to use
114
     *
115
     * @return void
116
     */
117 1
    public function setCreateProcessor(ProcessorInterface $createProcessor)
118
    {
119 1
        $this->createProcessor = $createProcessor;
120 1
    }
121
122
    /**
123
     * Return's the create processor instance.
124
     *
125
     * @return \TechDivision\Import\Actions\Processors\ProcessorInterface The create processor instance
126
     */
127 1
    public function getCreateProcessor()
128
    {
129 1
        return $this->createProcessor;
130
    }
131
132
    /**
133
     * Set's the delete processor instance.
134
     *
135
     * @param \TechDivision\Import\Actions\Processors\ProcessorInterface $deleteProcessor The delete processor instance to use
136
     *
137
     * @return void
138
     */
139 1
    public function setDeleteProcessor(ProcessorInterface $deleteProcessor)
140
    {
141 1
        $this->deleteProcessor = $deleteProcessor;
142 1
    }
143
144
    /**
145
     * Return's the delete processor instance.
146
     *
147
     * @return \TechDivision\Import\Actions\Processors\ProcessorInterface The delete processor instance
148
     */
149 1
    public function getDeleteProcessor()
150
    {
151 1
        return $this->deleteProcessor;
152
    }
153
154
    /**
155
     * Set's the update processor instance.
156
     *
157
     * @param \TechDivision\Import\Actions\Processors\ProcessorInterface $updateProcessor The update processor instance to use
158
     *
159
     * @return void
160
     */
161
    public function setUpdateProcessor(ProcessorInterface $updateProcessor)
162
    {
163
        $this->updateProcessor = $updateProcessor;
164
    }
165
166
    /**
167
     * Return's the update processor instance.
168
     *
169
     * @return \TechDivision\Import\Actions\Processors\ProcessorInterface The update processor instance
170
     */
171
    public function getUpdateProcessor()
172
    {
173
        return $this->updateProcessor;
174
    }
175
176
    /**
177
     * Helper method that create/update the passed entity, depending on
178
     * the entity's status.
179
     *
180
     * @param array       $row  The entity data to create/update
181
     * @param string|null $name The name of the prepared statement that has to be executed
182
     *
183
     * @return void
184
     */
185
    public function persist(array $row, $name = null)
186
    {
187
188
        // load the method name
189
        $methodName = $row[EntityStatus::MEMBER_NAME];
190
191
        // invoke the method
192
        $this->$methodName($row, $name);
193
    }
194
195
    /**
196
     * Creates's the entity with the passed attributes.
197
     *
198
     * @param array       $row  The attributes of the entity to create
199
     * @param string|null $name The name of the prepared statement that has to be executed
200
     *
201
     * @return void
202
     */
203 2
    public function create(array $row, $name = null)
204
    {
205 2
        $this->getCreateProcessor()->execute($row, $name, $this->getPrimaryKeyMemberName());
206 2
    }
207
208
    /**
209
     * Delete's the entity with the passed attributes.
210
     *
211
     * @param array       $row  The attributes of the entity to delete
212
     * @param string|null $name The name of the prepared statement that has to be executed
213
     *
214
     * @return void
215
     */
216 2
    public function delete(array $row, $name = null)
217
    {
218 2
        $this->getDeleteProcessor()->execute($row, $name, $this->getPrimaryKeyMemberName());
219 2
    }
220
221
    /**
222
     * Update's the entity with the passed attributes.
223
     *
224
     * @param array       $row  The attributes of the entity to update
225
     * @param string|null $name The name of the prepared statement that has to be executed
226
     *
227
     * @return void
228
     */
229
    public function update(array $row, $name = null)
230
    {
231
        $this->getUpdateProcessor()->execute($row, $name, $this->getPrimaryKeyMemberName());
232
    }
233
}
234