Completed
Push — master ( 295182...440812 )
by Derek Stephen
01:52
created

BaseController::httpMethodCheck()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 5
cp 0
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
namespace App\Controller;
4
5
use Bone\Mvc\Controller;
6
use InvalidArgumentException;
7
use JMS\Serializer\Serializer;
8
use JMS\Serializer\SerializerBuilder;
9
use Psr\Http\Message\ServerRequestInterface;
10
11
class BaseController extends Controller
12
{
13
    /** @var Serializer */
14
    protected $serializer;
15
16 4
    public function __construct(ServerRequestInterface $request)
17
    {
18 4
        parent::__construct($request);
19 4
        $this->serializer = SerializerBuilder::create()->build();
20 4
        $this->disableView();
21 4
        $this->disableLayout();
22 4
    }
23
24
    /**
25
     * @param $object
26
     * @param int $statusCode
27
     */
28
    public function sendJsonObjectResponse($object, $statusCode = 200)
29
    {
30
        if (!is_object($object)) {
31
            throw new InvalidArgumentException('You must pass an object.');
32
        }
33
        $this->disableLayout();
34
        $this->disableView();
35
        $this->setHeader('Cache-Control', 'no-cache, must-revalidate');
36
        $this->setHeader('Expires','Mon, 26 Jul 1997 05:00:00 GMT');
37
        $this->setHeader('Content-Type','application/json');
38
        $json = $this->serializer->serialize($object, 'json');
39
        $this->setBody($json);
40
        $this->setStatusCode($statusCode);
0 ignored issues
show
Bug introduced by
The method setStatusCode() does not seem to exist on object<App\Controller\BaseController>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
41
    }
42
43
    protected function httpMethodCheck($method)
44
    {
45
        if ($this->getRequest()->getMethod() !== $method) {
46
            $this->sendJsonResponse(['error' => 'Method not allowed'], 405);
0 ignored issues
show
Unused Code introduced by
The call to BaseController::sendJsonResponse() has too many arguments starting with 405.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
47
48
            return false;
49
        }
50
51
        return true;
52
    }
53
}