Passed
Pull Request — master (#1)
by Tim
03:10
created

loadAttributeGroupByAttributeSetNameAndAttributeGroupName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * TechDivision\Import\Attribute\Services\AttributeSetBunchProcessor
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 2019 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-set
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Attribute\Set\Services;
22
23
use TechDivision\Import\Connection\ConnectionInterface;
24
use TechDivision\Import\Repositories\EavAttributeSetRepositoryInterface;
25
use TechDivision\Import\Repositories\EavAttributeGroupRepositoryInterface;
26
use TechDivision\Import\Attribute\Set\Actions\EavAttributeSetActionInterface;
27
use TechDivision\Import\Attribute\Set\Actions\EavAttributeGroupActionInterface;
28
29
/**
30
 * The attribute set bunch processor implementation.
31
 *
32
 * @author    Tim Wagner <[email protected]>
33
 * @copyright 2019 TechDivision GmbH <[email protected]>
34
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
35
 * @link      https://github.com/techdivision/import-attribute-set
36
 * @link      http://www.techdivision.com
37
 */
38
class AttributeSetBunchProcessor implements AttributeSetBunchProcessorInterface
39
{
40
41
    /**
42
     * A connection to use.
43
     *
44
     * @var \TechDivision\Import\Connection\ConnectionInterface
45
     */
46
    protected $connection;
47
48
    /**
49
     * The EAV attribute set repository instance.
50
     *
51
     * @var \TechDivision\Import\Repositories\EavAttributeSetRepositoryInterface
52
     */
53
    protected $eavAttributeSetRepository;
54
55
    /**
56
     * The EAV attribute set repository instance.
57
     *
58
     * @var \TechDivision\Import\Repositories\EavAttributeGroupRepositoryInterface
59
     */
60
    protected $eavAttributeGroupRepository;
61
62
    /**
63
     * The attribute set action instance.
64
     *
65
     * @var \TechDivision\Import\Attribute\Set\Actions\EavAttributeSetActionInterface
66
     */
67
    protected $eavAttributeSetAction;
68
69
    /**
70
     * The attribute group action instance.
71
     *
72
     * @var \TechDivision\Import\Attribute\Set\Actions\EavAttributeGroupActionInterface
73
     */
74
    protected $eavAttributeGroupAction;
75
76
    /**
77
     * Initialize the processor with the necessary repository and action instances.
78
     *
79
     * @param \TechDivision\Import\Connection\ConnectionInterface                         $connection                  The connection to use
80
     * @param \TechDivision\Import\Repositories\EavAttributeSetRepositoryInterface        $eavAttributeSetRepository   The EAV attribute set repository instance
81
     * @param \TechDivision\Import\Repositories\EavAttributeGroupRepositoryInterface      $eavAttributeGroupRepository The EAV attribute group repository instance
82
     * @param \TechDivision\Import\Attribute\Set\Actions\EavAttributeSetActionInterface   $eavAttributeSetAction       The EAV attribute set action instance
83
     * @param \TechDivision\Import\Attribute\Set\Actions\EavAttributeGroupActionInterface $eavAttributeGroupAction     The EAV attribute gropu action instance
84
     */
85
    public function __construct(
86
        ConnectionInterface $connection,
87
        EavAttributeSetRepositoryInterface $eavAttributeSetRepository,
88
        EavAttributeGroupRepositoryInterface $eavAttributeGroupRepository,
89
        EavAttributeSetActionInterface $eavAttributeSetAction,
90
        EavAttributeGroupActionInterface $eavAttributeGroupAction
91
    ) {
92
        $this->setConnection($connection);
93
        $this->setEavAttributeSetRepository($eavAttributeSetRepository);
94
        $this->setEavAttributeGroupRepository($eavAttributeGroupRepository);
95
        $this->setEavAttributeSetAction($eavAttributeSetAction);
96
        $this->setEavAttributeGroupAction($eavAttributeGroupAction);
97
    }
98
99
    /**
100
     * Set's the passed connection.
101
     *
102
     * @param \TechDivision\Import\Connection\ConnectionInterface $connection The connection to set
103
     *
104
     * @return void
105
     */
106
    public function setConnection(ConnectionInterface $connection)
107
    {
108
        $this->connection = $connection;
109
    }
110
111
    /**
112
     * Return's the connection.
113
     *
114
     * @return \TechDivision\Import\Connection\ConnectionInterface The connection instance
115
     */
116
    public function getConnection()
117
    {
118
        return $this->connection;
119
    }
120
121
    /**
122
     * Turns off autocommit mode. While autocommit mode is turned off, changes made to the database via the PDO
123
     * object instance are not committed until you end the transaction by calling ProductProcessor::commit().
124
     * Calling ProductProcessor::rollBack() will roll back all changes to the database and return the connection
125
     * to autocommit mode.
126
     *
127
     * @return boolean Returns TRUE on success or FALSE on failure
128
     * @link http://php.net/manual/en/pdo.begintransaction.php
129
     */
130
    public function beginTransaction()
131
    {
132
        return $this->connection->beginTransaction();
133
    }
134
135
    /**
136
     * Commits a transaction, returning the database connection to autocommit mode until the next call to
137
     * ProductProcessor::beginTransaction() starts a new transaction.
138
     *
139
     * @return boolean Returns TRUE on success or FALSE on failure
140
     * @link http://php.net/manual/en/pdo.commit.php
141
     */
142
    public function commit()
143
    {
144
        return $this->connection->commit();
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->connection->commit() targeting TechDivision\Import\Conn...tionInterface::commit() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Bug Best Practice introduced by
The expression return $this->connection->commit() returns the type void which is incompatible with the documented return type boolean.
Loading history...
145
    }
146
147
    /**
148
     * Rolls back the current transaction, as initiated by ProductProcessor::beginTransaction().
149
     *
150
     * If the database was set to autocommit mode, this function will restore autocommit mode after it has
151
     * rolled back the transaction.
152
     *
153
     * Some databases, including MySQL, automatically issue an implicit COMMIT when a database definition
154
     * language (DDL) statement such as DROP TABLE or CREATE TABLE is issued within a transaction. The implicit
155
     * COMMIT will prevent you from rolling back any other changes within the transaction boundary.
156
     *
157
     * @return boolean Returns TRUE on success or FALSE on failure
158
     * @link http://php.net/manual/en/pdo.rollback.php
159
     */
160
    public function rollBack()
161
    {
162
        return $this->connection->rollBack();
163
    }
164
165
    /**
166
     * Set's the attribute set repository instance.
167
     *
168
     * @param \TechDivision\Import\Repositories\EavAttributeSetRepositoryInterface $eavAttributeSetRepository The attribute set repository instance
169
     *
170
     * @return void
171
     */
172
    public function setEavAttributeSetRepository(EavAttributeSetRepositoryInterface $eavAttributeSetRepository)
173
    {
174
        $this->eavAttributeSetRepository = $eavAttributeSetRepository;
175
    }
176
177
    /**
178
     * Return's the attribute set repository instance.
179
     *
180
     * @return \TechDivision\Import\Repositories\EavAttributeSetRepositoryInterface The attribute set repository instance
181
     */
182
    public function getEavAttributeSetRepository()
183
    {
184
        return $this->eavAttributeSetRepository;
185
    }
186
187
    /**
188
     * Set's the attribute group repository instance.
189
     *
190
     * @param \TechDivision\Import\Repositories\EavAttributeGroupRepositoryInterface $eavAttributeGroupRepository The attribute group repository instance
191
     *
192
     * @return void
193
     */
194
    public function setEavAttributeGroupRepository(EavAttributeGroupRepositoryInterface $eavAttributeGroupRepository)
195
    {
196
        $this->eavAttributeGroupRepository = $eavAttributeGroupRepository;
197
    }
198
199
    /**
200
     * Return's the attribute group repository instance.
201
     *
202
     * @return \TechDivision\Import\Repositories\EavAttributeGroupRepositoryInterface The attribute group repository instance
203
     */
204
    public function getEavAttributeGroupRepository()
205
    {
206
        return $this->eavAttributeGroupRepository;
207
    }
208
209
    /**
210
     * Set's the EAV attribute set action instance.
211
     *
212
     * @param \TechDivision\Import\Attribute\Set\Actions\EavAttributeSetActionInterface $eavAttributeSetAction The attribute set action instance
213
     *
214
     * @return void
215
     */
216
    public function setEavAttributeSetAction(EavAttributeSetActionInterface $eavAttributeSetAction)
217
    {
218
        $this->eavAttributeSetAction = $eavAttributeSetAction;
219
    }
220
221
    /**
222
     * Return's the attribute set action instance.
223
     *
224
     * @return \TechDivision\Import\Attribute\Set\Actions\EavAttributeSetActionInterface The attribute set action instance
225
     */
226
    public function getEavAttributeSetAction()
227
    {
228
        return $this->eavAttributeSetAction;
229
    }
230
231
    /**
232
     * Set's the EAV attribute group action instance.
233
     *
234
     * @param \TechDivision\Import\Attribute\Set\Actions\EavAttributeGroupActionInterface $eavAttributeGroupAction The attribute gropu action instance
235
     *
236
     * @return void
237
     */
238
    public function setEavAttributeGroupAction(EavAttributeGroupActionInterface $eavAttributeGroupAction)
239
    {
240
        $this->eavAttributeGroupAction = $eavAttributeGroupAction;
241
    }
242
243
    /**
244
     * Return's the attribute group action instance.
245
     *
246
     * @return \TechDivision\Import\Attribute\Set\Actions\EavAttributeGroupActionInterface The attribute group action instance
247
     */
248
    public function getEavAttributeGroupAction()
249
    {
250
        return $this->eavAttributeGroupAction;
251
    }
252
253
    /**
254
     * Load's and return's the EAV attribute set with the passed entity type code and attribute set name.
255
     *
256
     * @param string $entityTypeCode   The entity type code of the EAV attribute set to load
257
     * @param string $attributeSetName The attribute set name of the EAV attribute set to return
258
     *
259
     * @return array The EAV attribute set
260
     */
261
    public function loadAttributeSetByEntityTypeCodeAndAttributeSetName($entityTypeCode, $attributeSetName)
262
    {
263
        return $this->getEavAttributeSetRepository()->findOneByEntityTypeCodeAndAttributeSetName($entityTypeCode, $attributeSetName);
264
    }
265
266
    /**
267
     * Load's and return's the EAV attribute set with the passed entity type ID and attribute set name.
268
     *
269
     * @param string $entityTypeId     The entity type ID of the EAV attribute set to load
270
     * @param string $attributeSetName The attribute set name of the EAV attribute set to return
271
     *
272
     * @return array The EAV attribute set
273
     */
274
    public function loadAttributeSetByEntityTypeIdAndAttributeSetName($entityTypeId, $attributeSetName)
275
    {
276
        return $this->getEavAttributeSetRepository()->findOneByEntityTypeIdAndAttributeSetName($entityTypeId, $attributeSetName);
277
    }
278
279
    /**
280
     * Return's the EAV attribute group with the passed entity type code, attribute set and attribute group name.
281
     *
282
     * @param string $entityTypeCode     The entity type code of the EAV attribute group to return
283
     * @param string $attributeSetName   The attribute set name of the EAV attribute group to return
284
     * @param string $attributeGroupName The attribute group name of the EAV attribute group to return
285
     *
286
     * @return array The EAV attribute group
287
     */
288
    public function loadAttributeGroupByEntityTypeCodeAndAttributeSetNameAndAttributeGroupName($entityTypeCode, $attributeSetName, $attributeGroupName)
289
    {
290
        return $this->getEavAttributeGroupRepository()->findOneByEntityTypeCodeAndAttributeSetNameAndAttributeGroupName($entityTypeCode, $attributeSetName, $attributeGroupName);
291
    }
292
293
    /**
294
     * Persist's the passed EAV attribute set data and return's the ID.
295
     *
296
     * @param array       $attributeSet The attribute set data to persist
297
     * @param string|null $name         The name of the prepared statement that has to be executed
298
     *
299
     * @return string The ID of the persisted attribute set
300
     */
301
    public function persistAttributeSet(array $attributeSet, $name = null)
302
    {
303
        return $this->getEavAttributeSetAction()->persist($attributeSet);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getEavAttr...>persist($attributeSet) returns the type void which is incompatible with the documented return type string.
Loading history...
Bug introduced by
Are you sure the usage of $this->getEavAttributeSe...>persist($attributeSet) targeting TechDivision\Import\Acti...ionInterface::persist() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
304
    }
305
306
    /**
307
     * Persist the passed EAV attribute group.
308
     *
309
     * @param array       $attributeGroup The attribute group to persist
310
     * @param string|null $name           The name of the prepared statement that has to be executed
311
     *
312
     * @return void
313
     */
314
    public function persistAttributeGroup(array $attributeGroup, $name = null)
315
    {
316
        $this->getEavAttributeGroupAction()->persist($attributeGroup);
317
    }
318
319
    /**
320
     * Delete's the EAV attribute set with the passed attributes.
321
     *
322
     * @param array       $row  The attributes of the EAV attribute group to delete
323
     * @param string|null $name The name of the prepared statement that has to be executed
324
     *
325
     * @return void
326
     */
327
    public function deleteAttributeSet($row, $name = null)
328
    {
329
        $this->getEavAttributeSetAction()->delete($row, $name);
330
    }
331
}
332