EanField::toOptionArray()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 23
ccs 11
cts 11
cp 1
crap 2
rs 9.552
c 0
b 0
f 0
1
<?php
2
3
namespace Stockbase\Integration\Model\Config\Source;
4
5
use Magento\Eav\Model\Entity\Attribute\Set;
6
use Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection;
7
use Magento\Eav\Model\ResourceModel\Entity\Attribute;
8
use Magento\Framework\ObjectManagerInterface;
9
use Magento\Framework\Option\ArrayInterface;
10
11
/**
12
 * Field selector source for the EAN field select.
13
 */
14
class EanField implements ArrayInterface
15
{
16
    private $objectManager;
17
18
    /**
19
     * EanField constructor.
20
     * @param ObjectManagerInterface $interface
21
     */
22 1
    public function __construct(ObjectManagerInterface $interface)
23
    {
24 1
        $this->objectManager = $interface;
25 1
    }
26
27
    /**
28
     * @return array
29
     */
30 1
    public function toOptionArray()
31
    {
32
        // Load the ResourceModel with all the Attribute Collections (attribute sets in magento)
33 1
        $collection = $this->objectManager->get(Collection::class);
34
35
        // Filter on the Catalog_Product KEY_ENTITY_TYPE_ID which is 4 for products
36 1
        $collection->addFieldToFilter(Set::KEY_ENTITY_TYPE_ID, 4);
37
38
        // Load the items with the given filter (so all product attributes will be filtered)
39 1
        $attributes = $collection->load()->getItems();
40
41
        // Empty array for the option array to return for the configuration source model in stockbase settings
42 1
        $optionArray = [];
43 1
        $optionArray[] = ['label' => '', 'value' => ''];
44 1
        foreach ($attributes as $attribute) {
45 1
            $optionArray[] = [
46 1
                'label' => $attribute->getFrontendLabel(),
47 1
                'value' => $attribute->getAttributeCode(),
48
            ];
49
        }
50
51 1
        return $optionArray;
52
    }
53
}
54