QueueSpool::flush()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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