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

Emails::import()   C

Complexity

Conditions 7
Paths 20

Size

Total Lines 33
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 18
nc 20
nop 2
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Commerce\Services;
4
5
use Craft\Craft;
6
use Craft\Commerce_EmailModel;
7
use NerdsAndCompany\Schematic\Services\Base;
8
9
/**
10
 * Schematic Commerce Emails 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 Emails extends Base
21
{
22
    /**
23
     * Export emails.
24
     *
25
     * @param EmailModel[] $emails
26
     *
27
     * @return array
28
     */
29
    public function export(array $emails = [])
30
    {
31
        if (!count($emails)) {
32
            $emails = Craft::app()->commerce_emails->getAllEmails();
33
        }
34
35
        Craft::log(Craft::t('Exporting Commerce Emails'));
36
37
        $emailDefinitions = [];
38
39
        foreach ($emails as $email) {
40
            $emailDefinitions[$email->name] = $this->getEmailDefinition($email);
41
        }
42
43
        return $emailDefinitions;
44
    }
45
46
    /**
47
     * Get emails definition.
48
     *
49
     * @param Commerce_EmailModel $email
50
     *
51
     * @return array
52
     */
53
    private function getEmailDefinition(Commerce_EmailModel $email)
54
    {
55
        return [
56
            'name' => $email->name,
57
            'subject' => $email->subject,
58
            'recipientType' => $email->recipientType,
59
            'to' => $email->to,
60
            'bcc' => $email->bcc,
61
            'enabled' => $email->enabled,
62
            'templatePath' => $email->templatePath,
63
        ];
64
    }
65
66
    /**
67
     * Attempt to import emails.
68
     *
69
     * @param array $emailDefinitions
70
     * @param bool  $force            If set to true emails not included in the import will be deleted
71
     *
72
     * @return Result
73
     */
74
    public function import(array $emailDefinitions, $force = false)
75
    {
76
        Craft::log(Craft::t('Importing Commerce Emails'));
77
78
        $emails = array();
79
        foreach (Craft::app()->commerce_emails->getAllEmails() as $email) {
80
            $emails[$email->name] = $email;
81
        }
82
83
        foreach ($emailDefinitions as $emailHandle => $emailDefinition) {
84
            $email = array_key_exists($emailHandle, $emails)
85
                ? $emails[$emailHandle]
86
                : new Commerce_EmailModel();
87
88
            unset($emails[$emailHandle]);
89
90
            $this->populateEmail($email, $emailDefinition, $emailHandle);
91
92
            if (!Craft::app()->commerce_emails->saveEmail($email)) { // Save email via craft
93
                $this->addErrors($email->getAllErrors());
94
95
                continue;
96
            }
97
        }
98
99
        if ($force) {
100
            foreach ($emails as $email) {
101
                Craft::app()->commerce_emails->deleteEmailById($email->id);
102
            }
103
        }
104
105
        return $this->getResultModel();
106
    }
107
108
    /**
109
     * Populate email.
110
     *
111
     * @param Commerce_EmailModel $email
112
     * @param array               $emailDefinition
113
     * @param string              $emailHandle
114
     */
115
    private function populateEmail(Commerce_EmailModel $email, array $emailDefinition, $emailHandle)
116
    {
117
        $email->setAttributes([
118
            'name' => $emailHandle,
119
            'subject' => $emailDefinition['subject'],
120
            'recipientType' => $emailDefinition['recipientType'],
121
            'to' => $emailDefinition['to'],
122
            'bcc' => $emailDefinition['bcc'],
123
            'enabled' => $emailDefinition['enabled'],
124
            'templatePath' => $emailDefinition['templatePath'],
125
        ]);
126
    }
127
}
128