ResetPasswordRequestAction   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Test Coverage

Coverage 75.68%

Importance

Changes 0
Metric Value
dl 0
loc 83
ccs 28
cts 37
cp 0.7568
rs 10
c 0
b 0
f 0
wmc 9
lcom 2
cbo 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 5 1
A validates() 0 13 2
A execute() 0 27 5
A _authConfig() 0 8 1
1
<?php
2
/**
3
 * Copyright 2016 - 2018, Cake Development Corporation (http://cakedc.com)
4
 *
5
 * Licensed under The MIT License
6
 * Redistributions of files must retain the above copyright notice.
7
 *
8
 * @copyright Copyright 2016 - 2018, Cake Development Corporation (http://cakedc.com)
9
 * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
10
 */
11
12
namespace CakeDC\Api\Service\Action\Auth;
13
14
use CakeDC\Api\Exception\ValidationException;
15
use CakeDC\Api\Service\Action\Action;
16
use CakeDC\Users\Controller\Component\UsersAuthComponent;
17
use CakeDC\Users\Controller\Traits\CustomUsersTableTrait;
18
use CakeDC\Users\Exception\UserNotActiveException;
19
use CakeDC\Users\Exception\UserNotFoundException;
20
use Cake\Core\Configure;
21
use Cake\Utility\Hash;
22
use Cake\Validation\Validator;
23
use Exception;
24
25
/**
26
 * Class ResetPasswordAction
27
 *
28
 * @package CakeDC\Api\Service\Action
29
 */
30
class ResetPasswordRequestAction extends Action
31
{
32
    use CustomUsersTableTrait;
33
34
    /**
35
     * Initialize an action instance
36
     *
37
     * @param array $config Configuration options passed to the constructor
38
     * @return void
39
     */
40
    public function initialize(array $config)
41 2
    {
42
        parent::initialize($config);
43 2
        $this->Auth->allow($this->getName());
44 2
    }
45 2
46
    /**
47
     * Apply validation process.
48
     *
49
     * @return bool
50
     */
51
    public function validates()
52 2
    {
53
        $validator = new Validator();
54 2
        $validator
55
            ->requirePresence('reference', 'create')
56 2
            ->notBlank('reference');
57 2
        $errors = $validator->errors($this->getData());
0 ignored issues
show
Deprecated Code introduced by
The method Cake\Validation\Validator::errors() has been deprecated with message: 3.9.0 Renamed to validate()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
58 2
        if (!empty($errors)) {
59 2
            throw new ValidationException(__('Validation failed'), 0, null, $errors);
60
        }
61
62
        return true;
63 2
    }
64
65
    /**
66
     * Execute action.
67
     *
68
     * @return mixed
69
     * @throws Exception
70
     */
71
    public function execute()
72 2
    {
73
        $data = $this->getData();
74 2
        $reference = $data['reference'];
75 2
        try {
76
            $resetUser = $this->getUsersTable()->resetToken($reference, [
77 2
                'expiration' => Configure::read('Users.Token.expiration'),
78 2
                'checkActive' => false,
79 2
                'sendEmail' => true,
80 2
                'ensureActive' => Configure::read('Users.Registration.ensureActive')
81 2
            ]);
82 2
            if ($resetUser) {
83 2
                $message = __d('CakeDC/Api', 'Please check your email to continue with password reset process');
84 2
85
                return $message;
86 2
            } else {
87
                $message = __d('CakeDC/Api', 'The password token could not be generated. Please try again');
88
                throw new Exception($message, 500);
89
            }
90
        } catch (UserNotFoundException $exception) {
91
            throw new Exception(__d('CakeDC/Api', 'User {0} was not found', $reference), 404);
92
        } catch (UserNotActiveException $exception) {
93
            throw new Exception(__d('CakeDC/Api', 'The user is not active'), 404);
94
        } catch (Exception $exception) {
95
            throw new Exception(__d('CakeDC/Api', 'Token could not be reset'), 500);
96
        }
97
    }
98
99
    /**
100
     * Prepare Auth configuration.
101
     *
102
     * @return array
103
     */
104
    protected function _authConfig()
105 2
    {
106
        return Hash::merge(parent::_authConfig(), [
107 2
            'authenticate' => [
108
                'CakeDC/Api.Form' => []
109 2
            ],
110 2
        ]);
111 2
    }
112
}
113