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

BaseController   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 33.33%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 3
dl 0
loc 32
ccs 6
cts 18
cp 0.3333
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A sendJsonObjectResponse() 0 14 2
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
}