Passed
Push — master ( 16cc4b...9db413 )
by Viktor
01:52
created

QueueSpool::queueMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace izumi\spoolmailer\spools;
4
5
use Swift_Mime_SimpleMessage;
6
use Swift_Spool;
7
use Swift_Transport;
8
use Yii;
9
use yii\base\NotSupportedException;
10
use yii\di\Instance;
11
use yii\helpers\ArrayHelper;
12
use yii\mail\BaseMailer;
13
use yii\queue\Queue;
14
15
/**
16
 * Stores Messages using Queue Jobs.
17
 *
18
 * @author Viktor Khokhryakov <[email protected]>
19
 */
20
class QueueSpool extends BaseSpool implements Swift_Spool
21
{
22
    /**
23
     * @var Queue|array|string the queue object or the application component ID of the queue object.
24
     */
25
    public $queue = 'queue';
26
    /**
27
     * @var array the default configuration of jobs.
28
     */
29
    public $jobConfig = ['class' => 'izumi\spoolmailer\spools\QueueSpoolJob'];
30
31
    /**
32
     * @inheritdoc
33
     */
34
    public function init()
35
    {
36
        $this->queue = Instance::ensure($this->queue, Queue::className());
37
        parent::init();
38
        $this->setTransport([
39
            'class' => 'Swift_SpoolTransport',
40
            'constructArgs' => [$this],
41
        ]);
42
    }
43
44
    /**
45
     * @inheritdoc
46
     */
47
    public function flush(BaseMailer $mailer, &$failedRecipients = null)
48
    {
49
        throw new NotSupportedException();
50
    }
51
52
    /**
53
     * @inheritdoc
54
     */
55
    public function start()
56
    {
57
    }
58
59
    /**
60
     * @inheritdoc
61
     */
62
    public function stop()
63
    {
64
    }
65
66
    /**
67
     * @inheritdoc
68
     */
69
    public function isStarted()
70
    {
71
        return true;
72
    }
73
74
    /**
75
     * @inheritdoc
76
     */
77
    public function queueMessage(Swift_Mime_SimpleMessage $message)
78
    {
79
        $config = ArrayHelper::merge($this->jobConfig, ['message' => $message]);
80
        $job = Yii::createObject($config);
81
82
        return $this->queue->push($job) !== null;
83
    }
84
85
    /**
86
     * @inheritdoc
87
     */
88
    public function flushQueue(Swift_Transport $transport, &$failedRecipients = null)
89
    {
90
        throw new NotSupportedException();
91
    }
92
}
93