ProductAttributeHelper::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace DefaultValue\Bundle\AkeneoInlineEditBundle\Product;
4
5
use Pim\Component\Catalog\Repository\AttributeRepositoryInterface;
6
use Pim\Bundle\CatalogBundle\Doctrine\ORM\Repository\AttributeRepository;
7
use Doctrine\ORM\EntityManager;
8
9
/**
10
 * Helper for working with attribute repository
11
 */
12
class ProductAttributeHelper
13
{
14
    /**
15
     * @var AttributeRepository
16
     */
17
    private $attributeRepository;
18
19
    /**
20
     * @param AttributeRepository $attributeRepository
21
     */
22
    public function __construct(AttributeRepository $attributeRepository)
23
    {
24
        $this->attributeRepository = $attributeRepository;
25
    }
26
27
    /**
28
     * Get from repository codes of scopable attributes
29
     * @return array
30
     */
31
    public function getScopableAttributes()
32
    {
33
        $scopableEntities = $this->attributeRepository->findByScopable(1);
34
35
        return $this->prepareAttributesList($scopableEntities);
36
    }
37
38
    /**
39
     * Get from repository codes of localizable attributes
40
     * @return array
41
     */
42
    public function getLocalizableAttributes()
43
    {
44
        $localizableEntities = $this->attributeRepository->findByLocalizable(1);
45
46
        return $this->prepareAttributesList($localizableEntities);
47
    }
48
49
    /**
50
     * Get from repository codes of price attributes
51
     * @return array
52
     */
53
    public function getPriceAttributes()
54
    {
55
        $attributeType = 'pim_catalog_price_collection'; // attribute type for prices in database
56
        $priceEntities = $this->attributeRepository->findByAttributeType($attributeType);
57
58
        return $this->prepareAttributesList($priceEntities);
59
    }
60
61
    /**
62
     * @param $attributesCollection
63
     * @return array
64
     */
65
    private function prepareAttributesList($attributesCollection)
66
    {
67
        $attributes = [];
68
69
        /**
70
         * @var \Pim\Bundle\CatalogBundle\Entity\Attribute $entity
71
         */
72
        foreach ($attributesCollection as $entity) {
73
            $attributes[] = $entity->getCode();
74
        }
75
76
        return $attributes;
77
    }
78
}
79