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 (#6)
by Bart
04:04
created

OrderStatuses::import()   C

Complexity

Conditions 11
Paths 72

Size

Total Lines 46
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 71.5913

Importance

Changes 0
Metric Value
dl 0
loc 46
ccs 7
cts 34
cp 0.2059
rs 5.2653
c 0
b 0
f 0
cc 11
eloc 26
nc 72
nop 2
crap 71.5913

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace NerdsAndCompany\Schematic\Commerce\Services;
4
5
use Craft\Craft;
6
use Craft\Commerce_OrderStatusModel;
7
use NerdsAndCompany\Schematic\Services\Base;
8
9
/**
10
 * Schematic Commerce Order Statuses 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 OrderStatuses extends Base
21
{
22
    /**
23
     * Export orderStatuses.
24
     *
25
     * @param OrderStatusModel[] $orderStatuses
26
     *
27
     * @return array
28
     */
29 2
    public function export(array $orderStatuses = [])
30
    {
31 2
        if (!count($orderStatuses)) {
32
            $orderStatuses = Craft::app()->commerce_orderStatuses->getAllOrderStatuses();
33
        }
34
35 2
        Craft::log(Craft::t('Exporting Commerce Order Statuses'));
36
37 2
        $orderStatusDefinitions = [];
38
39 2
        foreach ($orderStatuses as $orderStatus) {
40 2
            $orderStatusDefinitions[$orderStatus->handle] = $this->getOrderStatusDefinition($orderStatus);
41
        }
42
43
        return $orderStatusDefinitions;
44
    }
45
46
    /**
47
     * Get order statuses definition.
48
     *
49
     * @param Commerce_OrderStatusModel $orderStatus
50
     *
51
     * @return array
52
     */
53 2
    private function getOrderStatusDefinition(Commerce_OrderStatusModel $orderStatus)
54
    {
55
        return [
56 2
            'name' => $orderStatus->name,
57 2
            'color' => $orderStatus->color,
58 2
            'sortOrder' => $orderStatus->sortOrder,
59 2
            'default' => $orderStatus->default,
60 2
            'emails' => array_column($orderStatus->getEmails(), 'name'),
61
        ];
62
    }
63
64
    /**
65
     * Attempt to import order statuses.
66
     *
67
     * @param array $orderStatusDefinitions
68
     * @param bool  $force                  If set to true order statuses not included in the import will be deleted
69
     *
70
     * @return Result
71
     */
72 4
    public function import(array $orderStatusDefinitions, $force = false)
73
    {
74 4
        Craft::log(Craft::t('Importing Commerce Order Statuses'));
75
76 4
        $orderStatuses = [];
77 4
        foreach (Craft::app()->commerce_orderStatuses->getAllOrderStatuses() as $orderStatus) {
78
            $orderStatuses[$orderStatus->handle] = $orderStatus;
79 4
        }
80
81 4
        $emails = [];
82 4
        foreach (Craft::app()->commerce_emails->getAllEmails() as $email) {
83
            $emails[$email->name] = $email;
84
        }
85
86
        foreach ($orderStatusDefinitions as $orderStatusHandle => $orderStatusDefinition) {
87
            $orderStatus = array_key_exists($orderStatusHandle, $orderStatuses)
88
                ? $orderStatuses[$orderStatusHandle]
89
                : new Commerce_OrderStatusModel();
90
91
            unset($orderStatuses[$orderStatusHandle]);
92
93
            $this->populateOrderStatus($orderStatus, $orderStatusDefinition, $orderStatusHandle);
94
            $emailIds = [];
95
            if (is_array(@$orderStatusDefinition['emails'])) {
96
                foreach ($orderStatusDefinition['emails'] as $emailName) {
97
                    if (array_key_exists($emailName, $emails)) {
98
                        $emailIds[] = $emails[$emailName]->id;
99
                    }
100
                }
101
            }
102
103
            if (!Craft::app()->commerce_orderStatuses->saveOrderStatus($orderStatus, $emailIds)) { // Save orderstatus via craft
104
                $this->addErrors($orderStatus->getAllErrors());
105
106
                continue;
107
            }
108
        }
109
110
        if ($force) {
111
            foreach ($orderStatuses as $orderStatus) {
112
                Craft::app()->commerce_orderStatuses->deleteOrderStatusById($orderStatus->id);
113
            }
114
        }
115
116
        return $this->getResultModel();
117
    }
118
119
    /**
120
     * Populate orderStatus.
121
     *
122
     * @param Commerce_OrderStatusModel $orderStatus
123
     * @param array                     $orderStatusDefinition
124
     * @param string                    $orderStatusHandle
125
     */
126
    private function populateOrderStatus(Commerce_OrderStatusModel $orderStatus, array $orderStatusDefinition, $orderStatusHandle)
127
    {
128
        $orderStatus->setAttributes([
129
            'handle' => $orderStatusHandle,
130
            'name' => $orderStatusDefinition['name'],
131
            'color' => $orderStatusDefinition['color'],
132
            'sortOrder' => $orderStatusDefinition['sortOrder'],
133
            'default' => $orderStatusDefinition['default'],
134
        ]);
135
    }
136
}
137