CreateEanAttribute   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 100
ccs 47
cts 47
cp 1
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
B execute() 0 62 5
1
<?php
2
3
4
namespace Stockbase\Integration\Controller\Adminhtml\System\Config;
5
6
use Magento\Catalog\Api\Data\ProductAttributeInterface;
7
use Magento\Catalog\Model\ResourceModel\Eav\Attribute;
8
use Magento\Catalog\Model\ResourceModel\Eav\AttributeFactory;
9
use Magento\Backend\App\Action;
10
use Magento\Backend\App\Action\Context;
11
use Magento\Eav\Api\AttributeManagementInterface;
12
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
13
use Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory as AttributeSetCollectionFactory;
14
use Magento\Framework\App\Request\Http;
15
use Magento\Framework\Controller\Result\JsonFactory;
16
use Magento\Framework\Controller\ResultFactory;
17
18
/**
19
 * CreateEanAttribute controller.
20
 */
21
class CreateEanAttribute extends Action
22
{
23
    /**
24
     * @var AttributeFactory
25
     */
26
    private $attributeFactory;
27
    /**
28
     * @var AttributeManagementInterface
29
     */
30
    private $attributeManagement;
31
    /**
32
     * @var AttributeSetCollectionFactory
33
     */
34
    private $attributeSetCollectionFactory;
35
36
    /**
37
     * @param Context                       $context
38
     * @param AttributeFactory              $attributeFactory
39
     * @param AttributeManagementInterface  $attributeManagement
40
     * @param AttributeSetCollectionFactory $attributeSetCollectionFactory
41
     */
42 3
    public function __construct(
43
        Context $context,
44
        AttributeFactory $attributeFactory,
45
        AttributeManagementInterface $attributeManagement,
46
        AttributeSetCollectionFactory $attributeSetCollectionFactory
47
    ) {
48 3
        parent::__construct($context);
49 3
        $this->attributeFactory = $attributeFactory;
50 3
        $this->attributeManagement = $attributeManagement;
51 3
        $this->attributeSetCollectionFactory = $attributeSetCollectionFactory;
52 3
    }
53
    
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 3
    public function execute()
59
    {
60
        /** @var \Magento\Framework\Controller\Result\Json $response */
61 3
        $response = $this->resultFactory->create(ResultFactory::TYPE_JSON);
62
        
63 3
        $request = $this->getRequest();
64 3
        if (!($request instanceof Http) || !$request->isPost()) {
0 ignored issues
show
Bug introduced by
The class Magento\Framework\App\Request\Http does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
65 1
            $response->setHttpResponseCode(\Magento\Framework\Webapi\Exception::HTTP_BAD_REQUEST);
66 1
            $response->setData(['message' => __('Invalid request.')]);
67
68 1
            return $response;
69
        }
70
        
71 2
        $result = [];
72
73 2
        $entityTypeId = $this->_objectManager->create(\Magento\Eav\Model\Entity::class)
74 2
            ->setType(ProductAttributeInterface::ENTITY_TYPE_CODE)
75 2
            ->getTypeId();
76
77 2
        $attribute = $this->attributeFactory->create();
78 2
        $attribute->loadByCode(ProductAttributeInterface::ENTITY_TYPE_CODE, 'ean');
79
        
80 2
        if ($attribute->isEmpty()) {
81
            // Create the attribute
82 1
            $attribute->setEntityTypeId($entityTypeId);
83 1
            $attribute->setAttributeCode('ean');
84 1
            $attribute->setDefaultFrontendLabel('EAN');
85 1
            $attribute->setBackendType('varchar');
86 1
            $attribute->setFrontendInput('text');
87 1
            $attribute->setFrontendClass('validate-digits');
88 1
            $attribute->setIsUserDefined(true);
89 1
            $attribute->setIsGlobal(ScopedAttributeInterface::SCOPE_GLOBAL);
90 1
            $attribute->setIsVisibleOnFront(false);
91 1
            $attribute->setIsRequired(false);
92 1
            $attribute->setIsUnique(false);
93 1
            $attribute->setData(Attribute::IS_USED_IN_GRID, false);
94 1
            $attribute->setApplyTo(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE);
95
96 1
            $attribute->save();
97
98
            // Assign the attribute to every attribute set
99 1
            $attributeSetCollection = $this->attributeSetCollectionFactory->create();
100 1
            $attributeSetCollection->setEntityTypeFilter($entityTypeId);
101 1
            foreach ($attributeSetCollection->getItems() as $attributeSet) {
102 1
                $this->attributeManagement->assign(
103 1
                    ProductAttributeInterface::ENTITY_TYPE_CODE,
104 1
                    $attributeSet->getAttributeSetId(),
105 1
                    $attributeSet->getDefaultGroupId(),
106 1
                    $attribute->getAttributeCode(),
107
                    0
108
                );
109
            }
110
        }
111
112 2
        $result['eanField'] = [
113 2
            'label' => $attribute->getDefaultFrontendLabel(),
114 2
            'value' => $attribute->getAttributeCode(),
115
        ];
116 2
        $response->setData($result);
117
        
118 2
        return $response;
119
    }
120
}
121