FeedbackSender   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 5
dl 0
loc 70
ccs 0
cts 25
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A checkOptions() 0 8 3
A send() 0 11 1
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