Passed
Push — master ( 877f87...c048a4 )
by Gabriel
11:44
created

ResponsePayloadFactory   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 8
eloc 22
c 1
b 0
f 1
dl 0
loc 49
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A checkRequestQuery() 0 11 3
A fromController() 0 6 1
A checkDefault() 0 4 2
A new() 0 3 1
A create() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Nip\Controllers\Response\ResponsePayload;
6
7
use Nip\Controllers\Response\ResponsePayload;
8
9
/**
10
 *
11
 */
12
class ResponsePayloadFactory
13
{
14
    protected $controller = null;
15
    protected $request = null;
16
    protected ?ResponsePayload $payload = null;
17
18
    /**
19
     * @param $controller
20
     * @return ResponsePayload
21
     */
22
    public static function fromController($controller): ResponsePayload
23
    {
24
        $factory = new self();
25
        $factory->controller = $controller;
26
        $factory->request = $controller->getRequest();
27
        return $factory->create();
28
    }
29
30
    protected function create(): ResponsePayload
31
    {
32
        $this->payload = $this->new();
33
        $this->checkDefault();
34
        $this->checkRequestQuery();
35
        return $this->payload;
36
    }
37
38
    protected function new(): ResponsePayload
39
    {
40
        return new ResponsePayload();
41
    }
42
43
    protected function checkDefault()
44
    {
45
        if (method_exists($this->controller, 'getView')) {
46
            $this->payload->withDefaultFormat('view');
0 ignored issues
show
Bug introduced by
The method withDefaultFormat() does not exist on null. ( Ignorable by Annotation )

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

46
            $this->payload->/** @scrutinizer ignore-call */ 
47
                            withDefaultFormat('view');

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...
47
        }
48
    }
49
50
    protected function checkRequestQuery()
51
    {
52
        if (!$this->request) {
53
            return;
54
        }
55
        $param = $this->request->get(ResponsePayload::REQUEST_PARAM_FORMAT);
56
        if (empty($param)) {
57
            return;
58
        }
59
60
        $this->payload->withFormat($param);
61
    }
62
}