AddAttributeOptions   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 14
c 1
b 0
f 1
dl 0
loc 61
rs 10
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A translate() 0 7 2
A __construct() 0 3 1
A getIterator() 0 3 1
A getAttributeOptions() 0 8 2
1
<?php
2
3
/**
4
 * Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace SprykerEco\Zed\AkeneoPimMiddlewareConnector\Business\Translator\TranslatorFunction;
9
10
use Iterator;
11
use SprykerEco\Zed\AkeneoPimMiddlewareConnector\Dependency\Service\AkeneoPimMiddlewareConnectorToAkeneoPimServiceInterface;
12
use SprykerMiddleware\Zed\Process\Business\Translator\TranslatorFunction\AbstractTranslatorFunction;
0 ignored issues
show
Bug introduced by
The type SprykerMiddleware\Zed\Pr...tractTranslatorFunction was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use SprykerMiddleware\Zed\Process\Business\Translator\TranslatorFunction\TranslatorFunctionInterface;
0 ignored issues
show
Bug introduced by
The type SprykerMiddleware\Zed\Pr...slatorFunctionInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
15
class AddAttributeOptions extends AbstractTranslatorFunction implements TranslatorFunctionInterface
16
{
17
    /**
18
     * @var array
19
     */
20
    protected const ATTRIBUTE_TYPES_WITH_OPTIONS = [
21
        'pim_catalog_simpleselect',
22
        'pim_catalog_multiselect',
23
    ];
24
25
    /**
26
     * @var \SprykerEco\Zed\AkeneoPimMiddlewareConnector\Dependency\Service\AkeneoPimMiddlewareConnectorToAkeneoPimServiceInterface
27
     */
28
    private $akeneoPimService;
29
30
    /**
31
     * @param \SprykerEco\Zed\AkeneoPimMiddlewareConnector\Dependency\Service\AkeneoPimMiddlewareConnectorToAkeneoPimServiceInterface $akeneoPimService
32
     */
33
    public function __construct(AkeneoPimMiddlewareConnectorToAkeneoPimServiceInterface $akeneoPimService)
34
    {
35
        $this->akeneoPimService = $akeneoPimService;
36
    }
37
38
    /**
39
     * @param mixed $value
40
     * @param array $payload
41
     *
42
     * @return array
43
     */
44
    public function translate($value, array $payload): array
45
    {
46
        if (!in_array($value['type'], static::ATTRIBUTE_TYPES_WITH_OPTIONS)) {
47
            return [];
48
        }
49
50
        return $this->getAttributeOptions($value['code']);
51
    }
52
53
    /**
54
     * @param string $code
55
     *
56
     * @return array
57
     */
58
    protected function getAttributeOptions(string $code): array
59
    {
60
        $options = [];
61
        foreach ($this->getIterator($code) as $option) {
62
            $options[$option['code']] = $option['labels'];
63
        }
64
65
        return $options;
66
    }
67
68
    /**
69
     * @param string $code
70
     *
71
     * @return \Iterator
72
     */
73
    protected function getIterator(string $code): Iterator
74
    {
75
        return $this->akeneoPimService->getAllAttributeOptions($code, $this->options['pageSize'] ?? 100);
76
    }
77
}
78