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.

Emails   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 92.31%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 1
dl 0
loc 108
ccs 48
cts 52
cp 0.9231
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A export() 0 16 3
A getEmailDefinition() 0 12 1
C import() 0 33 7
A populateEmail() 0 12 1
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 2
    public function export(array $emails = [])
30
    {
31 2
        if (!count($emails)) {
32
            $emails = Craft::app()->commerce_emails->getAllEmails();
33
        }
34
35 2
        Craft::log(Craft::t('Exporting Commerce Emails'));
36
37 2
        $emailDefinitions = [];
38
39 2
        foreach ($emails as $email) {
40 2
            $emailDefinitions[$email->name] = $this->getEmailDefinition($email);
41 2
        }
42
43 2
        return $emailDefinitions;
44
    }
45
46
    /**
47
     * Get emails definition.
48
     *
49
     * @param Commerce_EmailModel $email
50
     *
51
     * @return array
52
     */
53 2
    private function getEmailDefinition(Commerce_EmailModel $email)
54
    {
55
        return [
56 2
            'name' => $email->name,
57 2
            'subject' => $email->subject,
58 2
            'recipientType' => $email->recipientType,
59 2
            'to' => $email->to,
60 2
            'bcc' => $email->bcc,
61 2
            'enabled' => $email->enabled,
62 2
            'templatePath' => $email->templatePath,
63 2
        ];
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 4
    public function import(array $emailDefinitions, $force = false)
75
    {
76 4
        Craft::log(Craft::t('Importing Commerce Emails'));
77
78 4
        $emails = [];
79 4
        foreach (Craft::app()->commerce_emails->getAllEmails() as $email) {
80
            $emails[$email->name] = $email;
81 4
        }
82
83 4
        foreach ($emailDefinitions as $emailHandle => $emailDefinition) {
84 2
            $email = array_key_exists($emailHandle, $emails)
85 2
                ? $emails[$emailHandle]
86 2
                : new Commerce_EmailModel();
87
88 2
            unset($emails[$emailHandle]);
89
90 2
            $this->populateEmail($email, $emailDefinition, $emailHandle);
91
92 2
            if (!Craft::app()->commerce_emails->saveEmail($email)) { // Save email via craft
93 2
                $this->addErrors($email->getAllErrors());
94
95 2
                continue;
96
            }
97 4
        }
98
99 4
        if ($force) {
100 2
            foreach ($emails as $email) {
101
                Craft::app()->commerce_emails->deleteEmailById($email->id);
102 2
            }
103 2
        }
104
105 4
        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 2
    private function populateEmail(Commerce_EmailModel $email, array $emailDefinition, $emailHandle)
116
    {
117 2
        $email->setAttributes([
118 2
            'name' => $emailHandle,
119 2
            'subject' => $emailDefinition['subject'],
120 2
            'recipientType' => $emailDefinition['recipientType'],
121 2
            'to' => $emailDefinition['to'],
122 2
            'bcc' => $emailDefinition['bcc'],
123 2
            'enabled' => $emailDefinition['enabled'],
124 2
            'templatePath' => $emailDefinition['templatePath'],
125 2
        ]);
126 2
    }
127
}
128