Completed
Push — master ( d2eaaa...3e5621 )
by Derek Stephen
01:47
created

BaseController::sendJsonObjectResponse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 0
cts 12
cp 0
rs 9.7998
c 0
b 0
f 0
cc 2
nc 2
nop 2
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
}