|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace yiicod\mailqueue\components; |
|
4
|
|
|
|
|
5
|
|
|
use Yii; |
|
6
|
|
|
use yiicod\mailqueue\models\MailRepositoryInterface; |
|
7
|
|
|
|
|
8
|
|
|
class MailHandler implements MailHandlerInterface |
|
9
|
|
|
{ |
|
10
|
|
|
const STATUS_SENT = 1; |
|
11
|
|
|
|
|
12
|
|
|
const STATUS_PENDING = 0; |
|
13
|
|
|
|
|
14
|
|
|
const STATUS_FAILED = 2; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Find mails |
|
18
|
|
|
* |
|
19
|
|
|
* @return mixed |
|
20
|
|
|
*/ |
|
21
|
|
|
public function findAll() |
|
22
|
|
|
{ |
|
23
|
|
|
$class = Yii::$app->get('mailqueue')->modelMap['mailQueue']['class']; |
|
24
|
|
|
$models = $class::find() |
|
25
|
|
|
->where(sprintf('%s=:pending', $class::attributesMap()['fieldStatus'])) |
|
26
|
|
|
->params([':pending' => self::STATUS_PENDING]) |
|
27
|
|
|
->orderBy($class::attributesMap()['fieldPriority']) |
|
28
|
|
|
->limit(60) |
|
29
|
|
|
->all(); |
|
30
|
|
|
|
|
31
|
|
|
return $models; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param MailRepositoryInterface $item |
|
36
|
|
|
* |
|
37
|
|
|
* @return bool |
|
38
|
|
|
*/ |
|
39
|
|
|
public function send(MailRepositoryInterface $item): bool |
|
40
|
|
|
{ |
|
41
|
|
|
if (Yii::$app->has($item->mailer ?? '')) { |
|
|
|
|
|
|
42
|
|
|
$mailer = Yii::$app->get($item->mailer); |
|
|
|
|
|
|
43
|
|
|
} else { |
|
44
|
|
|
$mailer = Yii::$app->mailer; |
|
45
|
|
|
} |
|
46
|
|
|
$message = $mailer->compose(null, ['item' => $item]); |
|
47
|
|
|
$message->setTo($item->to) |
|
|
|
|
|
|
48
|
|
|
->setSubject($item->subject) |
|
|
|
|
|
|
49
|
|
|
->setHtmlBody($item->body); |
|
|
|
|
|
|
50
|
|
|
if (false === empty($item->from) && true === empty($message->getFrom())) { |
|
|
|
|
|
|
51
|
|
|
$message->setFrom($item->from); |
|
|
|
|
|
|
52
|
|
|
} |
|
53
|
|
|
foreach ($item->getAttaches() as $attach) { |
|
54
|
|
|
$message->attach($attach); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
return $message->send(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param array $ids |
|
62
|
|
|
* |
|
63
|
|
|
* @return mixed |
|
64
|
|
|
*/ |
|
65
|
|
|
public function success(array $ids) |
|
66
|
|
|
{ |
|
67
|
|
|
$class = Yii::$app->get('mailqueue')->modelMap['mailQueue']['class']; |
|
68
|
|
|
$class::updateAll([ |
|
69
|
|
|
$class::attributesMap()['fieldStatus'] => self::STATUS_SENT, |
|
70
|
|
|
$class::attributesMap()['fieldCreatedDate'] => date('Y-m-d H:i:s'), |
|
71
|
|
|
], ['id' => $ids]); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param array $ids |
|
76
|
|
|
* |
|
77
|
|
|
* @return mixed |
|
78
|
|
|
*/ |
|
79
|
|
|
public function failed(array $ids) |
|
80
|
|
|
{ |
|
81
|
|
|
$class = Yii::$app->get('mailqueue')->modelMap['mailQueue']['class']; |
|
82
|
|
|
$class::updateAll([ |
|
83
|
|
|
$class::attributesMap()['fieldStatus'] => self::STATUS_FAILED, |
|
84
|
|
|
$class::attributesMap()['fieldCreatedDate'] => date('Y-m-d H:i:s'), |
|
85
|
|
|
], ['id' => $ids]); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: