SOAPModelAccess::getErrorMessage()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * Basic SOAP Server to access and modify DataObject instances.
4
 * You can enable SOAP access on a DataObject by setting {@link DataObject::$api_access} to true.
5
 * This means that you'll also enable a RESTful API through {@link RestfulServer}.
6
 *
7
 * @todo Test relation methods
8
 */
9
class SOAPModelAccess extends SapphireSoapServer
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
10
{
11
    private static $methods = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $methods is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
12
        'getXML' => array(
13
            'class' => 'string',
14
            'id' => 'int',
15
            'relation' => 'string',
16
            '_returns' => 'string',
17
        ),
18
        'getJSON' => array(
19
            'class' => 'string',
20
            'id' => 'int',
21
            'relation' => 'string',
22
            '_returns' => 'string',
23
        ),
24
        'putXML' => array(
25
            'class' => 'string',
26
            'id' => 'int',
27
            'relation' => 'string',
28
            'data' => 'string',
29
            'username' => 'string',
30
            'password' => 'string',
31
            '_returns' => 'boolean',
32
        ),
33
        'putJSON' => array(
34
            'class' => 'string',
35
            'id' => 'int',
36
            'relation' => 'string',
37
            '_returns' => 'boolean',
38
        ),
39
    );
40
41
    public function Link($action = null)
42
    {
43
        return Controller::join_links('soap/v1/', $action);
44
    }
45
46
    /**
47
     * Used to emulate RESTful GET requests with XML data.
48
     *
49
     * @param string $class
50
     * @param Number $id
51
     * @param string $relation Relation name
52
     *
53
     * @return string
54
     */
55 View Code Duplication
    public function getXML($class, $id, $relation = false, $username = null, $password = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56
    {
57
        $this->authenticate($username, $password);
58
59
        $response = Director::test(
60
            $this->buildRestfulURL($class, $id, $relation, 'xml'),
0 ignored issues
show
Bug introduced by
It seems like $relation defined by parameter $relation on line 55 can also be of type false; however, SOAPModelAccess::buildRestfulURL() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
61
            null,
62
            null,
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a object<Session>|array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
63
            'GET'
64
        );
65
66
        return ($response->isError()) ? $this->getErrorMessage($response) : $response->getBody();
67
    }
68
69
    /**
70
     * Used to emulate RESTful GET requests with JSON data.
71
     *
72
     * @param string $class
73
     * @param Number $id
74
     * @param string $relation Relation name
75
     * @param string $username
76
     * @param string $password
77
     *
78
     * @return string
79
     */
80 View Code Duplication
    public function getJSON($class, $id, $relation = false, $username = null, $password = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
81
    {
82
        $this->authenticate($username, $password);
83
84
        $response = Director::test(
85
            $this->buildRestfulURL($class, $id, $relation, 'json'),
0 ignored issues
show
Bug introduced by
It seems like $relation defined by parameter $relation on line 80 can also be of type false; however, SOAPModelAccess::buildRestfulURL() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
86
            null,
87
            null,
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a object<Session>|array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
88
            'GET'
89
        );
90
91
        return ($response->isError()) ? $this->getErrorMessage($response) : $response->getBody();
92
    }
93
94
    /**
95
     * Used to emulate RESTful POST and PUT requests with XML data.
96
     *
97
     * @param string $class
98
     * @param Number $id
99
     * @param string $relation Relation name
100
     * @param array  $data
101
     * @param string $username
102
     * @param string $password
103
     *
104
     * @return string
105
     */
106 View Code Duplication
    public function putXML($class, $id = false, $relation = false, $data, $username = null, $password = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
107
    {
108
        $this->authenticate($username, $password);
109
110
        $response = Director::test(
111
            $this->buildRestfulURL($class, $id, $relation, 'xml'),
0 ignored issues
show
Bug introduced by
It seems like $id defined by parameter $id on line 106 can also be of type false; however, SOAPModelAccess::buildRestfulURL() does only seem to accept integer|double, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
Bug introduced by
It seems like $relation defined by parameter $relation on line 106 can also be of type false; however, SOAPModelAccess::buildRestfulURL() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
112
            array(),
113
            null,
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a object<Session>|array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
114
            ($id) ? 'PUT' : 'POST',
115
            $data
0 ignored issues
show
Documentation introduced by
$data is of type array, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
116
        );
117
118
        return ($response->isError()) ? $this->getErrorMessage($response) : $response->getBody();
119
    }
120
121
    /**
122
     * Used to emulate RESTful POST and PUT requests with JSON data.
123
     *
124
     * @param string $class
125
     * @param Number $id
126
     * @param string $relation Relation name
127
     * @param array  $data
128
     * @param string $username
129
     * @param string $password
130
     *
131
     * @return string
132
     */
133 View Code Duplication
    public function putJSON($class = false, $id = false, $relation = false, $data, $username = null, $password = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
134
    {
135
        $this->authenticate($username, $password);
136
137
        $response = Director::test(
138
            $this->buildRestfulURL($class, $id, $relation, 'json'),
0 ignored issues
show
Bug introduced by
It seems like $class defined by parameter $class on line 133 can also be of type false; however, SOAPModelAccess::buildRestfulURL() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
Bug introduced by
It seems like $id defined by parameter $id on line 133 can also be of type false; however, SOAPModelAccess::buildRestfulURL() does only seem to accept integer|double, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
Bug introduced by
It seems like $relation defined by parameter $relation on line 133 can also be of type false; however, SOAPModelAccess::buildRestfulURL() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
139
            array(),
140
            null,
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a object<Session>|array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
141
            ($id) ? 'PUT' : 'POST',
142
            $data
0 ignored issues
show
Documentation introduced by
$data is of type array, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
143
        );
144
145
        return ($response->isError()) ? $this->getErrorMessage($response) : $response->getBody();
146
    }
147
148
    /**
149
     * Used to emulate RESTful DELETE requests.
150
     *
151
     * @param string $class
152
     * @param Number $id
153
     * @param string $relation Relation name
154
     * @param string $username
155
     * @param string $password
156
     *
157
     * @return string
158
     */
159 View Code Duplication
    public function deleteXML($class, $id, $relation = false, $username = null, $password = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
160
    {
161
        $this->authenticate($username, $password);
162
163
        $response = Director::test(
164
            $this->buildRestfulURL($class, $id, $relation, 'xml'),
0 ignored issues
show
Bug introduced by
It seems like $relation defined by parameter $relation on line 159 can also be of type false; however, SOAPModelAccess::buildRestfulURL() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
165
            null,
166
            null,
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a object<Session>|array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
167
            'DELETE'
168
        );
169
170
        return ($response->isError()) ? $this->getErrorMessage($response) : $response->getBody();
171
    }
172
173
    /**
174
     * Used to emulate RESTful DELETE requests.
175
     *
176
     * @param string $class
177
     * @param Number $id
178
     * @param string $relation Relation name
179
     * @param string $username
180
     * @param string $password
181
     *
182
     * @return string
183
     */
184 View Code Duplication
    public function deleteJSON($class, $id, $relation = false, $username = null, $password = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
185
    {
186
        $this->authenticate($username, $password);
187
188
        $response = Director::test(
189
            $this->buildRestfulURL($class, $id, $relation, 'json'),
0 ignored issues
show
Bug introduced by
It seems like $relation defined by parameter $relation on line 184 can also be of type false; however, SOAPModelAccess::buildRestfulURL() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
190
            null,
191
            null,
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a object<Session>|array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
192
            'DELETE'
193
        );
194
195
        return ($response->isError()) ? $this->getErrorMessage($response) : $response->getBody();
196
    }
197
198
    /**
199
     * Faking an HTTP Basicauth login in the PHP environment
200
     * that RestfulServer can pick up.
201
     *
202
     * @param string $username Username
203
     * @param string $password Plaintext password
204
     */
205
    protected function authenticate($username, $password)
0 ignored issues
show
Coding Style introduced by
authenticate 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...
206
    {
207
        if (is_string($username)) {
208
            $_SERVER['PHP_AUTH_USER'] = $username;
209
        }
210
        if (is_string($password)) {
211
            $_SERVER['PHP_AUTH_PW'] = $password;
212
        }
213
    }
214
215
    /**
216
     * @param string $class
217
     * @param Number $id
218
     * @param string $relation
219
     * @param string $extension
220
     *
221
     * @return string
222
     */
223
    protected function buildRestfulURL($class, $id, $relation, $extension)
224
    {
225
        $url = "api/v1/{$class}";
226
        if ($id) {
227
            $url .= "/{$id}";
228
        }
229
        if ($relation) {
230
            $url .= "/{$relation}";
231
        }
232
        if ($extension) {
233
            $url .= "/.{$extension}";
234
        }
235
236
        return $url;
237
    }
238
239
    /**
240
     * @param SS_HTTPResponse $response
241
     *
242
     * @return string XML string containing the HTTP error message
243
     */
244
    protected function getErrorMessage($response)
245
    {
246
        return '<error type="authentication" code="'.$response->getStatusCode().'">'.$response->getStatusDescription().'</error>';
247
    }
248
}
249