SocialLoginAction::execute()   B
last analyzed

Complexity

Conditions 8
Paths 14

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
cc 8
nc 14
nop 0
dl 0
loc 27
rs 8.4444
c 0
b 0
f 0
ccs 0
cts 21
cp 0
crap 72
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\MissingEmailException;
18
use CakeDC\Users\Exception\TokenExpiredException;
19
use CakeDC\Users\Exception\UserAlreadyActiveException;
20
use CakeDC\Users\Exception\UserNotActiveException;
21
use CakeDC\Users\Exception\UserNotFoundException;
22
use Cake\Datasource\EntityInterface;
23
use Cake\Utility\Hash;
24
use Cake\Validation\Validator;
25
use Exception;
26
27
/**
28
 * Class SocialLoginAction
29
 *
30
 * @package CakeDC\Api\Service\Action
31
 */
32
class SocialLoginAction extends Action
33
{
34
35
    use CustomUsersTableTrait;
36
37
    /**
38
     * Initialize an action instance
39
     *
40
     * @param array $config Configuration options passed to the constructor
41
     * @return void
42
     */
43
    public function initialize(array $config)
44
    {
45
        parent::initialize($config);
46
        $this->Auth->allow($this->getName());
47
    }
48
49
    /**
50
     * Apply validation process.
51
     *
52
     * @return bool
53
     */
54
    public function validates()
55
    {
56
        $validator = new Validator();
57
        $validator
58
            ->requirePresence('data', 'create');
59
        $validator
60
            ->requirePresence('options', 'create');
61
        $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...
62
        if (!empty($errors)) {
63
            throw new ValidationException(__('Validation failed'), 0, null, $errors);
64
        }
65
66
        return true;
67
    }
68
69
    /**
70
     * Execute action.
71
     *
72
     * @return mixed
73
     * @throws Exception
74
     */
75
    public function execute()
76
    {
77
        $input = $this->getData();
78
        $data = $input['data'];
79
        $options = $input['options'];
80
81
        try {
82
            $result = $this->getUsersTable()->socialLogin($data, $options);
83
            if ($result instanceof EntityInterface) {
84
                $result = $result->toArray();
85
            }
86
87
            return $result;
88
        } catch (UserNotActiveException $ex) {
89
            throw new Exception(__d('CakeDC/Api', 'User account has not validated yet'), 501);
90
        } catch (UserAlreadyActiveException $ex) {
91
            throw new Exception($ex->getMessage(), 502);
92
        } catch (UserNotFoundException $ex) {
93
            throw new Exception(__d('CakeDC/Api', 'Invalid token or user account already validated'), 503);
94
        } catch (TokenExpiredException $ex) {
95
            throw new Exception($ex->getMessage(), 504);
96
        } catch (MissingEmailException $ex) {
97
            throw new Exception($ex->getMessage(), 505);
98
        } catch (\Exception $ex) {
99
            throw new Exception($ex->getMessage(), 500);
100
        }
101
    }
102
103
    /**
104
     * Prepare Auth configuration.
105
     *
106
     * @return array
107
     */
108
    protected function _authConfig()
109
    {
110
        return Hash::merge(parent::_authConfig(), [
111
            'authenticate' => [
112
                'CakeDC/Api.Form' => []
113
            ],
114
        ]);
115
    }
116
}
117