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
Push — master ( eeec88...a15255 )
by Bob Olde
10s
created

Countries::import()   C

Complexity

Conditions 7
Paths 20

Size

Total Lines 33
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 18
nc 20
nop 2
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Commerce\Services;
4
5
use Craft\Craft;
6
use Craft\Commerce_CountryModel;
7
use NerdsAndCompany\Schematic\Services\Base;
8
9
/**
10
 * Schematic Commerce Countries 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 Countries extends Base
21
{
22
    /**
23
     * Export countries.
24
     *
25
     * @param CountryModel[] $countries
26
     *
27
     * @return array
28
     */
29
    public function export(array $countries = [])
30
    {
31
        if (!count($countries)) {
32
            $countries = Craft::app()->commerce_countries->getAllCountries();
33
        }
34
35
        Craft::log(Craft::t('Exporting Commerce Countries'));
36
37
        $countryDefinitions = [];
38
39
        foreach ($countries as $country) {
40
            $countryDefinitions[$country->iso] = $this->getCountryDefinition($country);
41
        }
42
43
        return $countryDefinitions;
44
    }
45
46
    /**
47
     * Get countries definition.
48
     *
49
     * @param Commerce_CountryModel $country
50
     *
51
     * @return array
52
     */
53
    private function getCountryDefinition(Commerce_CountryModel $country)
54
    {
55
        return [
56
            'name' => $country->name,
57
            'stateRequired' => $country->stateRequired,
58
        ];
59
    }
60
61
    /**
62
     * Attempt to import countries.
63
     *
64
     * @param array $countryDefinitions
65
     * @param bool  $force              If set to true countries not included in the import will be deleted
66
     *
67
     * @return Result
68
     */
69
    public function import(array $countryDefinitions, $force = false)
70
    {
71
        Craft::log(Craft::t('Importing Commerce Countries'));
72
73
        $countries = array();
74
        foreach (Craft::app()->commerce_countries->getAllCountries() as $country) {
75
            $countries[$country->iso] = $country;
76
        }
77
78
        foreach ($countryDefinitions as $countryHandle => $countryDefinition) {
79
            $country = array_key_exists($countryHandle, $countries)
80
                ? $countries[$countryHandle]
81
                : new Commerce_CountryModel();
82
83
            unset($countries[$countryHandle]);
84
85
            $this->populateCountry($country, $countryDefinition, $countryHandle);
86
87
            if (!Craft::app()->commerce_countries->saveCountry($country)) { // Save country via craft
88
                $this->addErrors($country->getAllErrors());
89
90
                continue;
91
            }
92
        }
93
94
        if ($force) {
95
            foreach ($countries as $country) {
96
                Craft::app()->commerce_countries->deleteCountryById($country->id);
97
            }
98
        }
99
100
        return $this->getResultModel();
101
    }
102
103
    /**
104
     * Populate country.
105
     *
106
     * @param Commerce_CountryModel $country
107
     * @param array                 $countryDefinition
108
     * @param string                $countryHandle
109
     */
110
    private function populateCountry(Commerce_CountryModel $country, array $countryDefinition, $countryHandle)
111
    {
112
        $country->setAttributes([
113
            'iso' => $countryHandle,
114
            'name' => $countryDefinition['name'],
115
            'stateRequired' => $countryDefinition['stateRequired'],
116
        ]);
117
    }
118
}
119