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.

TaxRates   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 88.33%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 1
dl 0
loc 117
ccs 53
cts 60
cp 0.8833
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A export() 0 16 3
A getTaxRateDefinition() 0 12 1
C import() 0 34 7
A populateTaxRate() 0 20 3
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Commerce\Services;
4
5
use Craft\Craft;
6
use Craft\Commerce_TaxRateModel;
7
use NerdsAndCompany\Schematic\Services\Base;
8
9
/**
10
 * Schematic Commerce Tax Rates 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 TaxRates extends Base
21
{
22
    /**
23
     * Export taxRates.
24
     *
25
     * @param TaxRateModel[] $taxRates
26
     *
27
     * @return array
28
     */
29 2
    public function export(array $taxRates = [])
30
    {
31 2
        if (!count($taxRates)) {
32
            $taxRates = Craft::app()->commerce_taxRates->getAllTaxRates();
33
        }
34
35 2
        Craft::log(Craft::t('Exporting Commerce Tax Rates'));
36
37 2
        $taxRateDefinitions = [];
38
39 2
        foreach ($taxRates as $taxRate) {
40 2
            $taxRateDefinitions[$taxRate->name] = $this->getTaxRateDefinition($taxRate);
41 2
        }
42
43 2
        return $taxRateDefinitions;
44
    }
45
46
    /**
47
     * Get tax rates definition.
48
     *
49
     * @param Commerce_TaxRateModel $taxRate
50
     *
51
     * @return array
52
     */
53 2
    private function getTaxRateDefinition(Commerce_TaxRateModel $taxRate)
54
    {
55
        return [
56 2
            'name' => $taxRate->name,
57 2
            'rate' => $taxRate->rate,
58 2
            'include' => $taxRate->include,
59 2
            'isVat' => $taxRate->isVat,
60 2
            'taxable' => $taxRate->taxable,
61 2
            'taxCategory' => $taxRate->getTaxCategory()->handle,
62 2
            'taxZone' => $taxRate->getTaxZone()->name,
63 2
        ];
64
    }
65
66
    /**
67
     * Attempt to import tax rates.
68
     *
69
     * @param array $taxRateDefinitions
70
     * @param bool  $force              If set to true tax rates not included in the import will be deleted
71
     *
72
     * @return Result
73
     */
74 4
    public function import(array $taxRateDefinitions, $force = false)
75
    {
76 4
        Craft::log(Craft::t('Importing Commerce Tax Rates'));
77
78 4
        $taxRates = [];
79 4
        foreach (Craft::app()->commerce_taxRates->getAllTaxRates() as $taxRate) {
80
            $taxRates[$taxRate->name] = $taxRate;
81 4
        }
82
83 4
        foreach ($taxRateDefinitions as $taxRateDefinition) {
84 2
            $taxRateHandle = $taxRateDefinition['name'];
85 2
            $taxRate = array_key_exists($taxRateHandle, $taxRates)
86 2
                ? $taxRates[$taxRateHandle]
87 2
                : new Commerce_TaxRateModel();
88
89 2
            unset($taxRates[$taxRateHandle]);
90
91 2
            $this->populateTaxRate($taxRate, $taxRateDefinition, $taxRateHandle);
92
93 2
            if (!Craft::app()->commerce_taxRates->saveTaxRate($taxRate)) { // Save taxrate via craft
94 2
                $this->addErrors($taxRate->getAllErrors());
95
96 2
                continue;
97
            }
98 4
        }
99
100 4
        if ($force) {
101 2
            foreach ($taxRates as $taxRate) {
102
                Craft::app()->commerce_taxRates->deleteTaxRateById($taxRate->id);
103 2
            }
104 2
        }
105
106 4
        return $this->getResultModel();
107
    }
108
109
    /**
110
     * Populate taxRate.
111
     *
112
     * @param Commerce_TaxRateModel $taxRate
113
     * @param array                 $taxRateDefinition
114
     * @param string                $taxRateHandle
115
     */
116 2
    private function populateTaxRate(Commerce_TaxRateModel $taxRate, array $taxRateDefinition, $taxRateHandle)
117
    {
118 2
        $taxCategory = Craft::app()->commerce_taxCategories->getTaxCategoryByHandle($taxRateDefinition['taxCategory']);
119 2
        $taxZone = null;
120 2
        foreach (Craft::app()->commerce_taxZones->getAllTaxZones() as $zone) {
121
            if ($zone->name == $taxRateDefinition['taxZone']) {
122
                $taxZone = $zone->id;
123
            }
124 2
        }
125
126 2
        $taxRate->setAttributes([
127 2
            'name' => $taxRateHandle,
128 2
            'rate' => $taxRateDefinition['rate'],
129 2
            'include' => $taxRateDefinition['include'],
130 2
            'isVat' => $taxRateDefinition['isVat'],
131 2
            'taxable' => $taxRateDefinition['taxable'],
132 2
            'taxCategoryId' => $taxCategory->id,
133 2
            'taxZoneId' => $taxZone,
134 2
        ]);
135 2
    }
136
}
137