Completed
Push — master ( 8ec403...fa21f3 )
by Evgeny
03:18
created

ConfigTrait::_initializeRequest()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 16
nc 16
nop 3
dl 0
loc 23
rs 8.5906
c 0
b 0
f 0
1
<?php
2
/**
3
 * Copyright 2016, 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, 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\Network\Request;
16
use Cake\Network\Response;
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)));
0 ignored issues
show
Bug introduced by
It seems like $overwrite ? $extension ...lt, (array) $extension) can also be of type string; however, Cake\Utility\Hash::insert() does only seem to accept array|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
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 = [])
0 ignored issues
show
Coding Style introduced by
_initializeRequest uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
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 Request($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', ['redirect']);
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