FieldColumn::setInputId()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
/**
3
 * Copyright © O2TI. All rights reserved.
4
 *
5
 * @author    Bruno Elisei <[email protected]>
6
 * See COPYING.txt for license details.
7
 */
8
9
declare(strict_types=1);
10
11
namespace O2TI\FieldSortInCheckout\Block\Adminhtml\Form\Field\Address;
12
13
use Magento\Customer\Model\Address;
14
use Magento\Customer\Model\Customer;
15
use Magento\Eav\Api\Data\AttributeInterface;
16
use Magento\Framework\View\Element\Context;
17
use Magento\Framework\View\Element\Html\Select;
18
use Magento\Ui\Component\Form\AttributeMapper;
19
20
/**
21
 * Class Address Fields To Sort - Create Field to Column.
22
 */
23
class FieldColumn extends Select
24
{
25
    /**
26
     * @var Address
27
     */
28
    private $address;
29
30
    /**
31
     * @var Customer
32
     */
33
    private $customer;
34
35
    /**
36
     * @var AttributeMapper
37
     */
38
    protected $attributeMapper;
39
40
    /**
41
     * Constructor.
42
     *
43
     * @param Context         $context
44
     * @param Address         $address
45
     * @param Customer        $customer
46
     * @param AttributeMapper $attributeMapper
47
     * @param array           $data
48
     */
49
    public function __construct(
50
        Context $context,
51
        Address $address,
52
        Customer $customer,
53
        AttributeMapper $attributeMapper,
0 ignored issues
show
Unused Code introduced by
The parameter $attributeMapper is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

53
        /** @scrutinizer ignore-unused */ AttributeMapper $attributeMapper,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
54
        array $data = []
55
    ) {
56
        parent::__construct($context, $data);
57
        $this->address = $address;
58
        $this->customer = $customer;
59
    }
60
61
    /**
62
     * Set "name" for <select> element.
63
     *
64
     * @param string $value
65
     *
66
     * @return void
67
     */
68
    public function setInputName(string $value)
69
    {
70
        return $this->setName($value);
0 ignored issues
show
Bug introduced by
The method setName() does not exist on O2TI\FieldSortInCheckout...eld\Address\FieldColumn. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

70
        return $this->/** @scrutinizer ignore-call */ setName($value);
Loading history...
Bug Best Practice introduced by
The expression return $this->setName($value) also could return the type O2TI\FieldSortInCheckout...eldColumn|array|boolean which is incompatible with the documented return type void.
Loading history...
71
    }
72
73
    /**
74
     * Set "id" for <select> element.
75
     *
76
     * @param string $value
77
     *
78
     * @return void
79
     */
80
    public function setInputId(string $value)
81
    {
82
        return $this->setId($value);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->setId($value) returns the type O2TI\FieldSortInCheckout...eld\Address\FieldColumn which is incompatible with the documented return type void.
Loading history...
83
    }
84
85
    /**
86
     * Render block HTML.
87
     *
88
     * @return string
89
     */
90
    public function _toHtml(): string
91
    {
92
        if (!$this->getOptions()) {
93
            $this->setOptions($this->getSourceOptions());
94
        }
95
96
        return parent::_toHtml();
97
    }
98
99
    /**
100
     * Get Source Options.
101
     *
102
     * @return array
103
     */
104
    private function getSourceOptions(): array
105
    {
106
        $customerAttributes = $this->getCustomerAttributes();
107
108
        foreach ($customerAttributes as $atribute => $val) {
109
            $attributesArrays[] = [
110
                'label' => __($val['label']).' - '.$atribute,
111
                'value' => $atribute,
112
            ];
113
        }
114
115
        return $attributesArrays;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $attributesArrays seems to be defined by a foreach iteration on line 108. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
116
    }
117
118
    /**
119
     * Get customer attributes.
120
     *
121
     * @return array
122
     */
123
    private function getCustomerAttributes(): array
124
    {
125
        $elements = [];
126
127
        /** @var AttributeInterface[] $attributes */
128
        $attributesAddress = $this->address->getAttributes();
129
130
        foreach ($attributesAddress as $attribute) {
131
            $code = $attribute->getAttributeCode();
132
            $inForms = $attribute->getUsedInForms();
133
            $forms = [];
134
            foreach ($inForms as $value) {
135
                $forms[] = $value;
136
            }
137
            if (!in_array('customer_register_address', $forms)) {
138
                continue;
139
            }
140
            if ((int) $attribute->getData('is_visible') === 0) {
141
                continue;
142
            }
143
            $elements[$code]['label'] = $attribute->getData('frontend_label');
144
        }
145
146
        return $elements;
147
    }
148
}
149