|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Health monitoring for Yii2 applications |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://github.com/hiqdev/yii2-monitoring |
|
6
|
|
|
* @package yii2-monitoring |
|
7
|
|
|
* @license BSD-3-Clause |
|
8
|
|
|
* @copyright Copyright (c) 2017, HiQDev (http://hiqdev.com/) |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace hiqdev\yii2\monitoring\logic; |
|
12
|
|
|
|
|
13
|
|
|
use hiqdev\yii2\monitoring\models\FeedbackForm; |
|
14
|
|
|
use hiqdev\yii2\monitoring\Module; |
|
15
|
|
|
use yii\base\BaseObject; |
|
16
|
|
|
use yii\base\InvalidConfigException; |
|
17
|
|
|
use yii\mail\MailerInterface; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class FeedbackSender provides API to send send user feedback to the system administrators. |
|
21
|
|
|
*/ |
|
22
|
|
|
class FeedbackSender extends BaseObject |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var FeedbackForm |
|
26
|
|
|
*/ |
|
27
|
|
|
private $feedbackForm; |
|
28
|
|
|
/** |
|
29
|
|
|
* @var MailerInterface |
|
30
|
|
|
*/ |
|
31
|
|
|
private $mailer; |
|
32
|
|
|
/** |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
public $from; |
|
36
|
|
|
/** |
|
37
|
|
|
* @var string |
|
38
|
|
|
*/ |
|
39
|
|
|
public $to; |
|
40
|
|
|
/** |
|
41
|
|
|
* @var string |
|
42
|
|
|
*/ |
|
43
|
|
|
public $subject; |
|
44
|
|
|
/** |
|
45
|
|
|
* @var string |
|
46
|
|
|
*/ |
|
47
|
|
|
public $view; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* FeedbackSender constructor. |
|
51
|
|
|
* @param FeedbackForm $feedbackForm |
|
52
|
|
|
* @param MailerInterface $mailer |
|
53
|
|
|
* @param array $options |
|
54
|
|
|
*/ |
|
55
|
|
|
public function __construct($feedbackForm, MailerInterface $mailer, $options = []) |
|
56
|
|
|
{ |
|
57
|
|
|
parent::__construct($options); |
|
58
|
|
|
|
|
59
|
|
|
$this->feedbackForm = $feedbackForm; |
|
60
|
|
|
$this->mailer = $mailer; |
|
61
|
|
|
|
|
62
|
|
|
$this->checkOptions(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @throws InvalidConfigException when one of required configuration fields is not filled properly |
|
67
|
|
|
*/ |
|
68
|
|
|
private function checkOptions() |
|
69
|
|
|
{ |
|
70
|
|
|
foreach (['from', 'to', 'subject', 'view'] as $option) { |
|
71
|
|
|
if (!isset($this->{$option})) { |
|
72
|
|
|
throw new InvalidConfigException("Property \"$option\" property must be set"); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @return bool whether email was sent successfully |
|
79
|
|
|
*/ |
|
80
|
|
|
public function send() |
|
81
|
|
|
{ |
|
82
|
|
|
$module = Module::getInstance(); |
|
83
|
|
|
|
|
84
|
|
|
return $this->mailer |
|
85
|
|
|
->compose($this->view, ['form' => $this->feedbackForm]) |
|
86
|
|
|
->setTo($this->to) |
|
87
|
|
|
->setFrom($module->flagEmail($this->from)) |
|
88
|
|
|
->setSubject($module->flagText($this->subject)) |
|
89
|
|
|
->send(); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|