Completed
Push — master ( 5ee4c9...308b6a )
by Antonio
05:05
created

MailService::setViewParam()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 2
1
<?php
2
3
/*
4
 * This file is part of the 2amigos/yii2-usuario project.
5
 *
6
 * (c) 2amigOS! <http://2amigos.us/>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace Da\User\Service;
13
14
use Da\User\Contracts\ServiceInterface;
15
use Yii;
16
use yii\mail\BaseMailer;
17
use yii\mail\MailerInterface;
18
19
class MailService implements ServiceInterface
20
{
21
    protected $viewPath = '@Da/User/resources/views/mail';
22
23
    protected $from;
24
    protected $to;
25
    protected $subject;
26
    protected $view;
27
    protected $params = [];
28
    protected $mailer;
29
30
    /**
31
     * MailService constructor.
32
     *
33
     * @param string                     $from
34
     * @param string                     $to
35
     * @param string                     $subject
36
     * @param string                     $view
37
     * @param array                      $params
38
     * @param BaseMailer|MailerInterface $mailer
39
     */
40
    public function __construct($from, $to, $subject, $view, array $params, MailerInterface $mailer)
41
    {
42
        $this->from = $from;
43
        $this->to = $to;
44
        $this->subject = $subject;
45
        $this->view = $view;
46
        $this->params = $params;
47
        $this->mailer = $mailer;
48
        $this->mailer->setViewPath($this->viewPath);
49
        $this->mailer->getView()->theme = Yii::$app->view->theme;
50
    }
51
52
    /**
53
     * @param $name
54
     * @param $value
55
     *
56
     * @return $this
57
     */
58
    public function setViewParam($name, $value)
59
    {
60
        $this->params[$name] = $value;
61
62
        return $this;
63
    }
64
65
    /**
66
     * @return bool
67
     */
68
    public function run()
69
    {
70
        return $this->mailer
71
            ->compose(['html' => $this->view, 'text' => "text/{$this->view}"], $this->params)
72
            ->setFrom($this->from)
73
            ->setTo($this->to)
74
            ->setSubject($this->subject)
75
            ->send();
76
    }
77
}
78