Completed
Push — develop ( 30b661...4db1a6 )
by Nate
02:21
created

EmailByKey::resolveUser()   C

Complexity

Conditions 11
Paths 12

Size

Total Lines 56

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 0
Metric Value
dl 0
loc 56
ccs 0
cts 41
cp 0
rs 6.8133
c 0
b 0
f 0
cc 11
nc 12
nop 1
crap 132

1 Method

Rating   Name   Duplication   Size   Complexity  
A EmailByKey::unserialize() 0 7 1

How to fix   Long Method    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
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipboxfactory/craft-ember/blob/master/LICENSE
6
 * @link       https://github.com/flipboxfactory/craft-ember/
7
 */
8
9
namespace flipbox\craft\ember\queue;
10
11
use Craft;
12
use craft\elements\User;
13
use craft\helpers\ArrayHelper;
14
15
/**
16
 * @author Flipbox Factory <[email protected]>
17
 * @since 2.3.2
18
 */
19
class EmailByKey extends AbstractEmailByKey implements \Serializable
20
{
21
    /**
22
     *  The email key
23
     *
24
     * @var string
25
     */
26
    public $key;
27
28
    /**
29
     * The recipients
30
     *
31
     * @var array
32
     */
33
    protected $recipients = [];
34
35
    /**
36
     * The email params
37
     *
38
     * @var array
39
     */
40
    protected $params;
41
42
    /**
43
     * @inheritdoc
44
     */
45
    public function getKey(): string
46
    {
47
        return $this->key;
48
    }
49
50
    /**
51
     * @inheritdoc
52
     */
53
    public function getRecipients(): array
54
    {
55
        return $this->recipients;
56
    }
57
58
    /**
59
     * @param array $recipients
60
     * @return $this
61
     */
62
    public function setRecipients(array $recipients)
63
    {
64
        foreach ($recipients as $recipient) {
65
            $this->addRecipient($recipient);
66
        }
67
        return $this;
68
    }
69
70
    /**
71
     * @param $recipient
72
     * @return $this
73
     */
74
    public function addRecipient($recipient)
75
    {
76
        if ($recipient instanceof User) {
77
            $recipient = [$recipient->email];
78
        }
79
80
        $this->recipients[] = $recipient;
81
        return $this;
82
    }
83
84
    /**
85
     * @inheritdoc
86
     */
87
    public function getParams(): array
88
    {
89
        return $this->params;
90
    }
91
92
    /**
93
     * @param $params
94
     * @return $this
95
     */
96
    public function setParams($params)
97
    {
98
        $this->params = ArrayHelper::toArray($params);
99
        return $this;
100
    }
101
102
    /**
103
     * @param User|int|string|array $user
104
     * @return User
105
     */
106
    protected function resolveUser($user)
107
    {
108
        if ($user instanceof User) {
109
            return $user;
110
        }
111
112
        // An Id
113
        if (is_numeric($user)) {
114
            return Craft::$app->getUsers()->getUserById($user);
115
        }
116
117
        // An email
118
        if (is_string($user)) {
119
            if (!$element = Craft::$app->getUsers()->getUserByUsernameOrEmail($user)) {
120
                $element = new User([
121
                    'email' => $user
122
                ]);
123
            }
124
125
            return $element;
126
        }
127
128
        if (is_array($user)) {
129
            $email = key($user);
130
            $user = reset($user);
131
132
            // $user was an array [email => name]
133
            if (is_string($email)) {
134
                @list($firstName, $lastName) = explode(' ', $user);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
135
136
                // Resolve user (and set name)
137
                if ($element = $this->resolveUser($email)) {
138
                    $element->firstName = $firstName ?: $element->firstName;
139
                    $element->lastName = $lastName ?: $element->lastName;
140
                    return $element;
141
                }
142
143
                return new User([
144
                    'email' => $email,
145
                    'firstName' => $firstName,
146
                    'lastName' => $lastName
147
                ]);
148
            }
149
150
            // An array of [$user]
151
            if (!$element = $this->resolveUser($user)) {
152
                return new User([
153
                    'email' => $user
154
                ]);
155
            }
156
157
            return $element;
158
        }
159
160
        return null;
161
    }
162
163
    /**
164
     * @inheritdoc
165
     */
166
    public function serialize()
167
    {
168
        return serialize([
169
            'key' => $this->key,
170
            'recipients' => $this->recipients,
171
            'params' => $this->params
172
        ]);
173
    }
174
175
    /**
176
     * @inheritdoc
177
     */
178
    public function unserialize($serialized)
179
    {
180
        Craft::configure(
181
            $this,
182
            unserialize($serialized)
183
        );
184
    }
185
}
186