Config::getConfigFieldForToSortCustomer()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 13
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
namespace O2TI\FieldSortInCheckout\Helper;
10
11
use Magento\Framework\App\Config\ScopeConfigInterface;
12
use Magento\Framework\App\Helper\AbstractHelper;
13
use Magento\Framework\Serialize\Serializer\Json;
14
use Magento\Store\Model\ScopeInterface;
15
use Magento\Store\Model\StoreManagerInterface;
16
17
/**
18
 * Class Config - Helper configuration.
19
 */
20
class Config extends AbstractHelper
21
{
22
    public const CONFIG_PATH_GENERAL = 'field_sort_in_checkout/general/%s';
23
24
    public const CONFIG_PATH_ADDRESS_TO_SORT = 'field_sort_in_checkout/general/address/to_sort';
25
26
    public const CONFIG_PATH_CUSTOMER_TO_SORT = 'field_sort_in_checkout/general/customer/to_sort';
27
28
    /**
29
     * @var Json
30
     */
31
    protected $json;
32
33
    /**
34
     * @var ScopeConfigInterface
35
     */
36
    protected $scopeConfig;
37
38
    /**
39
     * @var StoreManagerInterface
40
     */
41
    protected $storeManagerInterface;
42
43
    /**
44
     * @param ScopeConfigInterface  $scopeConfig
45
     * @param StoreManagerInterface $storeManagerInterface
46
     * @param Json                  $json
47
     */
48
    public function __construct(
49
        ScopeConfigInterface $scopeConfig,
50
        StoreManagerInterface $storeManagerInterface,
51
        Json $json
52
    ) {
53
        $this->scopeConfig = $scopeConfig;
54
        $this->storeManagerInterface = $storeManagerInterface;
55
        $this->json = $json;
56
    }
57
58
    /**
59
     * Get Configs Module.
60
     *
61
     * @param string $field
62
     *
63
     * @return string
64
     */
65
    public function getConfigModule(string $field): ?string
66
    {
67
        $storeId = $this->storeManagerInterface->getStore()->getId();
68
        $configPath = sprintf(self::CONFIG_PATH_GENERAL, $field);
69
70
        return $this->scopeConfig->getValue($configPath, ScopeInterface::SCOPE_STORE, $storeId);
71
    }
72
73
    /**
74
     * Get Configs Field For to Sort Address.
75
     *
76
     * @return array
77
     */
78
    public function getConfigFieldForToSortAddress(): ?array
79
    {
80
        $storeId = $this->storeManagerInterface->getStore()->getId();
81
        $fields = $this->scopeConfig->getValue(
82
            self::CONFIG_PATH_ADDRESS_TO_SORT,
83
            ScopeInterface::SCOPE_STORE,
84
            $storeId
85
        );
86
        if (is_array($fields)) {
87
            return $fields;
88
        }
89
90
        return $this->json->unserialize($fields);
91
    }
92
93
    /**
94
     * Get Sort Order by Field Address.
95
     *
96
     * @param string $fieldName
97
     *
98
     * @return int
99
     */
100
    public function getSortOrderByFieldAddress(string $fieldName): ?int
101
    {
102
        $fieldsToSort = $this->getConfigFieldForToSortAddress();
103
        foreach ($fieldsToSort as $key => $fields) {
104
            if (is_array($fields) && $fields['field'] == $fieldName) {
105
                return $fieldsToSort[$key]['sort_order'];
106
            }
107
        }
108
109
        return 0;
110
    }
111
112
    /**
113
     * Get Configs Field For to Sort Customer.
114
     *
115
     * @return array
116
     */
117
    public function getConfigFieldForToSortCustomer(): ?array
118
    {
119
        $storeId = $this->storeManagerInterface->getStore()->getId();
120
        $fields = $this->scopeConfig->getValue(
121
            self::CONFIG_PATH_CUSTOMER_TO_SORT,
122
            ScopeInterface::SCOPE_STORE,
123
            $storeId
124
        );
125
        if (is_array($fields)) {
126
            return $fields;
127
        }
128
129
        return $this->json->unserialize($fields);
130
    }
131
132
    /**
133
     * Get Sort Order by Field Customer.
134
     *
135
     * @param string $fieldName
136
     *
137
     * @return int
138
     */
139
    public function getSortOrderByFieldCustomer(string $fieldName): ?int
140
    {
141
        $fieldsToSort = $this->getConfigFieldForToSortCustomer();
142
        foreach ($fieldsToSort as $key => $fields) {
143
            if (is_array($fields) && $fields['field'] == $fieldName) {
144
                return $fieldsToSort[$key]['sort_order'];
145
            }
146
        }
147
148
        return 0;
149
    }
150
151
    /**
152
     * Get If is Enabled Module.
153
     *
154
     * @return bool
155
     */
156
    public function isEnabled(): ?bool
157
    {
158
        return $this->getConfigModule('enabled');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getConfigModule('enabled') could return the type string which is incompatible with the type-hinted return boolean|null. Consider adding an additional type-check to rule them out.
Loading history...
159
    }
160
}
161