Completed
Push — master ( b5c840...3d5461 )
by Marcus
04:46
created

findOneByAttributeCodeAndStoreIdAndValue()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 33

Duplication

Lines 33
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 33
loc 33
ccs 0
cts 21
cp 0
rs 9.392
c 0
b 0
f 0
cc 3
nc 3
nop 3
crap 12

1 Method

Rating   Name   Duplication   Size   Complexity  
A EavAttributeOptionValueRepository::findAllByEntityTypeIdAndStoreId() 17 17 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Repositories\EavAttributeOptionValueRepository
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
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Repositories;
22
23
use TechDivision\Import\Utils\CacheKeys;
24
use TechDivision\Import\Utils\MemberNames;
25
use TechDivision\Import\Utils\SqlStatementKeys;
26
use TechDivision\Import\Cache\CacheAdapterInterface;
27
use TechDivision\Import\Connection\ConnectionInterface;
28
29
/**
30
 * Cached repository implementation to load cached EAV attribute option value data.
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
36
 * @link      http://www.techdivision.com
37
 */
38
class EavAttributeOptionValueRepository extends AbstractRepository implements EavAttributeOptionValueRepositoryInterface
39
{
40
41
    /**
42
     * The cache adapter instance.
43
     *
44
     * @var \TechDivision\Import\Cache\CacheAdapterInterface
45
     */
46
    protected $cacheAdapter;
47
48
    /**
49
     * The prepared statement to load the existing EAV attribute option values.
50
     *
51
     * @var \PDOStatement
52
     */
53
    protected $eavAttributeOptionValuesStmt;
54
55
    /**
56
     * The prepared statement to load the existing EAV attribute option values by their entity type and store ID.
57
     *
58
     * @var \PDOStatement
59
     */
60
    protected $eavAttributeOptionValuesByEntityTypeIdAndStoreIdStmt;
61
62
    /**
63
     * The prepared statement to load an existing EAV attribute option value by its option id and store ID
64
     *
65
     * @var \PDOStatement
66
     */
67
    protected $eavAttributeOptionValueByOptionIdAndStoreIdStmt;
68
69
    /**
70
     * The prepared statement to load an existing EAV attribute option value by its attribute code, store ID and value.
71
     *
72
     * @var \PDOStatement
73
     */
74
    protected $eavAttributeOptionValueByAttributeCodeAndStoreIdAndValueStmt;
75
76
    /**
77
     * The prepared statement to load an existing EAV attribute option value by its entity type ID, attribute code, store ID and value.
78
     *
79
     * @var \PDOStatement
80
     */
81
    protected $eavAttributeOptionValueByEntityTypeIdAndAttributeCodeAndStoreIdAndValueStmt;
82
83
    /**
84
     * Initialize the repository with the passed connection and utility class name.
85
     * .
86
     * @param \TechDivision\Import\Connection\ConnectionInterface               $connection             The connection instance
87
     * @param \TechDivision\Import\Repositories\SqlStatementRepositoryInterface $sqlStatementRepository The SQL repository instance
88
     * @param \TechDivision\Import\Cache\CacheAdapterInterface                  $cacheAdapter           The cache adapter instance
89
     */
90
    public function __construct(
91
        ConnectionInterface $connection,
92
        SqlStatementRepositoryInterface $sqlStatementRepository,
93
        CacheAdapterInterface $cacheAdapter
94
    ) {
95
96
        // pass the connection the SQL statement repository to the parent class
97
        parent::__construct($connection, $sqlStatementRepository);
98
99
        // set the cache adapter instance
100
        $this->cacheAdapter = $cacheAdapter;
101
    }
102
103
    /**
104
     * Initializes the repository's prepared statements.
105
     *
106
     * @return void
107
     */
108
    public function init()
109
    {
110
111
        // initialize the prepared statements
112
        $this->eavAttributeOptionValuesStmt =
113
            $this->getConnection()->prepare($this->loadStatement(SqlStatementKeys::EAV_ATTRIBUTE_OPTION_VALUES));
114
        $this->eavAttributeOptionValuesByEntityTypeIdAndStoreIdStmt =
115
            $this->getConnection()->prepare($this->loadStatement(SqlStatementKeys::EAV_ATTRIBUTE_OPTION_VALUES_BY_ENTITY_TYPE_ID_AND_STORE_ID));
116
        $this->eavAttributeOptionValueByOptionIdAndStoreIdStmt =
117
            $this->getConnection()->prepare($this->loadStatement(SqlStatementKeys::EAV_ATTRIBUTE_OPTION_VALUE_BY_OPTION_ID_AND_STORE_ID));
118
        $this->eavAttributeOptionValueByAttributeCodeAndStoreIdAndValueStmt =
119
            $this->getConnection()->prepare($this->loadStatement(SqlStatementKeys::EAV_ATTRIBUTE_OPTION_VALUE_BY_ATTRIBUTE_CODE_AND_STORE_ID_AND_VALUE));
120
        $this->eavAttributeOptionValueByEntityTypeIdAndAttributeCodeAndStoreIdAndValueStmt =
121
            $this->getConnection()->prepare($this->loadStatement(SqlStatementKeys::EAV_ATTRIBUTE_OPTION_VALUE_BY_ENTITY_TYPE_ID_AND_ATTRIBUTE_CODE_AND_STORE_ID_AND_VALUE));
122
    }
123
124
    /**
125
     * Return's the primary key name of the entity.
126
     *
127
     * @return string The name of the entity's primary key
128
     */
129
    public function getPrimaryKeyName()
130
    {
131
        return MemberNames::VALUE_ID;
132
    }
133
134
    /**
135
     * Returns the cache adapter instance used to warm the repository.
136
     *
137
     * @return \TechDivision\Import\Cache\CacheAdapterInterface The repository's cache adapter instance
138
     */
139
    public function getCacheAdapter()
140
    {
141
        return $this->cacheAdapter;
142
    }
143
144
    /**
145
     * Load's and return's the available EAV attribute option values.
146
     *
147
     * @return array The EAV attribute option values
148
     */
149
    public function findAll()
150
    {
151
152
        // load and return all available EAV attribute option values
153
        $this->eavAttributeOptionValuesStmt->execute(array());
154
        return $this->eavAttributeOptionValuesStmt->fetchAll(\PDO::FETCH_ASSOC);
155
    }
156
157
    /**
158
     * Load's and return's the available EAV attribute option values by the passed entity type and store ID.
159
     *
160
     * @param integer $entityTypeId The entity type ID of the attribute option values to return
161
     * @param integer $storeId      The store ID of the attribute option values to return
162
     *
163
     * @return array The EAV attribute option values
164
     */
165 View Code Duplication
    public function findAllByEntityTypeIdAndStoreId($entityTypeId, $storeId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
166
    {
167
168
        // the parameters of the EAV attribute option to load
169
        $params = array(
170
            MemberNames::ENTITY_TYPE_ID => $entityTypeId,
171
            MemberNames::STORE_ID       => $storeId,
172
        );
173
174
        // load and return all available EAV attribute option values by the passed params
175
        $this->eavAttributeOptionValuesByEntityTypeIdAndStoreIdStmt->execute($params);
176
177
        // fetch the values and return them
178
        while ($record = $this->eavAttributeOptionValuesByEntityTypeIdAndStoreIdStmt->fetch(\PDO::FETCH_ASSOC)) {
179
            yield $record;
180
        }
181
    }
182
183
    /**
184
     * Load's and return's the EAV attribute option value with the passed option ID and store ID
185
     *
186
     * @param string  $optionId The option ID of the attribute option
187
     * @param integer $storeId  The store ID of the attribute option to load
188
     *
189
     * @return array The EAV attribute option value
190
     */
191
    public function findOneByOptionIdAndStoreId($optionId, $storeId)
192
    {
193
194
        // the parameters of the EAV attribute option to load
195
        $params = array(
196
            MemberNames::OPTION_ID => $optionId,
197
            MemberNames::STORE_ID  => $storeId,
198
        );
199
200
        // prepare the cache key
201
        $cacheKey = $this->cacheAdapter->cacheKey(array(SqlStatementKeys::EAV_ATTRIBUTE_OPTION_VALUE_BY_OPTION_ID_AND_STORE_ID => $params));
202
203
        // query whether or not the item has already been cached
204
        if ($this->cacheAdapter->isCached($cacheKey)) {
205
            return $this->cacheAdapter->fromCache($cacheKey);
206
        }
207
208
        // load the EAV attribute option value with the passed parameters
209
        $this->eavAttributeOptionValueByOptionIdAndStoreIdStmt->execute($params);
210
211
        // query whether or not the EAV attribute option value is available in the database
212 View Code Duplication
        if ($eavAttributeOptionValue = $this->eavAttributeOptionValueByOptionIdAndStoreIdStmt->fetch(\PDO::FETCH_ASSOC)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
213
            // prepare the unique cache key for the EAV attribute option value
214
            $uniqueKey = array(CacheKeys::EAV_ATTRIBUTE_OPTION_VALUE => $eavAttributeOptionValue[$this->getPrimaryKeyName()]);
215
            // add the EAV attribute option value to the cache, register the cache key reference as well
216
            $this->cacheAdapter->toCache($uniqueKey, $eavAttributeOptionValue, array($cacheKey => $uniqueKey));
0 ignored issues
show
Documentation introduced by
$uniqueKey is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
217
        }
218
219
        // finally, return it
220
        return $eavAttributeOptionValue;
221
    }
222
223
    /**
224
     * Load's and return's the EAV attribute option value with the passed entity type ID, code, store ID and value.
225
     *
226
     * @param string  $entityTypeId  The entity type ID of the EAV attribute to load the option value for
227
     * @param string  $attributeCode The code of the EAV attribute option to load
228
     * @param integer $storeId       The store ID of the attribute option to load
229
     * @param string  $value         The value of the attribute option to load
230
     *
231
     * @return array The EAV attribute option value
232
     */
233
    public function findOneByEntityTypeIdAndAttributeCodeAndStoreIdAndValue($entityTypeId, $attributeCode, $storeId, $value)
234
    {
235
236
        // the parameters of the EAV attribute option to load
237
        $params = array(
238
            MemberNames::ENTITY_TYPE_ID => $entityTypeId,
239
            MemberNames::ATTRIBUTE_CODE => $attributeCode,
240
            MemberNames::STORE_ID       => $storeId,
241
            MemberNames::VALUE          => $value
242
        );
243
244
        // prepare the cache key
245
        $cacheKey = $this->cacheAdapter->cacheKey(array(SqlStatementKeys::EAV_ATTRIBUTE_OPTION_VALUE_BY_ENTITY_TYPE_ID_AND_ATTRIBUTE_CODE_AND_STORE_ID_AND_VALUE => $params));
246
247
        // return the cached result if available
248
        if ($this->cacheAdapter->isCached($cacheKey)) {
249
            return $this->cacheAdapter->fromCache($cacheKey);
250
        }
251
252
        // load the EAV attribute option value with the passed parameters
253
        $this->eavAttributeOptionValueByEntityTypeIdAndAttributeCodeAndStoreIdAndValueStmt->execute($params);
254
255
        // query whether or not the result has been cached
256 View Code Duplication
        if ($eavAttributeOptionValue = $this->eavAttributeOptionValueByEntityTypeIdAndAttributeCodeAndStoreIdAndValueStmt->fetch(\PDO::FETCH_ASSOC)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
257
            // prepare the unique cache key for the EAV attribute option value
258
            $uniqueKey = array(CacheKeys::EAV_ATTRIBUTE_OPTION_VALUE => $eavAttributeOptionValue[$this->getPrimaryKeyName()]);
259
            // add the EAV attribute option value to the cache, register the cache key reference as well
260
            $this->cacheAdapter->toCache($uniqueKey, $eavAttributeOptionValue, array($cacheKey => $uniqueKey));
0 ignored issues
show
Documentation introduced by
$uniqueKey is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
261
        }
262
263
        // finally, return it
264
        return $eavAttributeOptionValue;
265
    }
266
}
267