ConfigTrait::_tokenAccess()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 27
rs 9.488
c 0
b 0
f 0
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\Test;
13
14
use Cake\Core\Configure;
15
use Cake\Http\Response;
16
use Cake\Http\ServerRequest;
17
use Cake\Utility\Hash;
18
19
/**
20
 * Class ConfigTrait
21
 *
22
 * @package CakeDC\Api\Test
23
 */
24
trait ConfigTrait
25
{
26
27
    /**
28
     * Configure public auth access
29
     */
30
    protected function _publicAccess()
31
    {
32
        $config = Configure::read('Test.Api');
33
        $config['Auth'] = [
34
            'allow' => '*'
35
        ];
36
        Configure::write('Test.Api', $config);
37
    }
38
39
    /**
40
     * Configure token auth access
41
     */
42
    protected function _authAccess()
43
    {
44
        $config = (array)Configure::read('Test.Api');
45
        $auth = [
46
            'authorize' => [
47
                'CakeDC/Api.Crud' => []
48
            ],
49
            'authenticate' => [
50
                'all' => [
51
                    'finder' => 'active',
52
                ],
53
                'CakeDC/Api.Form' => [
54
                    'userModel' => 'CakeDC/Users.Users'
55
                ]
56
            ],
57
        ];
58
        $path = 'Service.default.Action.default.Auth';
59
        $config = Hash::insert($config, $path, $auth);
60
        Configure::write('Test.Api', $config);
61
    }
62
63
    /**
64
     * Configure token auth access
65
     */
66
    protected function _tokenAccess()
67
    {
68
        $config = (array)Configure::read('Test.Api');
69
        $config['Auth'] = [
70
            'Crud' => [
71
                'default' => 'allow'
72
            ],
73
        ];
74
75
        $auth = [
76
            'authorize' => [
77
                'CakeDC/Api.Crud' => []
78
            ],
79
            'authenticate' => [
80
                'all' => [
81
                    'finder' => 'auth',
82
                ],
83
                'CakeDC/Api.Token' => [
84
                    'require_ssl' => false,
85
                    'table' => 'CakeDC/Users.Users',
86
                ]
87
            ],
88
        ];
89
        $path = 'Service.default.Action.default.Auth';
90
        $config = Hash::insert($config, $path, $auth);
91
        Configure::write('Test.Api', $config);
92
    }
93
94
    /**
95
     * Insert api options into specific path.
96
     *
97
     * @param string $path Setting path.
98
     * @param mixed $options An options.
99
     * @return void
100
     */
101
    protected function _addSettingByPath($path, $options)
102
    {
103
        $config = (array)Configure::read('Test.Api');
104
        $config = Hash::insert($config, $path, $options);
105
        Configure::write('Test.Api', $config);
106
    }
107
108
    /**
109
     * Add default extensions into configuration.
110
     *
111
     * @param array|string $extension
112
     * @param bool $overwrite Owerwrite flag.
113
     */
114
    protected function _loadDefaultExtensions($extension, $overwrite = false)
115
    {
116
        $config = (array)Configure::read('Test.Api');
117
        $path = 'Service.default.Action.default.Extension';
118
        $default = (array)Hash::get($config, $path);
119
        $config = Hash::insert($config, $path, ($overwrite ? $extension : array_merge($default, (array)$extension)));
120
        Configure::write('Test.Api', $config);
121
    }
122
123
    /**
124
     * Performs controller initialization.
125
     *
126
     * @param array $requestOptions Request options.
127
     * @param string $method Http method.
128
     * @param array $options Options.
129
     * @return void
130
     */
131
    protected function _initializeRequest($requestOptions = [], $method = 'GET', $options = [])
132
    {
133
        $_SERVER['REQUEST_METHOD'] = $method;
134
        if (empty($requestOptions['params'])) {
135
            $requestOptions['params'] = [];
136
        }
137
        if (empty($requestOptions['params']['service'])) {
138
            $requestOptions['params']['service'] = 'articles';
139
        }
140
        if (empty($requestOptions['params']['pass'])) {
141
            $requestOptions['params']['pass'] = [];
142
        }
143
        $this->request = new ServerRequest($requestOptions);
0 ignored issues
show
Bug introduced by
The property request does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
144
145
        if (empty($options['response'])) {
146
            $this->response = new Response();
0 ignored issues
show
Bug introduced by
The property response does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
147
        } else {
148
            $this->response = $options['response'];
149
        }
150
        $this->Controller = $this->createMock('Cake\Controller\Controller');
0 ignored issues
show
Bug introduced by
The property Controller does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Bug introduced by
It seems like createMock() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
151
        $this->Controller->request = $this->request;
152
        $this->Controller->response = $this->response;
153
    }
154
}
155