MailFactory   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 97.56%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 5
dl 0
loc 121
ccs 40
cts 41
cp 0.9756
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 11 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\Factory;
13
14
use Da\User\Event\MailEvent;
15
use Da\User\Model\Token;
16
use Da\User\Model\User;
17
use Da\User\Module;
18
use Da\User\Service\MailService;
19
use Yii;
20
use yii\base\InvalidConfigException;
21
22
class MailFactory
23
{
24
    /**
25
     * @param User $user
26
     * @param bool $showPassword
27
     *
28
     * @throws InvalidConfigException
29
     * @return MailService
30
     */
31 7
    public static function makeWelcomeMailerService(User $user, $showPassword = false)
32
    {
33
        /** @var Module $module */
34 7
        $module = Yii::$app->getModule('user');
35 7
        $to = $user->email;
36 7
        $from = $module->mailParams['fromEmail'];
37 7
        $subject = $module->mailParams['welcomeMailSubject'];
38
        $params = [
39 7
            'user' => $user,
40
            'token' => null,
41 7
            'module' => $module,
42 7
            'showPassword' => $showPassword,
43
        ];
44
45 7
        return static::makeMailerService(MailEvent::TYPE_WELCOME, $from, $to, $subject, 'welcome', $params);
46
    }
47
48
    /**
49
     * @param string $email
50
     * @param Token  $token
51
     *
52
     * @throws InvalidConfigException
53
     * @return MailService
54
     */
55 1
    public static function makeRecoveryMailerService($email, Token $token = null)
56
    {
57
        /** @var Module $module */
58 1
        $module = Yii::$app->getModule('user');
59 1
        $to = $email;
60 1
        $from = $module->mailParams['fromEmail'];
61 1
        $subject = $module->mailParams['recoveryMailSubject'];
62
        $params = [
63 1
            'user' => $token && $token->user ? $token->user : null,
64 1
            'token' => $token,
65
        ];
66
67 1
        return static::makeMailerService(MailEvent::TYPE_RECOVERY, $from, $to, $subject, 'recovery', $params);
68
    }
69
70
    /**
71
     * @param User       $user
72
     * @param Token|null $token
73
     *
74
     * @throws InvalidConfigException
75
     * @return MailService
76
     */
77 1
    public static function makeConfirmationMailerService(User $user, Token $token = null)
78
    {
79
        /** @var Module $module */
80 1
        $module = Yii::$app->getModule('user');
81 1
        $to = $user->email;
82 1
        $from = $module->mailParams['fromEmail'];
83 1
        $subject = $module->mailParams['confirmationMailSubject'];
84
        $params = [
85 1
            'user' => $token && $token->user ? $token->user : null,
86 1
            'token' => $token,
87
        ];
88
89 1
        return static::makeMailerService(MailEvent::TYPE_CONFIRM, $from, $to, $subject, 'confirmation', $params);
90
    }
91
92
    /**
93
     * @param User  $user
94
     * @param Token $token
95
     *
96
     * @throws InvalidConfigException
97
     * @return MailService
98
     */
99 1
    public static function makeReconfirmationMailerService(User $user, Token $token)
100
    {
101
        /** @var Module $module */
102 1
        $module = Yii::$app->getModule('user');
103 1
        $to = $token->type === Token::TYPE_CONFIRM_NEW_EMAIL
104 1
            ? $user->unconfirmed_email
105 1
            : $user->email;
106
107 1
        $from = $module->mailParams['fromEmail'];
108 1
        $subject = $module->mailParams['reconfirmationMailSubject'];
109
        $params = [
110 1
            'user' => $token && $token->user ? $token->user : null,
111 1
            'token' => $token,
112
        ];
113
114 1
        return static::makeMailerService(MailEvent::TYPE_RECONFIRM, $from, $to, $subject, 'reconfirmation', $params);
115
    }
116
117
    /**
118
     * Builds a MailerService.
119
     *
120
     * @param string                $type
121
     * @param string|array|\Closure $from
122
     * @param string                $to
123
     * @param string                $subject
124
     * @param string                $view
125
     * @param array                 $params
126
     *
127
     * @throws InvalidConfigException
128
     * @return MailService
129
     *
130
     */
131 10
    public static function makeMailerService($type, $from, $to, $subject, $view, $params = [])
132
    {
133 10
        if ($from instanceof \Closure) {
134
            $from = $from($type);
135
        }
136
        /** @noinspection PhpIncompatibleReturnTypeInspection */
137 10
        return Yii::$container->get(
138 10
            MailService::class,
139 10
            [$type, $from, $to, $subject, $view, $params, Yii::$app->getMailer()]
140
        );
141
    }
142
}
143