ValidateAccountAction   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Test Coverage

Coverage 73.32%

Importance

Changes 0
Metric Value
dl 0
loc 76
ccs 22
cts 30
cp 0.7332
rs 10
c 0
b 0
f 0
wmc 9
lcom 2
cbo 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 5 1
A validates() 0 13 2
A execute() 0 20 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\Traits\CustomUsersTableTrait;
17
use CakeDC\Users\Exception\TokenExpiredException;
18
use CakeDC\Users\Exception\UserAlreadyActiveException;
19
use CakeDC\Users\Exception\UserNotFoundException;
20
use Cake\Utility\Hash;
21
use Cake\Validation\Validator;
22
use Exception;
23
24
/**
25
 * Class ValidateAccountAction
26
 *
27
 * @package CakeDC\Api\Service\Action
28
 */
29
class ValidateAccountAction extends Action
30
{
31
    use CustomUsersTableTrait;
32
33
    /**
34
     * Initialize an action instance
35
     *
36
     * @param array $config Configuration options passed to the constructor
37
     * @return void
38
     */
39
    public function initialize(array $config)
40 1
    {
41
        parent::initialize($config);
42 1
        $this->Auth->allow($this->getName());
43 1
    }
44 1
45
    /**
46
     * Apply validation process.
47
     *
48
     * @return bool
49
     */
50
    public function validates()
51 1
    {
52
        $validator = new Validator();
53 1
        $validator
54
            ->requirePresence('token', 'create')
55 1
            ->notBlank('token');
56 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...
57 1
        if (!empty($errors)) {
58 1
            throw new ValidationException(__('Validation failed'), 0, null, $errors);
59
        }
60
61
        return true;
62 1
    }
63
64
    /**
65
     * Execute action.
66
     *
67
     * @return mixed
68
     * @throws Exception
69
     */
70
    public function execute()
71 1
    {
72
        $data = $this->getData();
73 1
        $token = $data['token'];
74 1
75
        try {
76
            $result = $this->getUsersTable()->validate($token, 'activateUser');
0 ignored issues
show
Bug introduced by
The method validate() does not exist on Cake\ORM\Table. Did you maybe mean validateUnique()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
77 1
            if ($result) {
78 1
                return __d('CakeDC/Api', 'User account validated successfully');
79 1
            } else {
80
                throw new Exception(__d('CakeDC/Api', 'User account could not be validated'), 500);
81
            }
82
        } catch (UserAlreadyActiveException $exception) {
83
            throw new Exception(__d('CakeDC/Api', 'User already active'), 500);
84
        } catch (UserNotFoundException $ex) {
85
            throw new Exception(__d('CakeDC/Api', 'Invalid token or user account already validated'), 500);
86
        } catch (TokenExpiredException $ex) {
87
            throw new Exception(__d('CakeDC/Api', 'Token already expired'), 500);
88
        }
89
    }
90
91
    /**
92
     * Prepare Auth configuration.
93
     *
94
     * @return array
95
     */
96
    protected function _authConfig()
97 1
    {
98
        return Hash::merge(parent::_authConfig(), [
99 1
            'authenticate' => [
100
                'CakeDC/Api.Form' => []
101 1
            ],
102 1
        ]);
103 1
    }
104
}
105