GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#2)
by Bob Olde
04:50
created

TaxZones::getTaxZoneDefinition()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
cc 3
eloc 14
nc 4
nop 1
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Commerce\Services;
4
5
use Craft\Craft;
6
use Craft\Commerce_TaxZoneModel;
7
use NerdsAndCompany\Schematic\Services\Base;
8
9
/**
10
 * Schematic Commerce Tax Zones Service.
11
 *
12
 * Sync Craft Setups.
13
 *
14
 * @author    Nerds & Company
15
 * @copyright Copyright (c) 2015-2017, Nerds & Company
16
 * @license   MIT
17
 *
18
 * @see      http://www.nerds.company
19
 */
20
class TaxZones extends Base
21
{
22
    /**
23
     * Export taxZones.
24
     *
25
     * @param TaxZoneModel[] $taxZones
26
     *
27
     * @return array
28
     */
29
    public function export(array $taxZones = [])
30
    {
31
        if (!count($taxZones)) {
32
            $taxZones = Craft::app()->commerce_taxZones->getAllTaxZones(true);
33
        }
34
35
        Craft::log(Craft::t('Exporting Commerce Tax Zones'));
36
37
        $taxZoneDefinitions = [];
38
39
        foreach ($taxZones as $taxZone) {
40
            $taxZoneDefinitions[$taxZone->name] = $this->getTaxZoneDefinition($taxZone);
41
        }
42
43
        return $taxZoneDefinitions;
44
    }
45
46
    /**
47
     * Get tax zones definition.
48
     *
49
     * @param Commerce_TaxZoneModel $taxZone
50
     *
51
     * @return array
52
     */
53
    private function getTaxZoneDefinition(Commerce_TaxZoneModel $taxZone)
54
    {
55
        $countries = array();
56
        foreach ($taxZone->getCountries() as $country) {
57
            $countries[] = $country->iso;
58
        }
59
60
        $states = array();
61
        foreach ($taxZone->getStates() as $state) {
62
            $states[] = $state->abbreviation;
63
        }
64
65
        return [
66
            'name' => $taxZone->name,
67
            'description' => $taxZone->description,
68
            'countryBased' => $taxZone->countryBased,
69
            'default' => $taxZone->default,
70
            'countries' => $countries,
71
            'states' => $states,
72
        ];
73
    }
74
75
    /**
76
     * Attempt to import tax zones.
77
     *
78
     * @param array $taxZoneDefinitions
79
     * @param bool  $force              If set to true tax zones not included in the import will be deleted
80
     *
81
     * @return Result
82
     */
83
    public function import(array $taxZoneDefinitions, $force = false)
84
    {
85
        Craft::log(Craft::t('Importing Commerce Tax Zones'));
86
87
        $taxZones = array();
88
        foreach (Craft::app()->commerce_taxZones->getAllTaxZones() as $taxZone) {
89
            $taxZones[$taxZone->name] = $taxZone;
90
        }
91
92
        foreach ($taxZoneDefinitions as $taxZoneDefinition) {
93
            $taxZoneHandle = $taxZoneDefinition['name'];
94
            $taxZone = array_key_exists($taxZoneHandle, $taxZones)
95
                ? $taxZones[$taxZoneHandle]
96
                : new Commerce_TaxZoneModel();
97
98
            unset($taxZones[$taxZoneHandle]);
99
100
            $this->populateTaxZone($taxZone, $taxZoneDefinition, $taxZoneHandle);
101
102
            $countryIds = array();
103
            foreach ($taxZone->getCountries() as $country) {
104
                $countryIds[] = $country->id;
105
            }
106
107
            $stateIds = array();
108
            foreach ($taxZone->getStates() as $state) {
109
                $stateIds[] = $state->id;
110
            }
111
112
            if (!Craft::app()->commerce_taxZones->saveTaxZone($taxZone, $countryIds, $stateIds)) { // Save taxzone via craft
113
                $this->addErrors($taxZone->getAllErrors());
114
115
                continue;
116
            }
117
        }
118
119
        if ($force) {
120
            foreach ($taxZones as $taxZone) {
121
                Craft::app()->commerce_taxZones->deleteTaxZoneById($taxZone->id);
122
            }
123
        }
124
125
        return $this->getResultModel();
126
    }
127
128
    /**
129
     * Populate taxZone.
130
     *
131
     * @param Commerce_TaxZoneModel $taxZone
132
     * @param array                 $taxZoneDefinition
133
     * @param string                $taxZoneHandle
134
     */
135
    private function populateTaxZone(Commerce_TaxZoneModel $taxZone, array $taxZoneDefinition, $taxZoneHandle)
136
    {
137
        $taxZone->setAttributes([
138
            'name' => $taxZoneHandle,
139
            'description' => $taxZoneDefinition['description'],
140
            'countryBased' => $taxZoneDefinition['countryBased'],
141
            'default' => $taxZoneDefinition['default'],
142
        ]);
143
144
        $countries = array();
145
        foreach ($taxZoneDefinition['countries'] as $iso) {
146
            $countries[] = Craft::app()->commerce_countries->getCountryByAttributes(array('iso' => $iso));
147
        }
148
        $taxZone->setCountries($countries);
149
150
        $states = array();
151
        foreach ($taxZoneDefinition['states'] as $abbreviation) {
152
            $states[] = Craft::app()->commerce_states->getStateByAttributes(array('abbreviation' => $abbreviation));
153
        }
154
        $taxZone->setStates($states);
155
    }
156
}
157