MailAwareTrait::sendMail()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.2894

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 7
cts 12
cp 0.5833
rs 9.7333
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2.2894
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\Traits;
13
14
use Da\User\Event\MailEvent;
15
use Da\User\Model\User;
16
use Da\User\Service\MailService;
17
use Exception;
18
use Yii;
19
use yii\base\InvalidConfigException;
20
21
/**
22
 * @property MailService $mailService
23
 */
24
trait MailAwareTrait
25
{
26
    use ContainerAwareTrait;
27
28
    /**
29
     * Sends a mailer
30
     *
31
     * @param User $user
32
     *
33
     * @throws InvalidConfigException
34
     * @return bool
35
     */
36 9
    protected function sendMail(User $user)
37
    {
38 9
        $type = $this->mailService->getType();
39 9
        $event = $this->make(MailEvent::class, [$type, $user, $this->mailService, null]);
40 9
        $user->trigger(MailEvent::EVENT_BEFORE_SEND_MAIL, $event);
41
        try {
42 9
            $this->mailService->run();
43
        } catch (Exception $e) {
44
            $event = $this->make(MailEvent::class, [$type, $user, $this->mailService, $e]);
45
            Yii::error($e->getMessage(), 'usuario');
46
            $user->trigger(MailEvent::EVENT_AFTER_SEND_MAIL, $event);
47
            return false;
48
        }
49 9
        $user->trigger(MailEvent::EVENT_AFTER_SEND_MAIL, $event);
50 9
        return true;
51
    }
52
}
53