Passed
Push — develop ( d78ff9...fceade )
by Pablo
02:33
created

Base::getRequestBody()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * This class is the base class of all API controllers.
5
 *
6
 * @package     Nails
7
 * @subpackage  module-api
8
 * @category    Controller
9
 * @author      Nails Dev Team
10
 * @link
11
 */
12
13
namespace Nails\Api\Controller;
14
15
// --------------------------------------------------------------------------
16
17
use ApiRouter;
18
use Nails\Api\Events;
19
use Nails\Common\Exception\FactoryException;
20
use Nails\Common\Exception\NailsException;
21
use Nails\Common\Service\Event;
22
use Nails\Common\Service\Input;
23
use Nails\Factory;
24
25
// --------------------------------------------------------------------------
26
27
/**
28
 * Allow the app to add functionality, if needed
29
 */
30
if (class_exists('\App\Api\Controller\Base')) {
31
    abstract class BaseMiddle extends \App\Api\Controller\Base
0 ignored issues
show
Bug introduced by Pablo de la Peña
The type App\Api\Controller\Base was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
32
    {
33
    }
34
} else {
35
    abstract class BaseMiddle
36
    {
37
        public function __construct()
38
        {
39
        }
40
    }
41
}
42
43
// --------------------------------------------------------------------------
44
45
/**
46
 * Class Base
47
 *
48
 * @package Nails\Api\Controller
49
 */
50
abstract class Base extends BaseMiddle
51
{
52
    /**
53
     * Require the user be authenticated to use any endpoint
54
     *
55
     * @var bool
56
     */
57
    const REQUIRE_AUTH = false;
58
59
    /**
60
     * Require the user's access token to have a particular scope
61
     *
62
     * @var string
63
     */
64
    const REQUIRE_SCOPE = null;
65
66
    // --------------------------------------------------------------------------
67
68
    /**
69
     * The Api Router instance
70
     *
71
     * @var ApiRouter
72
     */
73
    protected $oApiRouter;
74
75
    // --------------------------------------------------------------------------
76
77
    /**
78
     * Base constructor.
79
     *
80
     * @param ApiRouter $oApiRouter The ApiRouter controller
81
     *
82
     * @throws FactoryException
83
     * @throws NailsException
84
     * @throws \ReflectionException
85
     */
86
    public function __construct(ApiRouter $oApiRouter)
87
    {
88
        parent::__construct();
89
90
        /** @var Event $oEventService */
91
        $oEventService = Factory::service('Event');
92
93
        //  Call the API:STARTUP event, API is constructing
94
        $oEventService->trigger(Events::API_STARTUP, Events::getEventNamespace());
95
96
        // --------------------------------------------------------------------------
97
98
        $this->oApiRouter = $oApiRouter;
99
100
        // --------------------------------------------------------------------------
101
102
        //  Call the API:READY event, API is all geared up and ready to go
103
        $oEventService->trigger(Events::API_READY, Events::getEventNamespace());
104
    }
105
106
    // --------------------------------------------------------------------------
107
108
    /**
109
     * Writes a line to the log
110
     *
111
     * @param string $sLine The line to write
112
     */
113
    protected function writeLog($sLine)
114
    {
115
        $this->oApiRouter->writeLog($sLine);
116
    }
117
118
    // --------------------------------------------------------------------------
119
120
    /**
121
     * Whether the user is authenticated.
122
     *
123
     * @param string $sHttpMethod The HTTP Method protocol being used
124
     * @param string $sMethod     The controller method being executed
125
     *
126
     * @return bool|array       Boolean true or false. Can also return an array
127
     *                             with two elements (status and error) which
128
     *                             will customise the response code and message.
129
     */
130
    public static function isAuthenticated($sHttpMethod = '', $sMethod = '')
0 ignored issues
show
Unused Code introduced by Pablo de la Peña
The parameter $sHttpMethod is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

130
    public static function isAuthenticated(/** @scrutinizer ignore-unused */ $sHttpMethod = '', $sMethod = '')

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by Pablo de la Peña
The parameter $sMethod is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

130
    public static function isAuthenticated($sHttpMethod = '', /** @scrutinizer ignore-unused */ $sMethod = '')

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
131
    {
132
        return static::REQUIRE_AUTH && !isLoggedIn() ? false : true;
133
    }
134
135
    // --------------------------------------------------------------------------
136
137
    /**
138
     * Returns the raw request body
139
     *
140
     * @return string
141
     */
142
    protected function getRequestBody(): string
143
    {
144
        return stream_get_contents(fopen('php://input', 'r'));
145
    }
146
147
    // --------------------------------------------------------------------------
148
149
    /**
150
     * Gets the request data from the POST vars, falling back to the request body
151
     *
152
     * @return array
153
     * @throws FactoryException
154
     */
155
    protected function getRequestData(): array
156
    {
157
        /**
158
         * First check the $_POST superglobal, if that's empty then fall back to
159
         * the body of the request assuming it is JSON.
160
         */
161
        /** @var Input $oInput */
162
        $oInput = Factory::service('Input');
163
        $aData  = $oInput->post();
164
165
        if (empty($aData)) {
166
            $sData = $this->getRequestBody();
167
            $aData = json_decode($sData, JSON_OBJECT_AS_ARRAY) ?: [];
0 ignored issues
show
Bug introduced by Pablo de la Peña
Nails\Api\Controller\JSON_OBJECT_AS_ARRAY of type integer is incompatible with the type boolean|null expected by parameter $associative of json_decode(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

167
            $aData = json_decode($sData, /** @scrutinizer ignore-type */ JSON_OBJECT_AS_ARRAY) ?: [];
Loading history...
168
        }
169
170
        return $aData;
171
    }
172
}
173