Completed
Pull Request — master (#251)
by
unknown
02:45
created

PasswordRecoveryService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 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\Service;
13
14
use Da\User\Contracts\ServiceInterface;
15
use Da\User\Factory\TokenFactory;
16
use Da\User\Model\User;
17
use Da\User\Query\UserQuery;
18
use Da\User\Traits\MailAwareTrait;
19
use Da\User\Traits\ModuleAwareTrait;
20
use Exception;
21
use Yii;
22
23
class PasswordRecoveryService implements ServiceInterface
24
{
25
    use MailAwareTrait;
26
    use ModuleAwareTrait;
27
28
    protected $query;
29
30
    protected $email;
31
    protected $mailService;
32
    protected $securityHelper;
33
34 1
    public function __construct($email, MailService $mailService, UserQuery $query)
35
    {
36 1
        $this->email = $email;
37 1
        $this->mailService = $mailService;
38 1
        $this->query = $query;
39 1
    }
40
41 1
    public function run()
42
    {
43
        try {
44
            /** @var User $user */
45 1
            $user = $this->query->whereEmail($this->email)->one();
46
47 1
            if ($user === null) {
48
                throw new \RuntimeException('User not found.');
49
            }
50
51 1
            $token = TokenFactory::makeRecoveryToken($user->id);
52
53 1
            if (!$token) {
54
                return false;
55
            }
56
57 1
            $this->mailService->setViewParam('user', $user);
58 1
            $this->mailService->setViewParam('token', $token);
59 1
            if (!$this->sendMail($user)) {
60
                return false;
61
            }
62
63 1
            if ($this->getModule()->enableFlashMessages == true) {
64 1
                Yii::$app->session->setFlash(
65 1
                    'info',
66 1
                    Yii::t('usuario', 'An email has been sent with instructions for resetting your password')
67
                );
68
            }
69
70 1
            return true;
71
        } catch (Exception $e) {
72
            Yii::error($e->getMessage(), 'usuario');
73
74
            return false;
75
        }
76
    }
77
}
78