Completed
Push — 15.x ( 5c7040...0f4675 )
by Tim
03:03
created

  A

Complexity

Conditions 3
Paths 3

Size

Total Lines 32

Duplication

Lines 32
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 32
loc 32
c 0
b 0
f 0
ccs 0
cts 18
cp 0
rs 9.408
cc 3
nc 3
nop 3
crap 12
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 an existing EAV attribute option value by its option id and store ID
57
     *
58
     * @var \PDOStatement
59
     */
60
    protected $eavAttributeOptionValueByOptionIdAndStoreIdStmt;
61
62
    /**
63
     * The prepared statement to load an existing EAV attribute option value by its attribute code, store ID and value.
64
     *
65
     * @var \PDOStatement
66
     */
67
    protected $eavAttributeOptionValueByAttributeCodeAndStoreIdAndValueStmt;
68
69
    /**
70
     * The prepared statement to load an existing EAV attribute option value by its entity type ID, attribute code, store ID and value.
71
     *
72
     * @var \PDOStatement
73
     */
74
    protected $eavAttributeOptionValueByEntityTypeIdAndAttributeCodeAndStoreIdAndValueStmt;
75
76
    /**
77
     * Initialize the repository with the passed connection and utility class name.
78
     * .
79
     * @param \TechDivision\Import\Connection\ConnectionInterface               $connection             The connection instance
80
     * @param \TechDivision\Import\Repositories\SqlStatementRepositoryInterface $sqlStatementRepository The SQL repository instance
81
     * @param \TechDivision\Import\Cache\CacheAdapterInterface                  $cacheAdapter           The cache adapter instance
82
     */
83
    public function __construct(
84
        ConnectionInterface $connection,
85
        SqlStatementRepositoryInterface $sqlStatementRepository,
86
        CacheAdapterInterface $cacheAdapter
87
    ) {
88
89
        // pass the connection the SQL statement repository to the parent class
90
        parent::__construct($connection, $sqlStatementRepository);
91
92
        // set the cache adapter instance
93
        $this->cacheAdapter = $cacheAdapter;
94
    }
95
96
    /**
97
     * Initializes the repository's prepared statements.
98
     *
99
     * @return void
100
     */
101 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...
102
    {
103
104
        // initialize the prepared statements
105
        $this->eavAttributeOptionValuesStmt =
106
            $this->getConnection()->prepare($this->loadStatement(SqlStatementKeys::EAV_ATTRIBUTE_OPTION_VALUES));
107
108
        // initialize the prepared statements
109
        $this->eavAttributeOptionValueByOptionIdAndStoreIdStmt =
110
            $this->getConnection()->prepare($this->loadStatement(SqlStatementKeys::EAV_ATTRIBUTE_OPTION_VALUE_BY_OPTION_ID_AND_STORE_ID));
111
112
        // initialize the prepared statements
113
        $this->eavAttributeOptionValueByAttributeCodeAndStoreIdAndValueStmt =
114
            $this->getConnection()->prepare($this->loadStatement(SqlStatementKeys::EAV_ATTRIBUTE_OPTION_VALUE_BY_ATTRIBUTE_CODE_AND_STORE_ID_AND_VALUE));
115
116
        // initialize the prepared statements
117
        $this->eavAttributeOptionValueByEntityTypeIdAndAttributeCodeAndStoreIdAndValueStmt =
118
            $this->getConnection()->prepare($this->loadStatement(SqlStatementKeys::EAV_ATTRIBUTE_OPTION_VALUE_BY_ENTITY_TYPE_ID_AND_ATTRIBUTE_CODE_AND_STORE_ID_AND_VALUE));
119
    }
120
121
    /**
122
     * Return's the primary key name of the entity.
123
     *
124
     * @return string The name of the entity's primary key
125
     */
126
    public function getPrimaryKeyName()
127
    {
128
        return MemberNames::VALUE_ID;
129
    }
130
131
    /**
132
     * Returns the cache adapter instance used to warm the repository.
133
     *
134
     * @return \TechDivision\Import\Cache\CacheAdapterInterface The repository's cache adapter instance
135
     */
136
    public function getCacheAdapter()
137
    {
138
        return $this->cacheAdapter;
139
    }
140
141
    /**
142
     * Load's and return's the available EAV attribute option values.
143
     *
144
     * @return array The EAV attribute option values
145
     */
146
    public function findAll()
147
    {
148
149
        // load and return all available EAV attribute option values
150
        $this->eavAttributeOptionValuesStmt->execute(array());
151
        return $this->eavAttributeOptionValuesStmt->fetchAll(\PDO::FETCH_ASSOC);
152
    }
153
154
    /**
155
     * Load's and return's the EAV attribute option value with the passed option ID and store ID
156
     *
157
     * @param string  $optionId The option ID of the attribute option
158
     * @param integer $storeId  The store ID of the attribute option to load
159
     *
160
     * @return array The EAV attribute option value
161
     */
162
    public function findOneByOptionIdAndStoreId($optionId, $storeId)
163
    {
164
165
        // the parameters of the EAV attribute option to load
166
        $params = array(
167
            MemberNames::OPTION_ID => $optionId,
168
            MemberNames::STORE_ID  => $storeId,
169
        );
170
171
        // prepare the cache key
172
        $cacheKey = $this->cacheAdapter->cacheKey(array(SqlStatementKeys::EAV_ATTRIBUTE_OPTION_VALUE_BY_OPTION_ID_AND_STORE_ID => $params));
173
174
        // query whether or not the item has already been cached
175
        if ($this->cacheAdapter->isCached($cacheKey)) {
176
            return $this->cacheAdapter->fromCache($cacheKey);
177
        }
178
179
        // load the EAV attribute option value with the passed parameters
180
        $this->eavAttributeOptionValueByOptionIdAndStoreIdStmt->execute($params);
181
182
        // query whether or not the EAV attribute option value is available in the database
183
        if ($eavAttributeOptionValue = $this->eavAttributeOptionValueByOptionIdAndStoreIdStmt->fetch(\PDO::FETCH_ASSOC)) {
184
            // prepare the unique cache key for the EAV attribute option value
185
            $uniqueKey = array(CacheKeys::EAV_ATTRIBUTE_OPTION_VALUE => $eavAttributeOptionValue[$this->getPrimaryKeyName()]);
186
            // add the EAV attribute option value to the cache, register the cache key reference as well
187
            $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...
188
        }
189
190
        // finally, return it
191
        return $eavAttributeOptionValue;
192
    }
193
194
    /**
195
     * Load's and return's the EAV attribute option value with the passed entity type ID, code, store ID and value.
196
     *
197
     * @param string  $entityTypeId  The entity type ID of the EAV attribute to load the option value for
198
     * @param string  $attributeCode The code of the EAV attribute option to load
199
     * @param integer $storeId       The store ID of the attribute option to load
200
     * @param string  $value         The value of the attribute option to load
201
     *
202
     * @return array The EAV attribute option value
203
     */
204
    public function findOneByEntityTypeIdAndAttributeCodeAndStoreIdAndValue($entityTypeId, $attributeCode, $storeId, $value)
205
    {
206
207
        // the parameters of the EAV attribute option to load
208
        $params = array(
209
            MemberNames::ENTITY_TYPE_ID => $entityTypeId,
210
            MemberNames::ATTRIBUTE_CODE => $attributeCode,
211
            MemberNames::STORE_ID       => $storeId,
212
            MemberNames::VALUE          => $value
213
        );
214
215
        // prepare the cache key
216
        $cacheKey = $this->cacheAdapter->cacheKey(array(SqlStatementKeys::EAV_ATTRIBUTE_OPTION_VALUE_BY_ENTITY_TYPE_ID_AND_ATTRIBUTE_CODE_AND_STORE_ID_AND_VALUE => $params));
217
218
        // return the cached result if available
219
        if ($this->cacheAdapter->isCached($cacheKey)) {
220
            return $this->cacheAdapter->fromCache($cacheKey);
221
        }
222
223
        // load the EAV attribute option value with the passed parameters
224
        $this->eavAttributeOptionValueByEntityTypeIdAndAttributeCodeAndStoreIdAndValueStmt->execute($params);
225
226
        // query whether or not the result has been cached
227
        if ($eavAttributeOptionValue = $this->eavAttributeOptionValueByEntityTypeIdAndAttributeCodeAndStoreIdAndValueStmt->fetch(\PDO::FETCH_ASSOC)) {
228
            // prepare the unique cache key for the EAV attribute option value
229
            $uniqueKey = array(CacheKeys::EAV_ATTRIBUTE_OPTION_VALUE => $eavAttributeOptionValue[$this->getPrimaryKeyName()]);
230
            // add the EAV attribute option value to the cache, register the cache key reference as well
231
            $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...
232
        }
233
234
        // finally, return it
235
        return $eavAttributeOptionValue;
236
    }
237
}
238