Completed
Pull Request — master (#124)
by Tim
22:56
created

EavAttributeOptionValueRepository   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 161
Duplicated Lines 55.28 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 89
loc 161
ccs 0
cts 62
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getPrimaryKeyName() 0 4 1
A init() 18 18 1
A findAll() 0 7 1
B findOneByOptionIdAndStoreId() 35 35 3
B findOneByAttributeCodeAndStoreIdAndValue() 36 36 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * TechDivision\Import\Repositories\EavAttributeOptionValueCachedRepository
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
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Repositories;
22
23
use TechDivision\Import\Utils\MemberNames;
24
25
/**
26
 * Cached repository implementation to load EAV attribute option value data.
27
 *
28
 * @author    Tim Wagner <[email protected]>
29
 * @copyright 2016 TechDivision GmbH <[email protected]>
30
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
31
 * @link      https://github.com/techdivision/import
32
 * @link      http://www.techdivision.com
33
 */
34
class EavAttributeOptionValueRepository extends AbstractCachedRepository implements EavAttributeOptionValueRepositoryInterface
35
{
36
37
    /**
38
     * The prepared statement to load the existing EAV attribute option values.
39
     *
40
     * @var \PDOStatement
41
     */
42
    protected $eavAttributeOptionValuesStmt;
43
44
    /**
45
     * The prepared statement to load an existing EAV attribute option value by its option id and store ID
46
     *
47
     * @var \PDOStatement
48
     */
49
    protected $eavAttributeOptionValueByOptionIdAndStoreIdStmt;
50
51
    /**
52
     * The prepared statement to load an existing EAV attribute option value by its attribute code, store ID and value.
53
     *
54
     * @var \PDOStatement
55
     */
56
    protected $eavAttributeOptionValueByAttributeCodeAndStoreIdAndValueStmt;
57
58
    /**
59
     * Return's the primary key name of the entity.
60
     *
61
     * @return string The name of the entity's primary key
62
     */
63
    public function getPrimaryKeyName()
64
    {
65
        return MemberNames::VALUE_ID;
66
    }
67
68
    /**
69
     * Initializes the repository's prepared statements.
70
     *
71
     * @return void
72
     */
73 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...
74
    {
75
76
        // load the utility class name
77
        $utilityClassName = $this->getUtilityClassName();
78
79
        // initialize the prepared statements
80
        $this->eavAttributeOptionValuesStmt =
81
            $this->getConnection()->prepare($this->getUtilityClass()->find($utilityClassName::EAV_ATTRIBUTE_OPTION_VALUES));
82
83
        // initialize the prepared statements
84
        $this->eavAttributeOptionValueByOptionIdAndStoreIdStmt =
85
            $this->getConnection()->prepare($this->getUtilityClass()->find($utilityClassName::EAV_ATTRIBUTE_OPTION_VALUE_BY_OPTION_ID_AND_STORE_ID));
86
87
        // initialize the prepared statements
88
        $this->eavAttributeOptionValueByAttributeCodeAndStoreIdAndValueStmt =
89
            $this->getConnection()->prepare($this->getUtilityClass()->find($utilityClassName::EAV_ATTRIBUTE_OPTION_VALUE_BY_ATTRIBUTE_CODE_AND_STORE_ID_AND_VALUE));
90
    }
91
92
    /**
93
     * Load's and return's the available EAV attribute option values.
94
     *
95
     * @return array The EAV attribute option values
96
     */
97
    public function findAll()
98
    {
99
100
        // load and return all available EAV attribute option values
101
        $this->eavAttributeOptionValuesStmt->execute(array());
102
        return $this->eavAttributeOptionValuesStmt->fetchAll(\PDO::FETCH_ASSOC);
103
    }
104
105
    /**
106
     * Load's and return's the EAV attribute option value with the passed option ID and store ID
107
     *
108
     * @param string  $optionId The option ID of the attribute option
109
     * @param integer $storeId  The store ID of the attribute option to load
110
     *
111
     * @return array The EAV attribute option value
112
     */
113 View Code Duplication
    public function findOneByOptionIdAndStoreId($optionId, $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...
114
    {
115
116
        // the parameters of the EAV attribute option to load
117
        $params = array(
118
            MemberNames::OPTION_ID => $optionId,
119
            MemberNames::STORE_ID  => $storeId,
120
        );
121
122
        // load the utility class name
123
        $utilityClassName = $this->getUtilityClassName();
124
125
        // prepare the cache key
126
        $cacheKey = $this->cacheKey($utilityClassName::EAV_ATTRIBUTE_OPTION_VALUE_BY_OPTION_ID_AND_STORE_ID, $params);
127
128
        // return the cached result if available
129
        if ($this->isCached($cacheKey)) {
130
            return $this->fromCache($cacheKey);
131
        }
132
133
        // load and return the EAV attribute option value with the passed parameters
134
        $this->eavAttributeOptionValueByOptionIdAndStoreIdStmt->execute($params);
135
136
        // query whether or not the EAV attribute option value is available in the database
137
        if ($eavAttributeOptionValue = $this->eavAttributeOptionValueByOptionIdAndStoreIdStmt->fetch(\PDO::FETCH_ASSOC)) {
138
            // add the EAV attribute option value to the cache, register the cache key reference as well
139
            $this->toCache(
140
                $eavAttributeOptionValue[MemberNames::VALUE_ID],
141
                $eavAttributeOptionValue,
142
                array($cacheKey => $eavAttributeOptionValue[MemberNames::VALUE_ID])
143
            );
144
            // finally, return it
145
            return $eavAttributeOptionValue;
146
        }
147
    }
148
149
    /**
150
     * Load's and return's the EAV attribute option value with the passed code, store ID and value.
151
     *
152
     * @param string  $attributeCode The code of the EAV attribute option to load
153
     * @param integer $storeId       The store ID of the attribute option to load
154
     * @param string  $value         The value of the attribute option to load
155
     *
156
     * @return array The EAV attribute option value
157
     */
158 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...
159
    {
160
161
        // the parameters of the EAV attribute option to load
162
        $params = array(
163
            MemberNames::ATTRIBUTE_CODE => $attributeCode,
164
            MemberNames::STORE_ID       => $storeId,
165
            MemberNames::VALUE          => $value
166
        );
167
168
        // load the utility class name
169
        $utilityClassName = $this->getUtilityClassName();
170
171
        // prepare the cache key
172
        $cacheKey = $this->cacheKey($utilityClassName::EAV_ATTRIBUTE_OPTION_VALUE_BY_ATTRIBUTE_CODE_AND_STORE_ID_AND_VALUE, $params);
173
174
        // return the cached result if available
175
        if ($this->isCached($cacheKey)) {
176
            return $this->fromCache($cacheKey);
177
        }
178
179
        // load and return the EAV attribute option value with the passed parameters
180
        $this->eavAttributeOptionValueByAttributeCodeAndStoreIdAndValueStmt->execute($params);
181
182
        // query whether or not the result has been cached
183
        if ($eavAttributeOptionValue = $this->eavAttributeOptionValueByAttributeCodeAndStoreIdAndValueStmt->fetch(\PDO::FETCH_ASSOC)) {
184
            // add the EAV attribute option value to the cache, register the cache key reference as well
185
            $this->toCache(
186
                $eavAttributeOptionValue[MemberNames::VALUE_ID],
187
                $eavAttributeOptionValue,
188
                array($cacheKey => $eavAttributeOptionValue[MemberNames::VALUE_ID])
189
            );
190
            // finally, return it
191
            return $eavAttributeOptionValue;
192
        }
193
    }
194
}
195