Completed
Push — master ( ef8001...cfe7f0 )
by Viktor
01:57
created

QueueSpool   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Test Coverage

Coverage 90.48%

Importance

Changes 0
Metric Value
dl 0
loc 71
ccs 19
cts 21
cp 0.9048
rs 10
c 0
b 0
f 0
wmc 7

7 Methods

Rating   Name   Duplication   Size   Complexity  
A flushQueue() 0 3 1
A queueMessage() 0 6 1
A init() 0 7 1
A flush() 0 3 1
A start() 0 2 1
A stop() 0 2 1
A isStarted() 0 3 1
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
     * @inheritdoc
39
     */
40 1
    public function init()
41
    {
42 1
        $this->queue = Instance::ensure($this->queue, Queue::className());
43 1
        parent::init();
44 1
        $this->setTransport([
45 1
            'class' => Swift_SpoolTransport::class,
46 1
            'constructArgs' => [$this],
47
        ]);
48 1
    }
49
50
    /**
51
     * @inheritdoc
52
     */
53 1
    public function flush(BaseMailer $mailer, &$failedRecipients = null)
54
    {
55 1
        throw new NotSupportedException();
56
    }
57
58
    /**
59
     * @inheritdoc
60
     */
61 1
    public function start()
62
    {
63 1
    }
64
65
    /**
66
     * @inheritdoc
67
     */
68 1
    public function stop()
69
    {
70 1
    }
71
72
    /**
73
     * @inheritdoc
74
     */
75 1
    public function isStarted()
76
    {
77 1
        return true;
78
    }
79
80
    /**
81
     * @inheritdoc
82
     */
83 1
    public function queueMessage(Swift_Mime_SimpleMessage $message)
84
    {
85 1
        $config = ArrayHelper::merge($this->jobConfig, ['message' => $message]);
86 1
        $job = Yii::createObject($config);
87
88 1
        return $this->queue->push($job) !== null;
89
    }
90
91
    /**
92
     * @inheritdoc
93
     */
94
    public function flushQueue(Swift_Transport $transport, &$failedRecipients = null)
95
    {
96
        throw new NotSupportedException();
97
    }
98
}
99