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

OrderStatuses   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 1
dl 0
loc 103
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A export() 0 16 3
A getOrderStatusDefinition() 0 9 1
C import() 0 33 7
A populateOrderStatus() 0 10 1
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
    public function export(array $orderStatuses = [])
30
    {
31
        if (!count($orderStatuses)) {
32
            $orderStatuses = Craft::app()->commerce_orderStatuses->getAllOrderStatuses();
33
        }
34
35
        Craft::log(Craft::t('Exporting Commerce Order Statuses'));
36
37
        $orderStatusDefinitions = [];
38
39
        foreach ($orderStatuses as $orderStatus) {
40
            $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
    private function getOrderStatusDefinition(Commerce_OrderStatusModel $orderStatus)
54
    {
55
        return [
56
            'name' => $orderStatus->name,
57
            'color' => $orderStatus->color,
58
            'sortOrder' => $orderStatus->sortOrder,
59
            'default' => $orderStatus->default,
60
        ];
61
    }
62
63
    /**
64
     * Attempt to import order statuses.
65
     *
66
     * @param array $orderStatusDefinitions
67
     * @param bool  $force                  If set to true order statuses not included in the import will be deleted
68
     *
69
     * @return Result
70
     */
71
    public function import(array $orderStatusDefinitions, $force = false)
72
    {
73
        Craft::log(Craft::t('Importing Commerce Order Statuses'));
74
75
        $orderStatuses = array();
76
        foreach (Craft::app()->commerce_orderStatuses->getAllOrderStatuses() as $orderStatus) {
77
            $orderStatuses[$orderStatus->handle] = $orderStatus;
78
        }
79
80
        foreach ($orderStatusDefinitions as $orderStatusHandle => $orderStatusDefinition) {
81
            $orderStatus = array_key_exists($orderStatusHandle, $orderStatuses)
82
                ? $orderStatuses[$orderStatusHandle]
83
                : new Commerce_OrderStatusModel();
84
85
            unset($orderStatuses[$orderStatusHandle]);
86
87
            $this->populateOrderStatus($orderStatus, $orderStatusDefinition, $orderStatusHandle);
88
89
            if (!Craft::app()->commerce_orderStatuses->saveOrderStatus($orderStatus, array())) { // Save orderstatus via craft
90
                $this->addErrors($orderStatus->getAllErrors());
91
92
                continue;
93
            }
94
        }
95
96
        if ($force) {
97
            foreach ($orderStatuses as $orderStatus) {
98
                Craft::app()->commerce_orderStatuses->deleteOrderStatusById($orderStatus->id);
99
            }
100
        }
101
102
        return $this->getResultModel();
103
    }
104
105
    /**
106
     * Populate orderStatus.
107
     *
108
     * @param Commerce_OrderStatusModel $orderStatus
109
     * @param array                     $orderStatusDefinition
110
     * @param string                    $orderStatusHandle
111
     */
112
    private function populateOrderStatus(Commerce_OrderStatusModel $orderStatus, array $orderStatusDefinition, $orderStatusHandle)
113
    {
114
        $orderStatus->setAttributes([
115
            'handle' => $orderStatusHandle,
116
            'name' => $orderStatusDefinition['name'],
117
            'color' => $orderStatusDefinition['color'],
118
            'sortOrder' => $orderStatusDefinition['sortOrder'],
119
            'default' => $orderStatusDefinition['default'],
120
        ]);
121
    }
122
}
123