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.

OrderSettings   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 94.74%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 1
dl 0
loc 95
ccs 36
cts 38
cp 0.9474
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A export() 0 16 3
A getOrderSettingDefinition() 0 9 1
B import() 0 25 5
A populateOrderSetting() 0 10 1
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Commerce\Services;
4
5
use Craft\Craft;
6
use Craft\Commerce_OrderSettingsModel;
7
use NerdsAndCompany\Schematic\Services\Base;
8
9
/**
10
 * Schematic Commerce Order Settings 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 OrderSettings extends Base
21
{
22
    /**
23
     * Export orderSettings.
24
     *
25
     * @param OrderSettingsModel[] $orderSettings
26
     *
27
     * @return array
28
     */
29 1
    public function export(array $orderSettings = [])
30
    {
31 1
        if (!count($orderSettings)) {
32
            $orderSettings = [Craft::app()->commerce_orderSettings->getOrderSettingByHandle('order')];
33
        }
34
35 1
        Craft::log(Craft::t('Exporting Commerce Order Settings'));
36
37 1
        $orderSettingDefinitions = [];
38
39 1
        foreach ($orderSettings as $orderSetting) {
40 1
            $orderSettingDefinitions[$orderSetting->handle] = $this->getOrderSettingDefinition($orderSetting);
41 1
        }
42
43 1
        return $orderSettingDefinitions;
44
    }
45
46
    /**
47
     * Get order settings definition.
48
     *
49
     * @param Commerce_OrderSettingsModel $orderSetting
50
     *
51
     * @return array
52
     */
53 1
    private function getOrderSettingDefinition(Commerce_OrderSettingsModel $orderSetting)
54
    {
55 1
        $fieldLayout = Craft::app()->fields->getLayoutById($orderSetting->fieldLayoutId);
56
57
        return [
58 1
            'name' => $orderSetting->name,
59 1
            'fieldLayout' => Craft::app()->schematic_fields->getFieldLayoutDefinition($fieldLayout),
60 1
        ];
61
    }
62
63
    /**
64
     * Attempt to import order settings.
65
     *
66
     * @param array $orderSettingDefinitions
67
     * @param bool  $force                   If set to true order settings not included in the import will be deleted
68
     *
69
     * @return Result
70
     */
71 4
    public function import(array $orderSettingDefinitions, $force = false)
72
    {
73 4
        Craft::log(Craft::t('Importing Commerce Order Settings'));
74
75 4
        $settings = Craft::app()->commerce_orderSettings->getOrderSettingByHandle('order');
76 4
        $orderSettings = $settings ? ['order' => $settings] : [];
77
78 4
        foreach ($orderSettingDefinitions as $orderSettingHandle => $orderSettingDefinition) {
79 2
            $orderSetting = array_key_exists($orderSettingHandle, $orderSettings)
80 2
                ? $orderSettings[$orderSettingHandle]
81 2
                : new Commerce_OrderSettingsModel();
82
83 2
            unset($orderSettings[$orderSettingHandle]);
84
85 2
            $this->populateOrderSetting($orderSetting, $orderSettingDefinition, $orderSettingHandle);
86
87 2
            if (!Craft::app()->commerce_orderSettings->saveOrderSetting($orderSetting)) { // Save ordersettings via craft
88 2
                $this->addErrors($orderSetting->getAllErrors());
89
90 2
                continue;
91
            }
92 4
        }
93
94 4
        return $this->getResultModel();
95
    }
96
97
    /**
98
     * Populate ordersettings.
99
     *
100
     * @param Commerce_OrderSettingsModel $orderSetting
101
     * @param array                       $orderSettingDefinition
102
     * @param string                      $orderSettingHandle
103
     */
104 2
    private function populateOrderSetting(Commerce_OrderSettingsModel $orderSetting, array $orderSettingDefinition, $orderSettingHandle)
105
    {
106 2
        $orderSetting->setAttributes([
107 2
            'handle' => $orderSettingHandle,
108 2
            'name' => $orderSettingDefinition['name'],
109 2
        ]);
110
111 2
        $fieldLayout = Craft::app()->schematic_fields->getFieldLayout($orderSettingDefinition['fieldLayout']);
112 2
        $orderSetting->setFieldLayout($fieldLayout);
113 2
    }
114
}
115