Completed
Push — 9.x ( 6043ef...c82ffe )
by Tim
07:16
created

getCacheAdapter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

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
nc 1
nop 0
crap 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\MemberNames;
24
use TechDivision\Import\Utils\SqlStatementKeys;
25
use TechDivision\Import\Cache\CacheAdapterInterface;
26
use TechDivision\Import\Connection\ConnectionInterface;
27
28
/**
29
 * Cached repository implementation to load cached EAV attribute option value data.
30
 *
31
 * @author    Tim Wagner <[email protected]>
32
 * @copyright 2019 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
35
 * @link      http://www.techdivision.com
36
 */
37
class EavAttributeOptionValueRepository extends AbstractRepository implements EavAttributeOptionValueRepositoryInterface
38
{
39
40
    /**
41
     * The cache adapter instance.
42
     *
43
     * @var \TechDivision\Import\Cache\CacheAdapterInterface
44
     */
45
    protected $cacheAdapter;
46
47
    /**
48
     * The prepared statement to load the existing EAV attribute option values.
49
     *
50
     * @var \PDOStatement
51
     */
52
    protected $eavAttributeOptionValuesStmt;
53
54
    /**
55
     * The prepared statement to load an existing EAV attribute option value by its option id and store ID
56
     *
57
     * @var \PDOStatement
58
     */
59
    protected $eavAttributeOptionValueByOptionIdAndStoreIdStmt;
60
61
    /**
62
     * The prepared statement to load an existing EAV attribute option value by its attribute code, store ID and value.
63
     *
64
     * @var \PDOStatement
65
     */
66
    protected $eavAttributeOptionValueByAttributeCodeAndStoreIdAndValueStmt;
67
68
    /**
69
     * The prepared statement to load an existing EAV attribute option value by its entity type ID, attribute code, store ID and value.
70
     *
71
     * @var \PDOStatement
72
     */
73
    protected $eavAttributeOptionValueByEntityTypeIdAndAttributeCodeAndStoreIdAndValueStmt;
74
75
    /**
76
     * Initialize the repository with the passed connection and utility class name.
77
     * .
78
     * @param \TechDivision\Import\Connection\ConnectionInterface               $connection             The connection instance
79
     * @param \TechDivision\Import\Repositories\SqlStatementRepositoryInterface $sqlStatementRepository The SQL repository instance
80
     * @param \TechDivision\Import\Cache\CacheAdapterInterface                  $cacheAdapter           The cache adapter instance
81
     */
82
    public function __construct(
83
        ConnectionInterface $connection,
84
        SqlStatementRepositoryInterface $sqlStatementRepository,
85
        CacheAdapterInterface $cacheAdapter
86
    ) {
87
88
        // pass the connection the SQL statement repository to the parent class
89
        parent::__construct($connection, $sqlStatementRepository);
90
91
        // set the cache adapter instance
92
        $this->cacheAdapter = $cacheAdapter;
93
    }
94
95
    /**
96
     * Initializes the repository's prepared statements.
97
     *
98
     * @return void
99
     */
100 View Code Duplication
    public function init()
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...
101
    {
102
103
        // initialize the prepared statements
104
        $this->eavAttributeOptionValuesStmt =
105
            $this->getConnection()->prepare($this->loadStatement(SqlStatementKeys::EAV_ATTRIBUTE_OPTION_VALUES));
106
107
        // initialize the prepared statements
108
        $this->eavAttributeOptionValueByOptionIdAndStoreIdStmt =
109
            $this->getConnection()->prepare($this->loadStatement(SqlStatementKeys::EAV_ATTRIBUTE_OPTION_VALUE_BY_OPTION_ID_AND_STORE_ID));
110
111
        // initialize the prepared statements
112
        $this->eavAttributeOptionValueByAttributeCodeAndStoreIdAndValueStmt =
113
            $this->getConnection()->prepare($this->loadStatement(SqlStatementKeys::EAV_ATTRIBUTE_OPTION_VALUE_BY_ATTRIBUTE_CODE_AND_STORE_ID_AND_VALUE));
114
115
        // initialize the prepared statements
116
        $this->eavAttributeOptionValueByEntityTypeIdAndAttributeCodeAndStoreIdAndValueStmt =
117
            $this->getConnection()->prepare($this->loadStatement(SqlStatementKeys::EAV_ATTRIBUTE_OPTION_VALUE_BY_ENTITY_TYPE_ID_AND_ATTRIBUTE_CODE_AND_STORE_ID_AND_VALUE));
118
    }
119
120
    /**
121
     * Return's the primary key name of the entity.
122
     *
123
     * @return string The name of the entity's primary key
124
     */
125
    public function getPrimaryKeyName()
126
    {
127
        return MemberNames::VALUE_ID;
128
    }
129
130
    /**
131
     * Returns the cache adapter instance used to warm the repository.
132
     *
133
     * @return \TechDivision\Import\Cache\CacheAdapterInterface The repository's cache adapter instance
134
     */
135
    public function getCacheAdapter()
136
    {
137
        return $this->cacheAdapter;
138
    }
139
140
    /**
141
     * Load's and return's the available EAV attribute option values.
142
     *
143
     * @return array The EAV attribute option values
144
     */
145
    public function findAll()
146
    {
147
148
        // load and return all available EAV attribute option values
149
        $this->eavAttributeOptionValuesStmt->execute(array());
150
        return $this->eavAttributeOptionValuesStmt->fetchAll(\PDO::FETCH_ASSOC);
151
    }
152
153
    /**
154
     * Load's and return's the EAV attribute option value with the passed option ID and store ID
155
     *
156
     * @param string  $optionId The option ID of the attribute option
157
     * @param integer $storeId  The store ID of the attribute option to load
158
     *
159
     * @return array The EAV attribute option value
160
     */
161
    public function findOneByOptionIdAndStoreId($optionId, $storeId)
162
    {
163
164
        // the parameters of the EAV attribute option to load
165
        $params = array(
166
            MemberNames::OPTION_ID => $optionId,
167
            MemberNames::STORE_ID  => $storeId,
168
        );
169
170
        // prepare the cache key
171
        $cacheKey = $this->cacheAdapter->cacheKey(SqlStatementKeys::EAV_ATTRIBUTE_OPTION_VALUE_BY_OPTION_ID_AND_STORE_ID, $params);
172
173
        // query whether or not the item has already been cached
174
        if ($this->cacheAdapter->isCached($cacheKey)) {
175
            return $this->cacheAdapter->fromCache($cacheKey);
176
        }
177
178
        // load the EAV attribute option value with the passed parameters
179
        $this->eavAttributeOptionValueByOptionIdAndStoreIdStmt->execute($params);
180
181
        // query whether or not the EAV attribute option value is available in the database
182
        if ($eavAttributeOptionValue = $this->eavAttributeOptionValueByOptionIdAndStoreIdStmt->fetch(\PDO::FETCH_ASSOC)) {
183
            // prepare the unique cache key for the EAV attribute option value
184
            $uniqueKey = $this->cacheAdapter->cacheKey(
185
                EavAttributeOptionValueRepositoryInterface::class,
186
                array($eavAttributeOptionValue[$this->getPrimaryKeyName()])
187
                );
188
            // add the EAV attribute option value to the cache, register the cache key reference as well
189
            $this->cacheAdapter->toCache($cacheKey, $eavAttributeOptionValue, array($cacheKey => $uniqueKey));
190
        }
191
192
        // finally, return it
193
        return $eavAttributeOptionValue;
194
    }
195
196
    /**
197
     * Load's and return's the EAV attribute option value with the passed code, store ID and value.
198
     *
199
     * @param string  $attributeCode The code of the EAV attribute option to load
200
     * @param integer $storeId       The store ID of the attribute option to load
201
     * @param string  $value         The value of the attribute option to load
202
     *
203
     * @return array The EAV attribute option value
204
     * @deprecated Since 2.0.2, because multiple attributes with the same attribute code, but differenct entity type code can be available
205
     * @see \TechDivision\Import\Repositories\EavAttributeOptionValueRepositoryInterface::findOneByEntityTypeIdAndAttributeCodeAndStoreIdAndValue()
206
     */
207 View Code Duplication
    public function findOneByAttributeCodeAndStoreIdAndValue($attributeCode, $storeId, $value)
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...
208
    {
209
210
        // the parameters of the EAV attribute option to load
211
        $params = array(
212
            MemberNames::ATTRIBUTE_CODE => $attributeCode,
213
            MemberNames::STORE_ID       => $storeId,
214
            MemberNames::VALUE          => $value
215
        );
216
217
        // prepare the cache key
218
        $cacheKey = $this->cacheAdapter->cacheKey(SqlStatementKeys::EAV_ATTRIBUTE_OPTION_VALUE_BY_ATTRIBUTE_CODE_AND_STORE_ID_AND_VALUE, $params);
219
220
        // return the cached result if available
221
        if ($this->cacheAdapter->isCached($cacheKey)) {
222
            return $this->cacheAdapter->fromCache($cacheKey);
223
        }
224
225
        // load the EAV attribute option value with the passed parameters
226
        $this->eavAttributeOptionValueByAttributeCodeAndStoreIdAndValueStmt->execute($params);
227
228
        // query whether or not the result has been cached
229
        if ($eavAttributeOptionValue = $this->eavAttributeOptionValueByAttributeCodeAndStoreIdAndValueStmt->fetch(\PDO::FETCH_ASSOC)) {
230
            // prepare the unique cache key for the EAV attribute option value
231
            $uniqueKey = $this->cacheAdapter->cacheKey(
232
                EavAttributeOptionValueRepositoryInterface::class,
233
                array($eavAttributeOptionValue[$this->getPrimaryKeyName()])
234
                );
235
            // add the EAV attribute option value to the cache, register the cache key reference as well
236
            $this->cacheAdapter->toCache($cacheKey, $eavAttributeOptionValue, array($cacheKey => $uniqueKey));
237
        }
238
239
        // finally, return it
240
        return $eavAttributeOptionValue;
241
    }
242
243
    /**
244
     * Load's and return's the EAV attribute option value with the passed entity type ID, code, store ID and value.
245
     *
246
     * @param string  $entityTypeId  The entity type ID of the EAV attribute to load the option value for
247
     * @param string  $attributeCode The code of the EAV attribute option to load
248
     * @param integer $storeId       The store ID of the attribute option to load
249
     * @param string  $value         The value of the attribute option to load
250
     *
251
     * @return array The EAV attribute option value
252
     */
253 View Code Duplication
    public function findOneByEntityTypeIdAndAttributeCodeAndStoreIdAndValue($entityTypeId, $attributeCode, $storeId, $value)
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...
254
    {
255
256
        // the parameters of the EAV attribute option to load
257
        $params = array(
258
            MemberNames::ENTITY_TYPE_ID => $entityTypeId,
259
            MemberNames::ATTRIBUTE_CODE => $attributeCode,
260
            MemberNames::STORE_ID       => $storeId,
261
            MemberNames::VALUE          => $value
262
        );
263
264
        // prepare the cache key
265
        $cacheKey = $this->cacheAdapter->cacheKey(SqlStatementKeys::EAV_ATTRIBUTE_OPTION_VALUE_BY_ENTITY_TYPE_ID_AND_ATTRIBUTE_CODE_AND_STORE_ID_AND_VALUE, $params);
266
267
        // return the cached result if available
268
        if ($this->cacheAdapter->isCached($cacheKey)) {
269
            return $this->cacheAdapter->fromCache($cacheKey);
270
        }
271
272
        // load the EAV attribute option value with the passed parameters
273
        $this->eavAttributeOptionValueByEntityTypeIdAndAttributeCodeAndStoreIdAndValueStmt->execute($params);
274
275
        // query whether or not the result has been cached
276
        if ($eavAttributeOptionValue = $this->eavAttributeOptionValueByEntityTypeIdAndAttributeCodeAndStoreIdAndValueStmt->fetch(\PDO::FETCH_ASSOC)) {
277
            // prepare the unique cache key for the EAV attribute option value
278
            $uniqueKey = $this->cacheAdapter->cacheKey(
279
                EavAttributeOptionValueRepositoryInterface::class,
280
                array($eavAttributeOptionValue[$this->getPrimaryKeyName()])
281
                );
282
            // add the EAV attribute option value to the cache, register the cache key reference as well
283
            $this->cacheAdapter->toCache($cacheKey, $eavAttributeOptionValue, array($cacheKey => $uniqueKey));
284
        }
285
286
        // finally, return it
287
        return $eavAttributeOptionValue;
288
    }
289
}
290