Completed
Push — master ( 9e5593...4a1352 )
by Tim
12s
created

ProductLinkProcessor::loadProductLink()   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 3
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
use TechDivision\Import\Product\Link\Repositories\ProductLinkRepository;
24
use TechDivision\Import\Product\Link\Repositories\ProductLinkAttributeIntRepository;
25
use TechDivision\Import\Product\Link\Actions\ProductLinkAction;
26
use TechDivision\Import\Product\Link\Actions\ProductLinkAttributeIntAction;
27
28
/**
29
 * A SLSB providing methods to load product data using a PDO connection.
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-product-link
35
 * @link      http://www.techdivision.com
36
 */
37
class ProductLinkProcessor implements ProductLinkProcessorInterface
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 repository to load product links.
49
     *
50
     * @var \TechDivision\Import\Product\Link\Repositories\\ProductLinkRepository
51
     */
52
    protected $productLinkRepository;
53
54
    /**
55
     * The repository to load product link attribute integer attributes.
56
     *
57
     * @var \TechDivision\Import\Product\Link\Repositories\\ProductLinkAttributeIntRepository
58
     */
59
    protected $productLinkAttributeIntRepository;
60
61
    /**
62
     * The action with the product link CRUD methods.
63
     *
64
     * @var \TechDivision\Import\Product\Link\Actions\ProductLinkAction
65
     */
66
    protected $productLinkAction;
67
68
    /**
69
     * The action with the product link attribute integer CRUD methods.
70
     *
71
     * @var \TechDivision\Import\Product\Link\Actions\ProductLinkAttributeIntAction
72
     */
73
    protected $productLinkAttributeIntAction;
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\Product\Link\Repositories\\ProductLinkRepository             $productLinkRepository             The product link repository to use
80
     * @param \TechDivision\Import\Product\Link\Repositories\\ProductLinkAttributeIntRepository $productLinkAttributeIntRepository The product link attribute integer repository to use
81
     * @param \TechDivision\Import\Product\Link\Actions\ProductLinkAction                       $productLinkAction                 The product link action to use
82
     * @param \TechDivision\Import\Product\Link\Actions\ProductLinkAttributeIntAction           $productLinkAttributeIntAction     The product link attribute integer action to use
83
     */
84
    public function __construct(
85
        \PDO $connection,
86
        ProductLinkRepository $productLinkRepository,
87
        ProductLinkAttributeIntRepository $productLinkAttributeIntRepository,
88
        ProductLinkAction $productLinkAction,
89
        ProductLinkAttributeIntAction $productLinkAttributeIntAction
90
    ) {
91
        $this->setConnection($connection);
92
        $this->setProductLinkRepository($productLinkRepository);
93
        $this->setProductLinkAttributeIntRepository($productLinkAttributeIntRepository);
94
        $this->setProductLinkAction($productLinkAction);
95
        $this->setProductLinkAttributeIntAction($productLinkAttributeIntAction);
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 repository to load product links.
166
     *
167
     * @param \TechDivision\Import\Product\Link\Repositories\ProductLinkRepository $productLinkRepository The repository instance
168
     *
169
     * @return void
170
     */
171
    public function setProductLinkRepository($productLinkRepository)
172
    {
173
        $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...
174
    }
175
176
    /**
177
     * Return's the repository to load product links.
178
     *
179
     * @return \TechDivision\Import\Product\Link\Repositories\ProductLinkRepository The repository instance
180
     */
181
    public function getProductLinkRepository()
182
    {
183
        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...
184
    }
185
186
    /**
187
     * Set's the repository to load product link attribute integer attributes.
188
     *
189
     * @param \TechDivision\Import\Product\Link\Repositories\ProductLinkAttributeIntRepository $productLinkAttributeIntRepository The repository instance
190
     *
191
     * @return void
192
     */
193
    public function setProductLinkAttributeIntRepository($productLinkAttributeIntRepository)
194
    {
195
        $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...
196
    }
197
198
    /**
199
     * Return's the repository to load product link attribute integer attributes.
200
     *
201
     * @return \TechDivision\Import\Product\Link\Repositories\ProductLinkAttributeIntRepository The repository instance
202
     */
203
    public function getProductLinkAttributeIntRepository()
204
    {
205
        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...
206
    }
207
208
    /**
209
     * Set's the action with the product link CRUD methods.
210
     *
211
     * @param \TechDivision\Import\Product\Link\Actions\ProductLinkAction $productLinkAction The action with the product link CRUD methods
212
     *
213
     * @return void
214
     */
215
    public function setProductLinkAction($productLinkAction)
216
    {
217
        $this->productLinkAction = $productLinkAction;
218
    }
219
220
    /**
221
     * Return's the action with the product link CRUD methods.
222
     *
223
     * @return \TechDivision\Import\Product\Link\Actions\ProductLinkGalleryAction The action with the product link CRUD methods
224
     */
225
    public function getProductLinkAction()
226
    {
227
        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...
228
    }
229
230
    /**
231
     * Set's the action with the product link attribute integer CRUD methods.
232
     *
233
     * @param \TechDivision\Import\Product\Link\Actions\ProductLinkAttributeIntAction $productLinkAttributeIntAction The action with the product link attribute integer CRUD methods
234
     *
235
     * @return void
236
     */
237
    public function setProductLinkAttributeIntAction($productLinkAttributeIntAction)
238
    {
239
        $this->productLinkAttributeIntAction = $productLinkAttributeIntAction;
240
    }
241
242
    /**
243
     * Return's the action with the product link attribute integer CRUD methods.
244
     *
245
     * @return \TechDivision\Import\Product\Link\Actions\ProductLinkAttributeIntAction The action with the product link attribute integer CRUD methods
246
     */
247
    public function getProductLinkAttributeIntAction()
248
    {
249
        return $this->productLinkAttributeIntAction;
250
    }
251
252
    /**
253
     * Load's the link with the passed product/linked product/link type ID.
254
     *
255
     * @param integer $productId       The product ID of the link to load
256
     * @param integer $linkedProductId The linked product ID of the link to load
257
     * @param integer $linkTypeId      The link type ID of the product to load
258
     *
259
     * @return array The link
260
     */
261
    public function loadProductLink($productId, $linkedProductId, $linkTypeId)
262
    {
263
        return $this->getProductLinkRepository()->findOneByProductIdAndLinkedProductIdAndLinkTypeId($productId, $linkedProductId, $linkTypeId);
264
    }
265
266
    /**
267
     * Return's the product link attribute integer value with the passed product link attribute/link ID.
268
     *
269
     * @param integer $productLinkAttributeId The product link attribute ID of the attributes
270
     * @param integer $linkId                 The link ID of the attribute
271
     *
272
     * @return array The product link attribute integer value
273
     */
274
    public function loadProductLinkAttributeInt($productLinkAttributeId, $linkId)
275
    {
276
        return $this->getProductLinkAttributeIntRepository()->findOneByProductLinkAttributeIdAndLinkId($productLinkAttributeId, $linkId);
277
    }
278
279
    /**
280
     * Persist's the passed product link data and return's the ID.
281
     *
282
     * @param array $productLink The product link data to persist
283
     *
284
     * @return string The ID of the persisted entity
285
     */
286
    public function persistProductLink($productLink)
287
    {
288
        return $this->getProductLinkAction()->persist($productLink);
289
    }
290
291
    /**
292
     * Persist's the passed product link attribute integer data.
293
     *
294
     * @param array $productLinkAttributeInt The product link attribute integer data to persist
295
     *
296
     * @return string The ID of the persisted entity
297
     */
298
    public function persistProductLinkAttributeInt($productLinkAttributeInt)
299
    {
300
        $this->getProductLinkAttributeIntAction()->persist($productLinkAttributeInt);
301
    }
302
}
303