Completed
Push — 11.x ( 54c763 )
by Tim
03:08
created

EavAttributeOptionValueCacheWarmer::warm()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 58

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 58
c 0
b 0
f 0
ccs 0
cts 37
cp 0
rs 8.9163
cc 3
nc 3
nop 0
crap 12

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * TechDivision\Import\Repositories\CacheWarmer\EavAttributeOptionValueCacheWarmer
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\CacheWarmer;
22
23
use TechDivision\Import\Utils\CacheKeys;
24
use TechDivision\Import\Utils\MemberNames;
25
use TechDivision\Import\Utils\SqlStatementKeys;
26
use TechDivision\Import\Repositories\EavAttributeOptionValueRepositoryInterface;
27
use TechDivision\Import\Repositories\EavEntityTypeRepositoryInterface;
28
29
/**
30
 * Cache warmer implementation to pre-load 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 EavAttributeOptionValueCacheWarmer implements CacheWarmerInterface
39
{
40
41
    /**
42
     * The repository with the cache that has to be warmed.
43
     *
44
     * @var \TechDivision\Import\Repositories\EavAttributeOptionValueRepositoryInterface
45
     */
46
    protected $repository;
47
48
    /**
49
     * The EAV entity type repository instance.
50
     *
51
     * @var \TechDivision\Import\Repositories\EavEntityTypeRepositoryInterface
52
     */
53
    protected $eavEntityTypeRepository;
54
55
    /**
56
     * Initialize the cache warmer with the repository that has to be warmed.
57
     *
58
     * @param \TechDivision\Import\Repositories\EavAttributeOptionValueRepositoryInterface $repository              The repository to warm
59
     * @param \TechDivision\Import\Repositories\EavEntityTypeRepositoryInterface           $eavEntityTypeRepository The EAV entity type repository instance
60
     */
61
    public function __construct(
62
        EavAttributeOptionValueRepositoryInterface $repository,
63
        EavEntityTypeRepositoryInterface $eavEntityTypeRepository
64
    ) {
65
        $this->repository = $repository;
66
        $this->eavEntityTypeRepository = $eavEntityTypeRepository;
67
    }
68
69
    /**
70
     * Warms the cache for the passed repository.
71
     *
72
     * @return void
73
     */
74
    public function warm()
75
    {
76
77
        // load the cache adapter
78
        /** @var \TechDivision\Import\Cache\CacheAdapterInterface $cacheAdapter */
79
        $cacheAdapter = $this->repository->getCacheAdapter();
80
81
        // load the available EAV attribute option values
82
        $eavAttributeOptionValues = $this->repository->findAll();
83
84
        // load the available EAV entity types
85
        $eavEntityTypes = $this->eavEntityTypeRepository->findAll();
86
87
        // prepare the caches for the statements
88
        foreach ($eavAttributeOptionValues as $eavAttributeOptionValue) {
89
            // (re-)sinitialize the array for the cache keys
90
            $cacheKeys = array();
91
92
            // prepare the unique cache key for the EAV attribute option value
93
            $uniqueKey = array(CacheKeys::EAV_ATTRIBUTE_OPTION_VALUE => $eavAttributeOptionValue[$this->repository->getPrimaryKeyName()]);
94
95
            // prepare the cache key and add the option value to the cache
96
            $cacheKeys[$cacheAdapter->cacheKey(
97
                SqlStatementKeys::EAV_ATTRIBUTE_OPTION_VALUE_BY_OPTION_ID_AND_STORE_ID,
98
                array(
0 ignored issues
show
Unused Code introduced by
The call to CacheAdapterInterface::cacheKey() has too many arguments starting with array(\TechDivision\Impo...emberNames::OPTION_ID]).

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.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
99
                    MemberNames::STORE_ID  => $eavAttributeOptionValue[MemberNames::STORE_ID],
100
                    MemberNames::OPTION_ID => $eavAttributeOptionValue[MemberNames::OPTION_ID]
101
                )
102
            )] = $uniqueKey;
103
104
            // prepare the cache key and add the option value to the cache
105
            $cacheKeys[$cacheAdapter->cacheKey(
106
                SqlStatementKeys::EAV_ATTRIBUTE_OPTION_VALUE_BY_ATTRIBUTE_CODE_AND_STORE_ID_AND_VALUE,
107
                array(
0 ignored issues
show
Unused Code introduced by
The call to CacheAdapterInterface::cacheKey() has too many arguments starting with array(\TechDivision\Impo...ls\MemberNames::VALUE]).

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.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
108
                    MemberNames::ATTRIBUTE_CODE => $eavAttributeOptionValue[MemberNames::ATTRIBUTE_CODE],
109
                    MemberNames::STORE_ID       => $eavAttributeOptionValue[MemberNames::STORE_ID],
110
                    MemberNames::VALUE          => $eavAttributeOptionValue[MemberNames::VALUE]
111
                )
112
            )] = $uniqueKey;
113
114
            // prepare the cache key and add the option value to the cache
115
            foreach ($eavEntityTypes as $eavEntityType) {
116
                // prepare the cache key and add the option value to the cache
117
                $cacheKeys[$cacheAdapter->cacheKey(
118
                    SqlStatementKeys::EAV_ATTRIBUTE_OPTION_VALUE_BY_ENTITY_TYPE_ID_AND_ATTRIBUTE_CODE_AND_STORE_ID_AND_VALUE,
119
                    array(
0 ignored issues
show
Unused Code introduced by
The call to CacheAdapterInterface::cacheKey() has too many arguments starting with array(\TechDivision\Impo...ls\MemberNames::VALUE]).

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.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
120
                        MemberNames::ENTITY_TYPE_ID => $eavEntityType[MemberNames::ENTITY_TYPE_ID],
121
                        MemberNames::ATTRIBUTE_CODE => $eavAttributeOptionValue[MemberNames::ATTRIBUTE_CODE],
122
                        MemberNames::STORE_ID       => $eavAttributeOptionValue[MemberNames::STORE_ID],
123
                        MemberNames::VALUE          => $eavAttributeOptionValue[MemberNames::VALUE]
124
                    )
125
                )] = $uniqueKey;
126
            }
127
128
            // add the EAV attribute option value to the cache
129
            $cacheAdapter->toCache($uniqueKey, $eavAttributeOptionValue, $cacheKeys);
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...
130
        }
131
    }
132
}
133