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

PasswordRecoveryService   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 77.27%

Importance

Changes 0
Metric Value
wmc 5
c 0
b 0
f 0
lcom 1
cbo 6
dl 0
loc 48
ccs 17
cts 22
cp 0.7727
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B run() 0 30 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\Service;
13
14
use Da\User\Contracts\ServiceInterface;
15
use Da\User\Factory\TokenFactory;
16
use Da\User\Model\Token;
17
use Da\User\Model\User;
18
use Da\User\Query\UserQuery;
19
use Exception;
20
use Yii;
21
use yii\log\Logger;
22
23
class PasswordRecoveryService implements ServiceInterface
24
{
25
    protected $query;
26
27
    protected $email;
28
    protected $mailService;
29
    protected $securityHelper;
30
    protected $logger;
31
32 1
    public function __construct($email, MailService $mailService, UserQuery $query, Logger $logger)
33
    {
34 1
        $this->email = $email;
35 1
        $this->mailService = $mailService;
36 1
        $this->query = $query;
37 1
        $this->logger = $logger;
38 1
    }
39
40 1
    public function run()
41
    {
42
        try {
43
            /** @var User $user */
44 1
            $user = $this->query->whereEmail($this->email)->one();
45
46 1
            $token = TokenFactory::makeRecoveryToken($user->id);
47
48 1
            if (!$token) {
49
                return false;
50
            }
51
52 1
            $this->mailService->setViewParam('user', $user);
53 1
            $this->mailService->setViewParam('token', $token);
54 1
            if (!$this->mailService->run()) {
55
                return false;
56
            }
57
58 1
            Yii::$app->session->setFlash(
59 1
                'info',
60 1
                Yii::t('usuario', 'An email has been sent with instructions for resetting your password')
61
            );
62
63 1
            return true;
64
        } catch (Exception $e) {
65
            $this->logger->log($e->getMessage(), Logger::LEVEL_ERROR);
66
67
            return false;
68
        }
69
    }
70
}
71