Passed
Push — PAC-894 ( 56c856 )
by
unknown
29:39 queued 25:27
created

ProductGroupedProcessor::deleteProductRelation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\Grouped\Services\ProductGroupedProcessor
5
 *
6
 * PHP version 7
7
 *
8
 * @author    Tim Wagner <[email protected]>
9
 * @copyright 2019 TechDivision GmbH <[email protected]>
10
 * @license   https://opensource.org/licenses/MIT
11
 * @link      https://github.com/techdivision/import-product-grouped
12
 * @link      http://www.techdivision.com
13
 */
14
15
namespace TechDivision\Import\Product\Grouped\Services;
16
17
use TechDivision\Import\Dbal\Actions\ActionInterface;
18
use TechDivision\Import\Dbal\Connection\ConnectionInterface;
19
use TechDivision\Import\Product\Repositories\ProductRelationRepositoryInterface;
20
21
/**
22
 * The product link processor implementation.
23
 *
24
 * @author    Tim Wagner <[email protected]>
25
 * @copyright 2019 TechDivision GmbH <[email protected]>
26
 * @license   https://opensource.org/licenses/MIT
27
 * @link      https://github.com/techdivision/import-product-grouped
28
 * @link      http://www.techdivision.com
29
 */
30
class ProductGroupedProcessor implements ProductGroupedProcessorInterface
31
{
32
33
    /**
34
     * A PDO connection initialized with the values from the Doctrine EntityManager.
35
     *
36
     * @var \TechDivision\Import\Dbal\Connection\ConnectionInterface
37
     */
38
    protected $connection;
39
40
    /**
41
     * The repository to access product relations.
42
     *
43
     * @var \TechDivision\Import\Product\Repositories\ProductRelationRepositoryInterface
44
     */
45
    protected $productRelationRepository;
46
47
    /**
48
     * The action for product relation CRUD methods.
49
     *
50
     * @var \TechDivision\Import\Dbal\Actions\ActionInterface
51
     */
52
    protected $productRelationAction;
53
54
    /**
55
     * Initialize the processor with the necessary assembler and repository instances.
56
     *
57
     * @param \TechDivision\Import\Dbal\Connection\ConnectionInterface                     $connection                The connection to use
58
     * @param \TechDivision\Import\Product\Repositories\ProductRelationRepositoryInterface $productRelationRepository The product relation repository to use
59
     * @param \TechDivision\Import\Dbal\Actions\ActionInterface                            $productRelationAction     The product relation action to use
60
     */
61
    public function __construct(
62
        ConnectionInterface $connection,
63
        ProductRelationRepositoryInterface $productRelationRepository,
64
        ActionInterface $productRelationAction
65
    ) {
66
        $this->setConnection($connection);
67
        $this->setProductRelationRepository($productRelationRepository);
68
        $this->setProductRelationAction($productRelationAction);
69
    }
70
71
    /**
72
     * Set's the passed connection.
73
     *
74
     * @param \TechDivision\Import\Dbal\Connection\ConnectionInterface $connection The connection to set
75
     *
76
     * @return void
77
     */
78
    public function setConnection(ConnectionInterface $connection)
79
    {
80
        $this->connection = $connection;
81
    }
82
83
    /**
84
     * Return's the connection.
85
     *
86
     * @return \TechDivision\Import\Dbal\Connection\ConnectionInterface The connection instance
87
     */
88
    public function getConnection()
89
    {
90
        return $this->connection;
91
    }
92
93
    /**
94
     * Turns off autocommit mode. While autocommit mode is turned off, changes made to the database via the PDO
95
     * object instance are not committed until you end the transaction by calling ProductProcessor::commit().
96
     * Calling ProductProcessor::rollBack() will roll back all changes to the database and return the connection
97
     * to autocommit mode.
98
     *
99
     * @return boolean Returns TRUE on success or FALSE on failure
100
     * @link http://php.net/manual/en/pdo.begintransaction.php
101
     */
102
    public function beginTransaction()
103
    {
104
        return $this->connection->beginTransaction();
105
    }
106
107
    /**
108
     * Commits a transaction, returning the database connection to autocommit mode until the next call to
109
     * ProductProcessor::beginTransaction() starts a new transaction.
110
     *
111
     * @return boolean Returns TRUE on success or FALSE on failure
112
     * @link http://php.net/manual/en/pdo.commit.php
113
     */
114
    public function commit()
115
    {
116
        return $this->connection->commit();
0 ignored issues
show
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...
Bug introduced by
Are you sure the usage of $this->connection->commit() targeting TechDivision\Import\Dbal...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...
117
    }
118
119
    /**
120
     * Rolls back the current transaction, as initiated by ProductProcessor::beginTransaction().
121
     *
122
     * If the database was set to autocommit mode, this function will restore autocommit mode after it has
123
     * rolled back the transaction.
124
     *
125
     * Some databases, including MySQL, automatically issue an implicit COMMIT when a database definition
126
     * language (DDL) statement such as DROP TABLE or CREATE TABLE is issued within a transaction. The implicit
127
     * COMMIT will prevent you from rolling back any other changes within the transaction boundary.
128
     *
129
     * @return boolean Returns TRUE on success or FALSE on failure
130
     * @link http://php.net/manual/en/pdo.rollback.php
131
     */
132
    public function rollBack()
133
    {
134
        return $this->connection->rollBack();
135
    }
136
137
    /**
138
     * Set's the repository to access product relations.
139
     *
140
     * @param \TechDivision\Import\Product\Repositories\ProductRelationRepositoryInterface $productRelationRepository The repository instance
141
     *
142
     * @return void
143
     */
144
    public function setProductRelationRepository(ProductRelationRepositoryInterface $productRelationRepository)
145
    {
146
        $this->productRelationRepository = $productRelationRepository;
147
    }
148
149
    /**
150
     * Return's the repository to access product relations.
151
     *
152
     * @return \TechDivision\Import\Product\Repositories\ProductRelationRepositoryInterface The repository instance
153
     */
154
    public function getProductRelationRepository()
155
    {
156
        return $this->productRelationRepository;
157
    }
158
159
    /**
160
     * Set's the action with the product relation CRUD methods.
161
     *
162
     * @param \TechDivision\Import\Dbal\Actions\ActionInterface $productRelationAction The action with the product relation CRUD methods
163
     *
164
     * @return void
165
     */
166
    public function setProductRelationAction(ActionInterface $productRelationAction)
167
    {
168
        $this->productRelationAction = $productRelationAction;
169
    }
170
171
    /**
172
     * Return's the action with the product relation CRUD methods.
173
     *
174
     * @return \TechDivision\Import\Dbal\Actions\ActionInterface The action instance
175
     */
176
    public function getProductRelationAction()
177
    {
178
        return $this->productRelationAction;
179
    }
180
181
    /**
182
     * Load's the product relation with the passed parent/child ID.
183
     *
184
     * @param integer $parentId The entity ID of the product relation's parent product
185
     * @param integer $childId  The entity ID of the product relation's child product
186
     *
187
     * @return array The product relation
188
     */
189
    public function loadProductRelation($parentId, $childId)
190
    {
191
        return $this->getProductRelationRepository()->findOneByParentIdAndChildId($parentId, $childId);
192
    }
193
194
    /**
195
     * Persist's the passed product relation data and return's the ID.
196
     *
197
     * @param array       $productRelation The product relation data to persist
198
     * @param string|null $name            The name of the prepared statement that has to be executed
199
     *
200
     * @return void
201
     */
202
    public function persistProductRelation($productRelation, $name = null)
203
    {
204
        return $this->getProductRelationAction()->persist($productRelation, $name);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getProductRelatio...productRelation, $name) targeting TechDivision\Import\Dbal...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...
Unused Code introduced by
The call to TechDivision\Import\Dbal...ionInterface::persist() has too many arguments starting with $name. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

204
        return $this->getProductRelationAction()->/** @scrutinizer ignore-call */ persist($productRelation, $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. Please note the @ignore annotation hint above.

Loading history...
205
    }
206
207
    /**
208
     * Deletes the passed product relation data.
209
     *
210
     * @param array $row The product relation to be deleted
211
     * @param string|null $name The name of the prepared statement that has to be executed
212
     *
213
     * @return void
214
     */
215
    public function deleteProductRelation(array $row, string $name = null): void
216
    {
217
        $this->getProductRelationAction()->delete($row, $name);
0 ignored issues
show
Unused Code introduced by
The call to TechDivision\Import\Dbal...tionInterface::delete() has too many arguments starting with $name. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

217
        $this->getProductRelationAction()->/** @scrutinizer ignore-call */ delete($row, $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. Please note the @ignore annotation hint above.

Loading history...
218
    }
219
}
220