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

AbstractEmailByKey::composeMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 11
cp 0
rs 9.9
c 0
b 0
f 0
nc 1
cc 1
nop 2
crap 2
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\queue\BaseJob;
12
use Craft;
13
use craft\elements\User;
14
use craft\helpers\StringHelper;
15
use yii\helpers\Json;
16
use yii\mail\MessageInterface;
17
18
/**
19
 * @author Flipbox Factory <[email protected]>
20
 * @since 2.3.2
21
 */
22
abstract class AbstractEmailByKey extends BaseJob
23
{
24
    /**
25
     * The email key
26
     *
27
     * @return string
28
     */
29
    public abstract function getKey(): string;
30
31
    /**
32
     * The recipients
33
     *
34
     * @return User[]
35
     */
36
    public abstract function getRecipients(): array;
37
38
    /**
39
     * The email params
40
     *
41
     * @return array
42
     */
43
    public abstract function getParams(): array;
44
45
    /**
46
     * Returns a default description for [[getDescription()]].
47
     *
48
     * @return string|null
49
     */
50
    protected function defaultDescription()
51
    {
52
        return 'Sending an email by key.';
53
    }
54
55
    /**
56
     * @param \craft\queue\QueueInterface|\yii\queue\Queue $queue
57
     * @throws \Exception
58
     */
59
    public function execute($queue)
60
    {
61
        // A random tracking string
62
        $id = StringHelper::randomString();
63
64
        try {
65
            Craft::info(
66
                sprintf(
67
                    "[Sending email via job '%s'.] %s",
68
                    (string)$id,
69
                    (string)Json::encode(
70
                        [
71
                            'tracking' => $id,
72
                            'key' => $this->getKey(),
73
                            'recipients' => count($this->getRecipients())
74
                        ]
75
                    )
76
                ), __METHOD__
77
            );
78
79
            foreach ($this->getRecipients() as $recipient) {
80
                if (!$this->composeMessage($recipient, $this->getParams())->send()) {
81
                    Craft::warning(
82
                        sprintf(
83
                            "Failed to send email via job '%s'",
84
                            (string)$id
85
                        ), __METHOD__
86
                    );
87
                    continue;
88
                }
89
90
                Craft::info(
91
                    sprintf(
92
                        "Successfully sent email via job '%s'",
93
                        (string)$id
94
                    ), __METHOD__
95
                );
96
            }
97
98
        } catch (\Exception $e) {
99
            Craft::warning(
100
                sprintf(
101
                    "Exception caught while trying to run '%s' (Id: %s) job. Exception: [%s].",
102
                    (string)get_class($this),
103
                    $id,
104
                    (string)Json::encode([
105
                        'Trace' => $e->getTraceAsString(),
106
                        'File' => $e->getFile(),
107
                        'Line' => $e->getLine(),
108
                        'Code' => $e->getCode(),
109
                        'Message' => $e->getMessage()
110
                    ])
111
                ), __METHOD__
112
            );
113
114
            throw $e;
115
        }
116
    }
117
118
    /**
119
     * @param $recipient
120
     * @param array $params
121
     * @return MessageInterface
122
     * @throws \yii\base\InvalidConfigException
123
     */
124
    protected function composeMessage($recipient, array $params): MessageInterface
125
    {
126
        return Craft::$app->getMailer()
127
            ->composeFromKey(
128
                $this->getKey(),
129
                array_merge(
130
                    ['recipient' => $recipient],
131
                    $params
132
                )
133
            )->setTo($recipient);
134
    }
135
}
136