EanField   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 40
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0
wmc 3
lcom 1
cbo 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A toOptionArray() 0 23 2
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