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.

PaymentCurrencies   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 85.45%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 1
dl 0
loc 116
ccs 47
cts 55
cp 0.8545
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
C import() 0 35 7
A populatePaymentCurrency() 0 8 1
A resetCraftPaymentCurrenciesServiceCache() 0 10 2
A export() 0 16 3
A getPaymentCurrencyDefinition() 0 8 1
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Commerce\Services;
4
5
use Craft\Craft;
6
use Craft\Commerce_PaymentCurrencyModel;
7
use NerdsAndCompany\Schematic\Services\Base;
8
9
/**
10
 * Schematic Commerce Payment Currencies 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 PaymentCurrencies extends Base
21
{
22
    /**
23
     * Export paymentCurrencies.
24
     *
25
     * @param PaymentCurrencyModel[] $paymentCurrencies
26
     *
27
     * @return array
28
     */
29 2
    public function export(array $paymentCurrencies = [])
30
    {
31 2
        if (!count($paymentCurrencies)) {
32
            $paymentCurrencies = Craft::app()->commerce_paymentCurrencies->getAllPaymentCurrencies();
33
        }
34
35 2
        Craft::log(Craft::t('Exporting Commerce Payment Currencies'));
36
37 2
        $paymentCurrencyDefinitions = [];
38
39 2
        foreach ($paymentCurrencies as $paymentCurrency) {
40 2
            $paymentCurrencyDefinitions[$paymentCurrency->iso] = $this->getPaymentCurrencyDefinition($paymentCurrency);
41 2
        }
42
43 2
        return $paymentCurrencyDefinitions;
44
    }
45
46
    /**
47
     * Get payment currencies definition.
48
     *
49
     * @param Commerce_PaymentCurrencyModel $paymentCurrency
50
     *
51
     * @return array
52
     */
53 2
    private function getPaymentCurrencyDefinition(Commerce_PaymentCurrencyModel $paymentCurrency)
54
    {
55
        return [
56 2
            'iso' => $paymentCurrency->iso,
57 2
            'primary' => $paymentCurrency->primary,
58 2
            'rate' => $paymentCurrency->rate,
59 2
        ];
60
    }
61
62
    /**
63
     * Attempt to import payment currencies.
64
     *
65
     * @param array $paymentCurrencyDefinitions
66
     * @param bool  $force                      If set to true payment currencies not included in the import will be deleted
67
     *
68
     * @return Result
69
     */
70 4
    public function import(array $paymentCurrencyDefinitions, $force = false)
71
    {
72 4
        Craft::log(Craft::t('Importing Commerce Payment Currencies'));
73
74 4
        $this->resetCraftPaymentCurrenciesServiceCache();
75 4
        $paymentCurrencies = [];
76 4
        foreach (Craft::app()->commerce_paymentCurrencies->getAllPaymentCurrencies() as $paymentCurrency) {
77
            $paymentCurrencies[$paymentCurrency->iso] = $paymentCurrency;
78 4
        }
79
80 4
        foreach ($paymentCurrencyDefinitions as $paymentCurrencyDefinition) {
81 2
            $paymentCurrencyHandle = $paymentCurrencyDefinition['iso'];
82 2
            $paymentCurrency = array_key_exists($paymentCurrencyHandle, $paymentCurrencies)
83 2
                ? $paymentCurrencies[$paymentCurrencyHandle]
84 2
                : new Commerce_PaymentCurrencyModel();
85
86 2
            unset($paymentCurrencies[$paymentCurrencyHandle]);
87
88 2
            $this->populatePaymentCurrency($paymentCurrency, $paymentCurrencyDefinition, $paymentCurrencyHandle);
89
90 2
            if (!Craft::app()->commerce_paymentCurrencies->savePaymentCurrency($paymentCurrency)) { // Save paymentcurrency via craft
91 2
                $this->addErrors($paymentCurrency->getAllErrors());
92
93 2
                continue;
94
            }
95 4
        }
96
97 4
        if ($force) {
98 2
            foreach ($paymentCurrencies as $paymentCurrency) {
99
                Craft::app()->commerce_paymentCurrencies->deletePaymentCurrencyById($paymentCurrency->id);
100 2
            }
101 2
        }
102
103 4
        return $this->getResultModel();
104
    }
105
106
    /**
107
     * Populate paymentCurrency.
108
     *
109
     * @param Commerce_PaymentCurrencyModel $paymentCurrency
110
     * @param array                         $paymentCurrencyDefinition
111
     * @param string                        $paymentCurrencyHandle
112
     */
113 2
    private function populatePaymentCurrency(Commerce_PaymentCurrencyModel $paymentCurrency, array $paymentCurrencyDefinition, $paymentCurrencyHandle)
114
    {
115 2
        $paymentCurrency->setAttributes([
116 2
            'iso' => $paymentCurrencyHandle,
117 2
            'primary' => $paymentCurrencyDefinition['primary'],
118 2
            'rate' => $paymentCurrencyDefinition['rate'],
119 2
        ]);
120 2
    }
121
122
    /**
123
     * Reset service cache using reflection.
124
     */
125 4
    private function resetCraftPaymentCurrenciesServiceCache()
126
    {
127 4
        $obj = Craft::app()->commerce_paymentCurrencies;
128 4
        $refObject = new \ReflectionObject($obj);
129 4
        if ($refObject->hasProperty('_allCurrencies')) {
130
            $refProperty = $refObject->getProperty('_allCurrencies');
131
            $refProperty->setAccessible(true);
132
            $refProperty->setValue($obj, null);
133
        }
134 4
    }
135
}
136