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.

TaxCategories   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 75.81%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 1
dl 0
loc 123
ccs 47
cts 62
cp 0.7581
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A export() 0 16 3
A getTaxCategoryDefinition() 0 8 1
B import() 0 31 6
A populateTaxCategory() 0 9 1
A resetCraftTaxCategoriesServiceCache() 0 20 4
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Commerce\Services;
4
5
use Craft\Craft;
6
use Craft\Commerce_TaxCategoryModel;
7
use NerdsAndCompany\Schematic\Services\Base;
8
9
/**
10
 * Schematic Commerce Tax Categories 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 TaxCategories extends Base
21
{
22
    /**
23
     * Export taxCategories.
24
     *
25
     * @param TaxCategoryModel[] $taxCategories
26
     *
27
     * @return array
28
     */
29 2
    public function export(array $taxCategories = [])
30
    {
31 2
        if (!count($taxCategories)) {
32
            $taxCategories = Craft::app()->commerce_taxCategories->getAllTaxCategories();
33
        }
34
35 2
        Craft::log(Craft::t('Exporting Commerce Tax Categories'));
36
37 2
        $taxCategoryDefinitions = [];
38
39 2
        foreach ($taxCategories as $taxCategory) {
40 2
            $taxCategoryDefinitions[$taxCategory->handle] = $this->getTaxCategoryDefinition($taxCategory);
41 2
        }
42
43 2
        return $taxCategoryDefinitions;
44
    }
45
46
    /**
47
     * Get tax categories definition.
48
     *
49
     * @param Commerce_TaxCategoryModel $taxCategory
50
     *
51
     * @return array
52
     */
53 2
    private function getTaxCategoryDefinition(Commerce_TaxCategoryModel $taxCategory)
54
    {
55
        return [
56 2
            'name' => $taxCategory->name,
57 2
            'description' => $taxCategory->description,
58 2
            'default' => $taxCategory->default,
59 2
        ];
60
    }
61
62
    /**
63
     * Attempt to import tax categories.
64
     *
65
     * @param array $taxCategoryDefinitions
66
     * @param bool  $force                  If set to true tax categories not included in the import will be deleted
67
     *
68
     * @return Result
69
     */
70 4
    public function import(array $taxCategoryDefinitions, $force = false)
71
    {
72 4
        Craft::log(Craft::t('Importing Commerce Tax Categories'));
73
74 4
        $this->resetCraftTaxCategoriesServiceCache();
75 4
        $taxCategories = Craft::app()->commerce_taxCategories->getAllTaxCategories('handle');
76
77 4
        foreach ($taxCategoryDefinitions as $taxCategoryHandle => $taxCategoryDefinition) {
78 2
            $taxCategory = array_key_exists($taxCategoryHandle, $taxCategories)
79 2
                ? $taxCategories[$taxCategoryHandle]
80 2
                : new Commerce_TaxCategoryModel();
81
82 2
            unset($taxCategories[$taxCategoryHandle]);
83
84 2
            $this->populateTaxCategory($taxCategory, $taxCategoryDefinition, $taxCategoryHandle);
85
86 2
            if (!Craft::app()->commerce_taxCategories->saveTaxCategory($taxCategory)) { // Save taxcategory via craft
87 2
                $this->addErrors($taxCategory->getAllErrors());
88
89 2
                continue;
90
            }
91 4
        }
92
93 4
        if ($force) {
94 2
            foreach ($taxCategories as $taxCategory) {
95
                Craft::app()->commerce_taxCategories->deleteTaxCategoryById($taxCategory->id);
96 2
            }
97 2
        }
98
99 4
        return $this->getResultModel();
100
    }
101
102
    /**
103
     * Populate taxcategory.
104
     *
105
     * @param Commerce_TaxCategoryModel $taxCategory
106
     * @param array                     $taxCategoryDefinition
107
     * @param string                    $taxCategoryHandle
108
     */
109 2
    private function populateTaxCategory(Commerce_TaxCategoryModel $taxCategory, array $taxCategoryDefinition, $taxCategoryHandle)
110
    {
111 2
        $taxCategory->setAttributes([
112 2
            'handle' => $taxCategoryHandle,
113 2
            'name' => $taxCategoryDefinition['name'],
114 2
            'description' => $taxCategoryDefinition['description'],
115 2
            'default' => $taxCategoryDefinition['default'],
116 2
        ]);
117 2
    }
118
119
    /**
120
     * Reset service cache using reflection.
121
     */
122 4
    private function resetCraftTaxCategoriesServiceCache()
123
    {
124 4
        $obj = Craft::app()->commerce_taxCategories;
125 4
        $refObject = new \ReflectionObject($obj);
126 4
        if ($refObject->hasProperty('_fetchedAllTaxCategories')) {
127
            $refProperty = $refObject->getProperty('_fetchedAllTaxCategories');
128
            $refProperty->setAccessible(true);
129
            $refProperty->setValue($obj, false);
130
        }
131 4
        if ($refObject->hasProperty('_taxCategoriesById')) {
132
            $refProperty = $refObject->getProperty('_taxCategoriesById');
133
            $refProperty->setAccessible(true);
134
            $refProperty->setValue($obj, []);
135
        }
136 4
        if ($refObject->hasProperty('_taxCategoriesByHandle')) {
137
            $refProperty = $refObject->getProperty('_taxCategoriesByHandle');
138
            $refProperty->setAccessible(true);
139
            $refProperty->setValue($obj, []);
140
        }
141 4
    }
142
}
143