Passed
Push[email protected] ( 84519e...8c7e5a )
by Bruno
03:44
created

ChangesSortOrder::changeAddressFieldSortOrder()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 7
c 1
b 0
f 0
nc 5
nop 1
dl 0
loc 14
rs 9.6111
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\Model;
10
11
use O2TI\FieldSortInCheckout\Helper\Config;
12
13
/**
14
 *  ChangesSortOrder - Change Compoments for Inputs.
15
 */
16
class ChangesSortOrder
17
{
18
    /**
19
     * @var Config
20
     */
21
    protected $config;
22
23
    /**
24
     * @param Config $config
25
     */
26
    public function __construct(
27
        Config $config
28
    ) {
29
        $this->config = $config;
30
    }
31
32
    /**
33
     * Change Components at Address Fields.
34
     *
35
     * @param array $fields
36
     *
37
     * @return array
38
     */
39
    public function changeAddressFieldSortOrder(array $fields): array
40
    {
41
        foreach ($fields as $key => $data) {
42
            if (in_array('sortOrder', $fields[$key])) {
43
                if ($fields[$key]['sortOrder']) {
44
                    $newSortOrder = $this->config->getSortOrderByFieldAddress($key);
45
                    if ($newSortOrder) {
46
                        $fields[$key]['sortOrder'] = (int) $newSortOrder;
47
                    }
48
                }
49
            }
50
        }
51
52
        return $fields;
53
    }
54
55
    /**
56
     * Change Components at Customer Fields.
57
     *
58
     * @param array $fields
59
     *
60
     * @return array
61
     */
62
    public function changeCreateAccountFieldSortOrder(array $fields): array
63
    {
64
        foreach ($fields as $key => $data) {
65
            if (in_array('sortOrder', $fields[$key])) {
66
                if ($fields[$key]['sortOrder']) {
67
                    $newSortOrder = $this->config->getSortOrderByFieldCustomer($key);
68
                    if ($newSortOrder) {
69
                        $fields[$key]['sortOrder'] = (int) $newSortOrder;
70
                    }
71
                }
72
            }
73
        }
74
75
        return $fields;
76
    }
77
}
78