Completed
Push — master ( 215d99...92be6e )
by Tristan
10:33
created

ParamFetcher::setController()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Nicofuma\SwaggerBundle\FosRest;
4
5
use FOS\RestBundle\Request\ParamFetcherInterface;
6
use Nicofuma\SwaggerBundle\Exception\NoValidatorException;
7
use Nicofuma\SwaggerBundle\Validator\Validator;
8
use Nicofuma\SwaggerBundle\Validator\ValidatorMap;
9
use Symfony\Component\HttpFoundation\RequestStack;
10
11
class ParamFetcher implements ParamFetcherInterface
12
{
13
    /** @var ParamFetcherInterface */
14
    private $decorated;
15
16
    /** @var ValidatorMap */
17
    private $validatorMap;
18
19
    /** @var RequestStack */
20
    private $requestStack;
21
22
    /** @var Validator */
23
    private $currentValidator;
24
25
    /** @var \stdClass[] */
26
    private $currentSchema;
27
28
    private $usingSwagger = false;
29
30 6
    public function __construct(ParamFetcherInterface $decorated, ValidatorMap $validatorMap, RequestStack $requestStack)
31
    {
32 6
        $this->decorated = $decorated;
33 6
        $this->validatorMap = $validatorMap;
34 6
        $this->requestStack = $requestStack;
35 6
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 6
    public function setController($controller)
41
    {
42 6
        $request = $this->requestStack->getCurrentRequest();
43
44
        try {
45 6
            $this->currentValidator = $this->validatorMap->getValidator($request);
0 ignored issues
show
Bug introduced by
It seems like $request defined by $this->requestStack->getCurrentRequest() on line 42 can be null; however, Nicofuma\SwaggerBundle\V...atorMap::getValidator() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
46 5
            $this->currentSchema = null;
0 ignored issues
show
Documentation Bug introduced by
It seems like null of type null is incompatible with the declared type array<integer,object<stdClass>> of property $currentSchema.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
47 5
            $this->usingSwagger = true;
48 6
        } catch (NoValidatorException $e) {
49 1
            $this->usingSwagger = false;
50 1
            $this->decorated->setController($controller);
51
        }
52 6
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 6
    public function get($name, $strict = null)
58
    {
59 6
        if ($this->usingSwagger) {
60 5
            $currentSchema = $this->getCurrentSchema();
61 5
            if (array_key_exists($name, $currentSchema)) {
62 4
                $default = isset($currentSchema[$name]->default) ? $currentSchema[$name]->default : null;
63
64 4
                return $this->requestStack->getCurrentRequest()->query->get($name, $default);
65
            } else {
66 1
                throw new \InvalidArgumentException(sprintf("No swagger definition for parameter '%s'.", $name));
67
            }
68
        } else {
69 1
            return $this->decorated->get($name, $strict);
70
        }
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76 2
    public function all($strict = false)
77
    {
78 2
        if ($this->usingSwagger) {
79 1
            $parameters = [];
80 1
            foreach ($this->getCurrentSchema() as $item) {
81 1
                $parameters[$item->name] = $this->get($item->name);
82 1
            }
83
84 1
            return $parameters;
85
        } else {
86 1
            return $this->decorated->all($strict);
87
        }
88
    }
89
90
    /**
91
     * @return \stdClass[]
92
     */
93 5
    private function getCurrentSchema()
94
    {
95 5
        $request = $this->requestStack->getCurrentRequest();
96 5
        $path = $request->getPathInfo();
97
98 5
        if ($this->currentSchema === null) {
99 5
            $schemaManager = $this->currentValidator->getSchemaManager();
100
101 5
            $this->currentSchema = [];
102 5
            if ($schemaManager->findPathInTemplates($path, $template, $params)) {
103 5
                $currentSchema = $schemaManager->getRequestQueryParameters($template, $request->getMethod());
104
105 5
                foreach ($currentSchema as $keySchema) {
106 5
                    $this->currentSchema[$keySchema->name] = $keySchema;
107 5
                }
108 5
            }
109 5
        }
110
111 5
        return $this->currentSchema;
112
    }
113
}
114