Passed
Branch request-processor (31ada2)
by Iakov
02:59
created

DefaultRequestProcessor::getSingle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Kami\ApiCoreBundle\RequestProcessor;
4
5
6
use Kami\ApiCoreBundle\RequestProcessing\Strategy\RequestProcessorInterface;
7
use Kami\ApiCoreBundle\RequestProcessor\Step\StepInterface;
8
use Symfony\Component\HttpFoundation\Request;
9
10
class DefaultRequestProcessor implements RequestProcessorInterface
11
{
12
    /**
13
     * @var array
14
     */
15
    protected $indexStrategy;
16
17
    /**
18
     * @var array
19
     */
20
    protected $singleStrategy;
21
22
    /**
23
     * @var array
24
     */
25
    protected $filterStrategy;
26
27
    /**
28
     * @var array
29
     */
30
    protected $createStrategy;
31
32
    /**
33
     * @var array
34
     */
35
    protected $updateStrategy;
36
37
    /**
38
     * @var array
39
     */
40
    protected $deleteStrategy;
41
42
    /**
43
     * @var array
44
     */
45
    protected $myStrategy;
46
47
    /**
48
     * @param Request $request
49
     * @return ProcessorResponse|ResponseInterface|\Symfony\Component\HttpFoundation\Response
50
     */
51
    public function getIndex(Request $request)
52
    {
53
        return $this->executeStrategy($this->indexStrategy, $request);
54
    }
55
56
    public function getSingle(Request $request)
57
    {
58
        return $this->executeStrategy($this->singleStrategy, $request);
59
    }
60
61
    public function filter(Request $request)
62
    {
63
        return $this->executeStrategy($this->filterStrategy, $request);
64
    }
65
66
    public function create(Request $request)
67
    {
68
        return $this->executeStrategy($this->createStrategy, $request);
69
    }
70
71
    public function update(Request $request)
72
    {
73
        return $this->executeStrategy($this->updateStrategy, $request);
74
    }
75
76
    public function delete(Request $request)
77
    {
78
        return $this->executeStrategy($this->deleteStrategy, $request);
79
    }
80
81
    public function my(Request $request)
82
    {
83
        return $this->executeStrategy($this->myStrategy, $request);
84
    }
85
86
    protected function executeStrategy(array $strategy, Request $request)
87
    {
88
        $response = new ProcessorResponse($request, []);
89
        $executedSteps = [];
90
91
        foreach ($strategy as $step) { /** @var StepInterface $step */
92
            if (!in_array($step->requiresBefore(), $executedSteps)) {
93
                throw new ProcessingException(
94
                    "Request didn't pass required steps yet. Try to adjust your processing strategy\n" .
95
                    "Required steps are: " . implode(',',  $step->requiresBefore())
96
                );
97
98
            }
99
100
            $response = $step->execute();
101
102
            if(!$response) {
103
                throw new ProcessingException(
104
                    sprintf('RequestProcessor didn\'t receive any response from %s'), get_class($step));
0 ignored issues
show
Bug introduced by
get_class($step) of type string is incompatible with the type integer expected by parameter $code of Kami\ApiCoreBundle\Reque...xception::__construct(). ( Ignorable by Annotation )

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

104
                    sprintf('RequestProcessor didn\'t receive any response from %s'), /** @scrutinizer ignore-type */ get_class($step));
Loading history...
105
            }
106
            $executedSteps[] = $step->getName();
107
108
            if (200 !== $response->getStatus()) {
109
                break;
110
            }
111
        }
112
113
        return $response->toHttpResponse();
114
    }
115
116
117
}