-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractAction.php
More file actions
230 lines (204 loc) · 7.17 KB
/
AbstractAction.php
File metadata and controls
230 lines (204 loc) · 7.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php
/**
* TechDivision\Import\Dbal\Actions\AbstractAction
*
* PHP version 7
*
* @author Tim Wagner <t.wagner@techdivision.com>
* @copyright 2021 TechDivision GmbH <info@techdivision.com>
* @license https://opensource.org/licenses/MIT
* @link https://github.com/techdivision/import-dbal-collection
* @link http://www.techdivision.com
*/
namespace TechDivision\Import\Dbal\Collection\Actions;
use TechDivision\Import\Dbal\Utils\EntityStatus;
use TechDivision\Import\Dbal\Actions\ActionInterface;
use TechDivision\Import\Dbal\Actions\Processors\ProcessorInterface;
use TechDivision\Import\Dbal\Actions\ProcessorAwareActionInterface;
use TechDivision\Import\Dbal\Actions\PrimaryKeyAwareActionInterface;
/**
* An abstract action implementation.
*
* @author Tim Wagner <t.wagner@techdivision.com>
* @copyright 2021 TechDivision GmbH <info@techdivision.com>
* @license https://opensource.org/licenses/MIT
* @link https://github.com/techdivision/import-dbal-collection
* @link http://www.techdivision.com
*/
abstract class AbstractAction implements ActionInterface, ProcessorAwareActionInterface, PrimaryKeyAwareActionInterface
{
/**
* The primary key name to use.
*
* @var string
*/
protected $primaryKeyMemberName;
/**
* The create processor instance.
*
* @var \TechDivision\Import\Dbal\Actions\Processors\ProcessorInterface
*/
protected $createProcessor;
/**
* The delete processor instance.
*
* @var \TechDivision\Import\Dbal\Actions\Processors\ProcessorInterface
*/
protected $deleteProcessor;
/**
* The update processor instance.
*
* @var \TechDivision\Import\Dbal\Actions\Processors\ProcessorInterface
*/
protected $updateProcessor;
/**
* Initialize the instance with the passed processors.
*
* @param \TechDivision\Import\Dbal\Actions\Processors\ProcessorInterface|null $createProcessor The create processor instance
* @param \TechDivision\Import\Dbal\Actions\Processors\ProcessorInterface|null $updateProcessor The update processor instance
* @param \TechDivision\Import\Dbal\Actions\Processors\ProcessorInterface|null $deleteProcessor The delete processor instance
* @param string|null $primaryKeyMemberName The primary key member name
*/
public function __construct(
?ProcessorInterface $createProcessor = null,
?ProcessorInterface $updateProcessor = null,
?ProcessorInterface $deleteProcessor = null,
$primaryKeyMemberName = null
) {
// query whether or not a create processor has been passed
if ($createProcessor instanceof ProcessorInterface) {
$this->setCreateProcessor($createProcessor);
}
// query whether or not a update processor has been passed
if ($updateProcessor instanceof ProcessorInterface) {
$this->setUpdateProcessor($updateProcessor);
}
// query whether or not a delete processor has been passed
if ($deleteProcessor instanceof ProcessorInterface) {
$this->setDeleteProcessor($deleteProcessor);
}
// initialize the primary key member name
$this->primaryKeyMemberName = $primaryKeyMemberName;
}
/**
* Return's the primary key member name of the entity to persist.
*
* @return string The primary key member name
*/
public function getPrimaryKeyMemberName()
{
return $this->primaryKeyMemberName;
}
/**
* Set's the create processor instance.
*
* @param \TechDivision\Import\Dbal\Actions\Processors\ProcessorInterface $createProcessor The create processor instance to use
*
* @return void
*/
public function setCreateProcessor(ProcessorInterface $createProcessor)
{
$this->createProcessor = $createProcessor;
}
/**
* Return's the create processor instance.
*
* @return \TechDivision\Import\Dbal\Actions\Processors\ProcessorInterface The create processor instance
*/
public function getCreateProcessor()
{
return $this->createProcessor;
}
/**
* Set's the delete processor instance.
*
* @param \TechDivision\Import\Dbal\Actions\Processors\ProcessorInterface $deleteProcessor The delete processor instance to use
*
* @return void
*/
public function setDeleteProcessor(ProcessorInterface $deleteProcessor)
{
$this->deleteProcessor = $deleteProcessor;
}
/**
* Return's the delete processor instance.
*
* @return \TechDivision\Import\Dbal\Actions\Processors\ProcessorInterface The delete processor instance
*/
public function getDeleteProcessor()
{
return $this->deleteProcessor;
}
/**
* Set's the update processor instance.
*
* @param \TechDivision\Import\Dbal\Actions\Processors\ProcessorInterface $updateProcessor The update processor instance to use
*
* @return void
*/
public function setUpdateProcessor(ProcessorInterface $updateProcessor)
{
$this->updateProcessor = $updateProcessor;
}
/**
* Return's the update processor instance.
*
* @return \TechDivision\Import\Dbal\Actions\Processors\ProcessorInterface The update processor instance
*/
public function getUpdateProcessor()
{
return $this->updateProcessor;
}
/**
* Helper method that create/update the passed entity, depending on
* the entity's status.
*
* @param array $row The entity data to create/update
* @param string|null $name The name of the prepared statement that has to be executed
*
* @return void
*/
public function persist(array $row, $name = null)
{
// load the method name
$methodName = $row[EntityStatus::MEMBER_NAME];
// invoke the method
$this->$methodName($row, $name);
}
/**
* Creates's the entity with the passed attributes.
*
* @param array $row The attributes of the entity to create
* @param string|null $name The name of the prepared statement that has to be executed
*
* @return void
*/
public function create(array $row, $name = null)
{
$this->getCreateProcessor()->execute($row, $name, $this->getPrimaryKeyMemberName());
}
/**
* Delete's the entity with the passed attributes.
*
* @param array $row The attributes of the entity to delete
* @param string|null $name The name of the prepared statement that has to be executed
*
* @return void
*/
public function delete(array $row, $name = null)
{
$this->getDeleteProcessor()->execute($row, $name, $this->getPrimaryKeyMemberName());
}
/**
* Update's the entity with the passed attributes.
*
* @param array $row The attributes of the entity to update
* @param string|null $name The name of the prepared statement that has to be executed
*
* @return void
*/
public function update(array $row, $name = null)
{
$this->getUpdateProcessor()->execute($row, $name, $this->getPrimaryKeyMemberName());
}
}