ValidateAccountRequestAction::execute()   A
last analyzed

Complexity

Conditions 5
Paths 10

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 7.0935

Importance

Changes 0
Metric Value
cc 5
nc 10
nop 0
dl 0
loc 23
rs 9.2408
c 0
b 0
f 0
ccs 9
cts 16
cp 0.5625
crap 7.0935
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\UserAlreadyActiveException;
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 ValidateAccountRequestAction
27
 *
28
 * @package CakeDC\Api\Service\Action
29
 */
30
class ValidateAccountRequestAction 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 1
    {
42
        parent::initialize($config);
43 1
        $this->Auth->allow($this->getName());
44 1
    }
45 1
46
    /**
47
     * Apply validation process.
48
     *
49
     * @return bool
50
     */
51
    public function validates()
52 1
    {
53
        $validator = new Validator();
54 1
        $validator
55
            ->requirePresence('reference', 'create')
56 1
            ->notBlank('reference');
57 1
        $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 1
        if (!empty($errors)) {
59 1
            throw new ValidationException(__('Validation failed'), 0, null, $errors);
60
        }
61
62
        return true;
63 1
    }
64
65
    /**
66
     * Execute action.
67
     *
68
     * @return mixed
69
     * @throws Exception
70
     */
71
    public function execute()
72 1
    {
73
        $data = $this->getData();
74 1
        $reference = $data['reference'];
75 1
        try {
76
            if ($this->getUsersTable()->resetToken($reference, [
77 1
                'expiration' => Configure::read('Users.Token.expiration'),
78 1
                'checkActive' => true,
79 1
                'sendEmail' => true,
80 1
                'emailTemplate' => 'CakeDC/Users.validation'
81
            ])) {
82 1
                return __d('CakeDC/Api', 'Token has been reset successfully. Please check your email.');
83 1
            } else {
84
                throw new Exception(__d('CakeDC/Api', 'Token could not be reset'), 500);
85
            }
86
        } catch (UserNotFoundException $ex) {
87
            throw new Exception(__d('CakeDC/Api', 'User {0} was not found', $reference), 404);
88
        } catch (UserAlreadyActiveException $ex) {
89
            throw new Exception(__d('CakeDC/Api', 'User {0} is already active', $reference), 404);
90
        } catch (Exception $ex) {
91
            throw new Exception(__d('CakeDC/Api', 'Token could not be reset'), 500);
92
        }
93
    }
94
95
    /**
96
     * Prepare Auth configuration.
97
     *
98
     * @return array
99
     */
100
    protected function _authConfig()
101 1
    {
102
        return Hash::merge(parent::_authConfig(), [
103 1
            'authenticate' => [
104
                'CakeDC/Api.Form' => []
105 1
            ],
106 1
        ]);
107 1
    }
108
}
109