Completed
Push — master ( fa31eb...190b3f )
by Tim
9s
created

AttributeBunchProcessor::persistAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Attribute\Services\AttributeBunchProcessor
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-attribute
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Attribute\Services;
22
23
use TechDivision\Import\Attribute\Actions\AttributeAction;
24
use TechDivision\Import\Attribute\Actions\CatalogAttributeAction;
25
use TechDivision\Import\Attribute\Repositories\AttributeRepository;
26
use TechDivision\Import\Attribute\Repositories\CatalogAttributeRepository;
27
28
/**
29
 * The attribute bunch processor implementation.
30
 *
31
 * @author    Tim Wagner <[email protected]>
32
 * @copyright 2016 TechDivision GmbH <[email protected]>
33
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
34
 * @link      https://github.com/techdivision/import-attribute
35
 * @link      http://www.techdivision.com
36
 */
37
class AttributeBunchProcessor implements AttributeBunchProcessorInterface
38
{
39
40
    /**
41
     * A PDO connection initialized with the values from the Doctrine EntityManager.
42
     *
43
     * @var \PDO
44
     */
45
    protected $connection;
46
47
    /**
48
     * The attribute repository instance.
49
     *
50
     * @var \TechDivision\Import\Attribute\Repositories\AttributeRepository
51
     */
52
    protected $attributeRepository;
53
54
    /**
55
     * The catalog attribute repository instance.
56
     *
57
     * @var \TechDivision\Import\Attribute\Repositories\CatalogAttributeRepository
58
     */
59
    protected $catalogAttributeRepository;
60
61
    /**
62
     * The attribute action instance.
63
     *
64
     * @var \TechDivision\Import\Attribute\Actions\AttributeAction
65
     */
66
    protected $attributeAction;
67
68
    /**
69
     * The attribute action instance.
70
     *
71
     * @var \TechDivision\Import\Attribute\Actions\CatalogAttributeAction
72
     */
73
    protected $catalogAttributeAction;
74
75
    /**
76
     * Initialize the processor with the necessary assembler and repository instances.
77
     *
78
     * @param \PDO                                                                   $connection                 The PDO connection to use
79
     * @param \TechDivision\Import\Attribute\Repositories\AttributeRepository        $attributeRepository        The attribute repository instance
80
     * @param \TechDivision\Import\Attribute\Repositories\CatalogAttributeRepository $catalogAttributeRepository The catalog attribute repository instance
81
     * @param \TechDivision\Import\Attribute\Actions\AttributeAction                 $attributeAction            The attribute action instance
82
     * @param \TechDivision\Import\Attribute\Actions\CatalogAttributeAction          $catalogAttributeAction     The catalog attribute action instance
83
     */
84
    public function __construct(
85
        \PDO $connection,
86
        AttributeRepository $attributeRepository,
87
        CatalogAttributeRepository $catalogAttributeRepository,
88
        AttributeAction $attributeAction,
89
        CatalogAttributeAction $catalogAttributeAction
90
    ) {
91
        $this->setConnection($connection);
92
        $this->setAttributeRepository($attributeRepository);
93
        $this->setCatalogAttributeRepository($catalogAttributeRepository);
94
        $this->setAttributeAction($attributeAction);
95
        $this->setCatalogAttributeAction($catalogAttributeAction);
96
    }
97
98
    /**
99
     * Set's the passed connection.
100
     *
101
     * @param \PDO $connection The connection to set
102
     *
103
     * @return void
104
     */
105
    public function setConnection(\PDO $connection)
106
    {
107
        $this->connection = $connection;
108
    }
109
110
    /**
111
     * Return's the connection.
112
     *
113
     * @return \PDO The connection instance
114
     */
115
    public function getConnection()
116
    {
117
        return $this->connection;
118
    }
119
120
    /**
121
     * Turns off autocommit mode. While autocommit mode is turned off, changes made to the database via the PDO
122
     * object instance are not committed until you end the transaction by calling ProductProcessor::commit().
123
     * Calling ProductProcessor::rollBack() will roll back all changes to the database and return the connection
124
     * to autocommit mode.
125
     *
126
     * @return boolean Returns TRUE on success or FALSE on failure
127
     * @link http://php.net/manual/en/pdo.begintransaction.php
128
     */
129
    public function beginTransaction()
130
    {
131
        return $this->connection->beginTransaction();
132
    }
133
134
    /**
135
     * Commits a transaction, returning the database connection to autocommit mode until the next call to
136
     * ProductProcessor::beginTransaction() starts a new transaction.
137
     *
138
     * @return boolean Returns TRUE on success or FALSE on failure
139
     * @link http://php.net/manual/en/pdo.commit.php
140
     */
141
    public function commit()
142
    {
143
        return $this->connection->commit();
144
    }
145
146
    /**
147
     * Rolls back the current transaction, as initiated by ProductProcessor::beginTransaction().
148
     *
149
     * If the database was set to autocommit mode, this function will restore autocommit mode after it has
150
     * rolled back the transaction.
151
     *
152
     * Some databases, including MySQL, automatically issue an implicit COMMIT when a database definition
153
     * language (DDL) statement such as DROP TABLE or CREATE TABLE is issued within a transaction. The implicit
154
     * COMMIT will prevent you from rolling back any other changes within the transaction boundary.
155
     *
156
     * @return boolean Returns TRUE on success or FALSE on failure
157
     * @link http://php.net/manual/en/pdo.rollback.php
158
     */
159
    public function rollBack()
160
    {
161
        return $this->connection->rollBack();
162
    }
163
164
    /**
165
     * Set's the attribute repository instance.
166
     *
167
     * @param \TechDivision\Import\Attribute\Repositories\AttributeRepository $attributeRepository The attribute repository instance
168
     *
169
     * @return void
170
     */
171
    public function setAttributeRepository(AttributeRepository $attributeRepository)
172
    {
173
        $this->attributeRepository = $attributeRepository;
174
    }
175
176
    /**
177
     * Return's the attribute repository instance.
178
     *
179
     * @return \TechDivision\Import\Attribute\Repositories\AttributeRepository The attribute repository instance
180
     */
181
    public function getAttributeRepository()
182
    {
183
        return $this->attributeRepository;
184
    }
185
186
    /**
187
     * Set's the catalog attribute repository instance.
188
     *
189
     * @param \TechDivision\Import\Attribute\Repositories\CatalogAttributeRepository $catalogAttributeRepository The catalog attribute repository instance
190
     *
191
     * @return void
192
     */
193
    public function setCatalogAttributeRepository(CatalogAttributeRepository $catalogAttributeRepository)
194
    {
195
        $this->catalogAttributeRepository = $catalogAttributeRepository;
196
    }
197
198
    /**
199
     * Return's the catalog attribute repository instance.
200
     *
201
     * @return \TechDivision\Import\Attribute\Repositories\CatalogAttributeRepository The catalog attribute repository instance
202
     */
203
    public function getCatalogAttributeRepository()
204
    {
205
        return $this->catalogAttributeRepository;
206
    }
207
208
    /**
209
     * Set's the attribute action instance.
210
     *
211
     * @param \TechDivision\Import\Attribute\Actions\AttributeAction $attributeAction The attribute action instance
212
     *
213
     * @return void
214
     */
215
    public function setAttributeAction(AttributeAction $attributeAction)
216
    {
217
        $this->attributeAction = $attributeAction;
218
    }
219
220
    /**
221
     * Return's the attribute action instance.
222
     *
223
     * @return \TechDivision\Import\Attribute\Actions\AttributeAction The attribute action instance
224
     */
225
    public function getAttributeAction()
226
    {
227
        return $this->attributeAction;
228
    }
229
230
    /**
231
     * Set's the catalog attribute action instance.
232
     *
233
     * @param \TechDivision\Import\Attribute\Actions\CatalogAttributeAction $catalogAttributeAction The catalog attribute action instance
234
     *
235
     * @return void
236
     */
237
    public function setCatalogAttributeAction(CatalogAttributeAction $catalogAttributeAction)
238
    {
239
        $this->catalogAttributeAction = $catalogAttributeAction;
240
    }
241
242
    /**
243
     * Return's the catalog attribute action instance.
244
     *
245
     * @return \TechDivision\Import\Attribute\Actions\CatalogAttributeAction The catalog attribute action instance
246
     */
247
    public function getCatalogAttributeAction()
248
    {
249
        return $this->catalogAttributeAction;
250
    }
251
252
    /**
253
     * Load's and return's the EAV attribute with the passed code.
254
     *
255
     * @param string $attributeCode The code of the EAV attribute to load
256
     *
257
     * @return array The EAV attribute
258
     */
259
    public function loadAttributeByAttributeCode($attributeCode)
260
    {
261
        return $this->getAttributeRepository()->findOneByAttributeCode($attributeCode);
262
    }
263
264
    /**
265
     * Load's and retur's the EAV catalog attribute with the passed ID.
266
     *
267
     * @param string $attributeId The ID of the EAV catalog attribute to return
268
     *
269
     * @return array The EAV catalog attribute
270
     */
271
    public function loadCatalogAttribute($attributeId)
272
    {
273
        return $this->getCatalogAttributeRepository()->load($attributeId);
274
    }
275
276
    /**
277
     * Persist's the passed EAV attribute data and return's the ID.
278
     *
279
     * @param array       $attribute The attribute data to persist
280
     * @param string|null $name      The name of the prepared statement that has to be executed
281
     *
282
     * @return string The ID of the persisted attribute
283
     */
284
    public function persistAttribute(array $attribute, $name = null)
285
    {
286
        return $this->getAttributeAction()->persist($attribute, $name);
0 ignored issues
show
Unused Code introduced by
The call to AttributeAction::persist() has too many arguments starting with $name.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
287
    }
288
289
    /**
290
     * Persist's the passed EAV catalog attribute data and return's the ID.
291
     *
292
     * @param array       $catalogAttribute The catalog attribute data to persist
293
     * @param string|null $name             The name of the prepared statement that has to be executed
294
     *
295
     * @return void
296
     */
297
    public function persistCatalogAttribute(array $catalogAttribute, $name = null)
298
    {
299
        $this->getCatalogAttributeAction()->persist($catalogAttribute, $name);
0 ignored issues
show
Unused Code introduced by
The call to CatalogAttributeAction::persist() has too many arguments starting with $name.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
300
    }
301
302
    /**
303
     * Delete's the EAV attribute with the passed attributes.
304
     *
305
     * @param array       $row  The attributes of the EAV attribute to delete
306
     * @param string|null $name The name of the prepared statement that has to be executed
307
     *
308
     * @return void
309
     */
310
    public function deleteAttribute($row, $name = null)
311
    {
312
        $this->getAttributeAction()->delete($row, $name);
313
    }
314
}
315