Completed
Pull Request — master (#5)
by Tim
04:05
created

ProductLinkProcessor::commit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\Link\Services\ProductLinkProcessor
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-product-link
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Product\Link\Services;
22
23
/**
24
 * A SLSB providing methods to load product data using a PDO connection.
25
 *
26
 * @author    Tim Wagner <[email protected]>
27
 * @copyright 2016 TechDivision GmbH <[email protected]>
28
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
29
 * @link      https://github.com/techdivision/import-product-link
30
 * @link      http://www.techdivision.com
31
 */
32
class ProductLinkProcessor implements ProductLinkProcessorInterface
33
{
34
35
    /**
36
     * A PDO connection initialized with the values from the Doctrine EntityManager.
37
     *
38
     * @var \PDO
39
     */
40
    protected $connection;
41
42
    /**
43
     * The repository to load product links.
44
     *
45
     * @var \TechDivision\Import\Product\Link\Repositories\\ProductLinkRepository
46
     */
47
    protected $productLinkRepository;
48
49
    /**
50
     * The repository to load product link attribute integer attributes.
51
     *
52
     * @var \TechDivision\Import\Product\Link\Repositories\\ProductLinkAttributeIntRepository
53
     */
54
    protected $productLinkAttributeIntRepository;
55
56
    /**
57
     * The action with the product link CRUD methods.
58
     *
59
     * @var \TechDivision\Import\Product\Link\Actions\ProductLinkAction
60
     */
61
    protected $productLinkAction;
62
63
    /**
64
     * The action with the product link attribute CRUD methods.
65
     *
66
     * @var \TechDivision\Import\Product\Link\Actions\ProductLinkAttributeAction
67
     */
68
    protected $productLinkAttributeAction;
69
70
    /**
71
     * The action with the product link attribute decimal CRUD methods.
72
     *
73
     * @var \TechDivision\Import\Product\Link\Actions\ProductLinkAttributeDecimalAction
74
     */
75
    protected $productLinkAttributeDecimalAction;
76
77
    /**
78
     * The action with the product link attribute integer CRUD methods.
79
     *
80
     * @var \TechDivision\Import\Product\Link\Actions\ProductLinkAttributeIntAction
81
     */
82
    protected $productLinkAttributeIntAction;
83
84
    /**
85
     * The action with the product link attribute varchar CRUD methods.
86
     *
87
     * @var \TechDivision\Import\Product\Link\Actions\ProductLinkAttributeVarcharAction
88
     */
89
    protected $productLinkAttributeVarcharAction;
90
91
    /**
92
     * Set's the passed connection.
93
     *
94
     * @param \PDO $connection The connection to set
95
     *
96
     * @return void
97
     */
98
    public function setConnection(\PDO $connection)
99
    {
100
        $this->connection = $connection;
101
    }
102
103
    /**
104
     * Return's the connection.
105
     *
106
     * @return \PDO The connection instance
107
     */
108
    public function getConnection()
109
    {
110
        return $this->connection;
111
    }
112
113
    /**
114
     * Turns off autocommit mode. While autocommit mode is turned off, changes made to the database via the PDO
115
     * object instance are not committed until you end the transaction by calling ProductProcessor::commit().
116
     * Calling ProductProcessor::rollBack() will roll back all changes to the database and return the connection
117
     * to autocommit mode.
118
     *
119
     * @return boolean Returns TRUE on success or FALSE on failure
120
     * @link http://php.net/manual/en/pdo.begintransaction.php
121
     */
122
    public function beginTransaction()
123
    {
124
        return $this->connection->beginTransaction();
125
    }
126
127
    /**
128
     * Commits a transaction, returning the database connection to autocommit mode until the next call to
129
     * ProductProcessor::beginTransaction() starts a new transaction.
130
     *
131
     * @return boolean Returns TRUE on success or FALSE on failure
132
     * @link http://php.net/manual/en/pdo.commit.php
133
     */
134
    public function commit()
135
    {
136
        return $this->connection->commit();
137
    }
138
139
    /**
140
     * Rolls back the current transaction, as initiated by ProductProcessor::beginTransaction().
141
     *
142
     * If the database was set to autocommit mode, this function will restore autocommit mode after it has
143
     * rolled back the transaction.
144
     *
145
     * Some databases, including MySQL, automatically issue an implicit COMMIT when a database definition
146
     * language (DDL) statement such as DROP TABLE or CREATE TABLE is issued within a transaction. The implicit
147
     * COMMIT will prevent you from rolling back any other changes within the transaction boundary.
148
     *
149
     * @return boolean Returns TRUE on success or FALSE on failure
150
     * @link http://php.net/manual/en/pdo.rollback.php
151
     */
152
    public function rollBack()
153
    {
154
        return $this->connection->rollBack();
155
    }
156
157
    /**
158
     * Set's the repository to load product links.
159
     *
160
     * @param \TechDivision\Import\Product\Link\Repositories\ProductLinkRepository $productLinkRepository The repository instance
161
     *
162
     * @return void
163
     */
164
    public function setProductLinkRepository($productLinkRepository)
165
    {
166
        $this->productLinkRepository = $productLinkRepository;
0 ignored issues
show
Documentation Bug introduced by
It seems like $productLinkRepository of type object<TechDivision\Impo...\ProductLinkRepository> is incompatible with the declared type object<TechDivision\Impo...\ProductLinkRepository> of property $productLinkRepository.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
167
    }
168
169
    /**
170
     * Return's the repository to load product links.
171
     *
172
     * @return \TechDivision\Import\Product\Link\Repositories\ProductLinkRepository The repository instance
173
     */
174
    public function getProductLinkRepository()
175
    {
176
        return $this->productLinkRepository;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->productLinkRepository; (TechDivision\Import\Prod...\\ProductLinkRepository) is incompatible with the return type declared by the interface TechDivision\Import\Prod...etProductLinkRepository of type TechDivision\Import\Prod...s\ProductLinkRepository.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
177
    }
178
179
    /**
180
     * Set's the repository to load product link attribute integer attributes.
181
     *
182
     * @param \TechDivision\Import\Product\Link\Repositories\ProductLinkAttributeIntRepository $productLinkAttributeIntRepository The repository instance
183
     *
184
     * @return void
185
     */
186
    public function setProductLinkAttributeIntRepository($productLinkAttributeIntRepository)
187
    {
188
        $this->productLinkAttributeIntRepository = $productLinkAttributeIntRepository;
0 ignored issues
show
Documentation Bug introduced by
It seems like $productLinkAttributeIntRepository of type object<TechDivision\Impo...AttributeIntRepository> is incompatible with the declared type object<TechDivision\Impo...AttributeIntRepository> of property $productLinkAttributeIntRepository.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
189
    }
190
191
    /**
192
     * Return's the repository to load product link attribute integer attributes.
193
     *
194
     * @return \TechDivision\Import\Product\Link\Repositories\ProductLinkAttributeIntRepository The repository instance
195
     */
196
    public function getProductLinkAttributeIntRepository()
197
    {
198
        return $this->productLinkAttributeIntRepository;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->productLinkAttributeIntRepository; (TechDivision\Import\Prod...kAttributeIntRepository) is incompatible with the return type declared by the interface TechDivision\Import\Prod...kAttributeIntRepository of type TechDivision\Import\Prod...kAttributeIntRepository.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
199
    }
200
201
    /**
202
     * Set's the action with the product link CRUD methods.
203
     *
204
     * @param \TechDivision\Import\Product\Link\Actions\ProductLinkAction $productLinkAction The action with the product link CRUD methods
205
     *
206
     * @return void
207
     */
208
    public function setProductLinkAction($productLinkAction)
209
    {
210
        $this->productLinkAction = $productLinkAction;
211
    }
212
213
    /**
214
     * Return's the action with the product link CRUD methods.
215
     *
216
     * @return \TechDivision\Import\Product\Link\Actions\ProductLinkGalleryAction The action with the product link CRUD methods
217
     */
218
    public function getProductLinkAction()
219
    {
220
        return $this->productLinkAction;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->productLinkAction; (TechDivision\Import\Prod...tions\ProductLinkAction) is incompatible with the return type declared by the interface TechDivision\Import\Prod...e::getProductLinkAction of type TechDivision\Import\Prod...roductLinkGalleryAction.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
221
    }
222
223
    /**
224
     * Set's the action with the product link attribute CRUD methods.
225
     *
226
     * @param \TechDivision\Import\Product\Link\Actions\ProductLinkAttributeAction $productLinkAttributeAction The action with the product link attribute CRUD methods
227
     *
228
     * @return void
229
     */
230
    public function setProductLinkAttributeAction($productLinkAttributeAction)
231
    {
232
        $this->productLinkAttributeAction = $productLinkAttributeAction;
233
    }
234
235
    /**
236
     * Return's the action with the product link attribute CRUD methods.
237
     *
238
     * @return \TechDivision\Import\Product\Link\Actions\ProductLinkAttributeAction The action with the product link attribute CRUD methods
239
     */
240
    public function getProductLinkAttributeAction()
241
    {
242
        return $this->productLinkAttributeAction;
243
    }
244
245
    /**
246
     * Set's the action with the product link attribute decimal CRUD methods.
247
     *
248
     * @param \TechDivision\Import\Product\Link\Actions\ProductLinkAttributeDecimalAction $productLinkAttributeDecimalAction The action with the product link attribute decimal CRUD methods
249
     *
250
     * @return void
251
     */
252
    public function setProductLinkAttributeDecimalAction($productLinkAttributeDecimalAction)
253
    {
254
        $this->productLinkAttributeDecimalAction = $productLinkAttributeDecimalAction;
255
    }
256
257
    /**
258
     * Return's the action with the product link attribute decimal CRUD methods.
259
     *
260
     * @return \TechDivision\Import\Product\Link\Actions\ProductLinkAttributeDecimalAction The action with the product link attribute decimal CRUD methods
261
     */
262
    public function getProductLinkAttributeDecimalAction()
263
    {
264
        return $this->productLinkAttributeDecimalAction;
265
    }
266
267
    /**
268
     * Set's the action with the product link attribute integer CRUD methods.
269
     *
270
     * @param \TechDivision\Import\Product\Link\Actions\ProductLinkAttributeIntAction $productLinkAttributeIntAction The action with the product link attribute integer CRUD methods
271
     *
272
     * @return void
273
     */
274
    public function setProductLinkAttributeIntAction($productLinkAttributeIntAction)
275
    {
276
        $this->productLinkAttributeIntAction = $productLinkAttributeIntAction;
277
    }
278
279
    /**
280
     * Return's the action with the product link attribute integer CRUD methods.
281
     *
282
     * @return \TechDivision\Import\Product\Link\Actions\ProductLinkAttributeIntAction The action with the product link attribute integer CRUD methods
283
     */
284
    public function getProductLinkAttributeIntAction()
285
    {
286
        return $this->productLinkAttributeIntAction;
287
    }
288
289
    /**
290
     * Set's the action with the product link attribute varchar CRUD methods.
291
     *
292
     * @param \TechDivision\Import\Product\Link\Actions\ProductLinkAttributeVarcharAction $productLinkAttributeVarcharAction The action with the product link attribute varchar CRUD methods
293
     *
294
     * @return void
295
     */
296
    public function setProductLinkAttributeVarcharAction($productLinkAttributeVarcharAction)
297
    {
298
        $this->productLinkAttributeVarcharAction = $productLinkAttributeVarcharAction;
299
    }
300
301
    /**
302
     * Return's the action with the product link attribute varchar CRUD methods.
303
     *
304
     * @return \TechDivision\Import\Product\Link\Actions\ProductLinkAttributeVarcharAction The action with the product link attribute varchar CRUD methods
305
     */
306
    public function getProductLinkAttributeVarcharAction()
307
    {
308
        return $this->productLinkAttributeVarcharAction;
309
    }
310
311
    /**
312
     * Load's the link with the passed product/linked product/link type ID.
313
     *
314
     * @param integer $productId       The product ID of the link to load
315
     * @param integer $linkedProductId The linked product ID of the link to load
316
     * @param integer $linkTypeId      The link type ID of the product to load
317
     *
318
     * @return array The link
319
     */
320
    public function loadProductLink($productId, $linkedProductId, $linkTypeId)
321
    {
322
        return $this->getProductLinkRepository()->findOneByProductIdAndLinkedProductIdAndLinkTypeId($productId, $linkedProductId, $linkTypeId);
323
    }
324
325
    /**
326
     * Return's the product link attribute integer value with the passed product link attribute/link ID.
327
     *
328
     * @param integer $productLinkAttributeId The product link attribute ID of the attributes
329
     * @param integer $linkId                 The link ID of the attribute
330
     *
331
     * @return array The product link attribute integer value
332
     */
333
    public function loadProductLinkAttributeInt($productLinkAttributeId, $linkId)
334
    {
335
        return $this->getProductLinkAttributeIntRepository()->findOneByProductLinkAttributeIdAndLinkId($productLinkAttributeId, $linkId);
336
    }
337
338
    /**
339
     * Persist's the passed product link data and return's the ID.
340
     *
341
     * @param array $productLink The product link data to persist
342
     *
343
     * @return string The ID of the persisted entity
344
     */
345
    public function persistProductLink($productLink)
346
    {
347
        return $this->getProductLinkAction()->persist($productLink);
348
    }
349
350
    /**
351
     * Persist's the passed product link attribute data and return's the ID.
352
     *
353
     * @param array $productLinkAttribute The product link attribute data to persist
354
     *
355
     * @return string The ID of the persisted entity
356
     */
357
    public function persistProductLinkAttribute($productLinkAttribute)
358
    {
359
        return $this->getProductLinkAttributeAction()->persist($productLinkAttribute);
360
    }
361
362
    /**
363
     * Persist's the passed product link attribute decimal data.
364
     *
365
     * @param array $productLinkAttributeDecimal The product link attribute decimal data to persist
366
     *
367
     * @return void
368
     */
369
    public function persistProductLinkAttributeDecimal($productLinkAttributeDecimal)
370
    {
371
        $this->getProductLinkAttributeDecimalAction()->persist($productLinkAttributeDecimal);
372
    }
373
374
    /**
375
     * Persist's the passed product link attribute integer data.
376
     *
377
     * @param array $productLinkAttributeInt The product link attribute integer data to persist
378
     *
379
     * @return string The ID of the persisted entity
380
     */
381
    public function persistProductLinkAttributeInt($productLinkAttributeInt)
382
    {
383
        $this->getProductLinkAttributeIntAction()->persist($productLinkAttributeInt);
384
    }
385
386
    /**
387
     * Persist's the passed product link attribute varchar data.
388
     *
389
     * @param array $productLinkAttributeVarchar The product link attribute varchar data to persist
390
     *
391
     * @return string The ID of the persisted entity
392
     */
393
    public function persistProductLinkAttributeVarchar($productLinkAttributeVarchar)
394
    {
395
        $this->getProductLinkAttributeVarcharAction()->persist($productLinkAttributeVarchar);
396
    }
397
}
398