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

MailFactory   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

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