ConfigurationInstitutionValueResolver   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 38
rs 10
c 0
b 0
f 0
wmc 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B getInstitutionFromRequest() 0 17 7
A resolve() 0 9 3
1
<?php
2
3
/**
4
 * Copyright 2014 SURFnet bv
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
0 ignored issues
show
Coding Style introduced by
Missing @link tag in file comment
Loading history...
18
19
namespace Surfnet\StepupMiddleware\ApiBundle\Request;
20
21
use Surfnet\Stepup\Configuration\Value\Institution;
22
use Surfnet\StepupMiddleware\ApiBundle\Exception\BadApiRequestException;
23
use Symfony\Component\HttpFoundation\Request;
24
use Symfony\Component\HttpKernel\Controller\ValueResolverInterface;
25
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
26
27
class ConfigurationInstitutionValueResolver implements ValueResolverInterface
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class ConfigurationInstitutionValueResolver
Loading history...
28
{
29
    public const INSTITUTION = 'institution';
30
31
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $request should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $argument should have a doc-comment as per coding-style.
Loading history...
32
     * @return Institution[]
33
     */
34
    public function resolve(Request $request, ArgumentMetadata $argument): iterable
35
    {
36
        $argumentType = $argument->getType();
37
        if (!$argumentType || $argumentType !== Institution::class
38
        ) {
39
            return [];
40
        }
41
42
        return [new Institution($this->getInstitutionFromRequest($request))];
43
    }
44
45
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $request should have a doc-comment as per coding-style.
Loading history...
46
     * @return string
47
     */
48
    private function getInstitutionFromRequest(Request $request): string
0 ignored issues
show
Coding Style introduced by
Private method name "ConfigurationInstitutionValueResolver::getInstitutionFromRequest" must be prefixed with an underscore
Loading history...
49
    {
50
        $institution = $request->attributes->get(self::INSTITUTION);
51
        $request->attributes->remove(self::INSTITUTION);
52
53
        if (is_string($institution) && ($institution !== '' && $institution !== '0')) {
54
            return $institution;
55
        }
56
57
        $institution = $request->query->get(self::INSTITUTION);
58
        $request->query->remove(self::INSTITUTION);
59
60
        if (is_string($institution) && ($institution !== '' && $institution !== '0')) {
61
            return $institution;
62
        }
63
64
        throw new BadApiRequestException(['This API-call MUST include the institution in the path or query parameters'],);
65
    }
66
}
67