MailAwareTrait   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 58.33%

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 3
dl 0
loc 29
ccs 7
cts 12
cp 0.5833
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A sendMail() 0 16 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\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