Completed
Push — master ( 831b77...b56796 )
by Antonio
02:41
created

MailFactory   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 72.21%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 5
dl 0
loc 106
ccs 26
cts 36
cp 0.7221
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A makeWelcomeMailerService() 0 16 1
A makeRecoveryMailerService() 0 14 3
A makeConfirmationMailerService() 0 14 3
A makeMailerService() 0 4 1
A makeReconfirmationMailerService() 0 17 4
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\Factory;
13
14
use Da\User\Model\Token;
15
use Da\User\Model\User;
16
use Da\User\Module;
17
use Da\User\Service\MailService;
18
use Yii;
19
20
class MailFactory
21
{
22
    /**
23
     * @param User $user
24
     *
25
     * @return MailService
26
     */
27 3
    public static function makeWelcomeMailerService(User $user)
28
    {
29
        /** @var Module $module */
30 3
        $module = Yii::$app->getModule('user');
31 3
        $to = $user->email;
32 3
        $from = $module->mailParams['fromEmail'];
33 3
        $subject = $module->mailParams['welcomeMailSubject'];
34
        $params = [
35 3
            'user' => $user,
36
            'token' => null,
37 3
            'module' => $module,
38
            'showPassword' => false,
39
        ];
40
41 3
        return static::makeMailerService($from, $to, $subject, 'welcome', $params);
42
    }
43
44
    /**
45
     * @param string $email
46
     * @param Token  $token
47
     *
48
     * @return MailService
49
     */
50 1
    public static function makeRecoveryMailerService($email, Token $token = null)
51
    {
52
        /** @var Module $module */
53 1
        $module = Yii::$app->getModule('user');
54 1
        $to = $email;
55 1
        $from = $module->mailParams['fromEmail'];
56 1
        $subject = $module->mailParams['recoveryMailSubject'];
57
        $params = [
58 1
            'user' => $token && $token->user ? $token->user : null,
59 1
            'token' => $token,
60
        ];
61
62 1
        return static::makeMailerService($from, $to, $subject, 'recovery', $params);
63
    }
64
65
    /**
66
     * @param User       $user
67
     * @param Token|null $token
68
     *
69
     * @return MailService
70
     */
71 1
    public static function makeConfirmationMailerService(User $user, Token $token = null)
72
    {
73
        /** @var Module $module */
74 1
        $module = Yii::$app->getModule('user');
75 1
        $to = $user->email;
76 1
        $from = $module->mailParams['fromEmail'];
77 1
        $subject = $module->mailParams['confirmationMailSubject'];
78
        $params = [
79 1
            'user' => $token && $token->user ? $token->user : null,
80 1
            'token' => $token,
81
        ];
82
83 1
        return static::makeMailerService($from, $to, $subject, 'recovery', $params);
84
    }
85
86
    /**
87
     * @param User  $user
88
     * @param Token $token
89
     *
90
     * @return MailService
91
     */
92
    public static function makeReconfirmationMailerService(User $user, Token $token)
93
    {
94
        /** @var Module $module */
95
        $module = Yii::$app->getModule('user');
96
        $to = $token->type === Token::TYPE_CONFIRM_NEW_EMAIL
97
            ? $user->unconfirmed_email
98
            : $user->email;
99
100
        $from = $module->mailParams['fromEmail'];
101
        $subject = $module->mailParams['reconfirmationMailSubject'];
102
        $params = [
103
            'user' => $token && $token->user ? $token->user : null,
104
            'token' => $token,
105
        ];
106
107
        return static::makeMailerService($from, $to, $subject, 'recovery', $params);
108
    }
109
110
    /**
111
     * Builds a MailerService.
112
     *
113
     * @param string $from
114
     * @param string $to
115
     * @param string $subject
116
     * @param string $view
117
     * @param array  $params
118
     *
119
     * @return MailService
120
     */
121 5
    public static function makeMailerService($from, $to, $subject, $view, array $params = [])
122
    {
123 5
        return Yii::$container->get(MailService::class, [$from, $to, $subject, $view, $params, Yii::$app->getMailer()]);
124
    }
125
}
126