Completed
Push — master ( ace74a...38a91c )
by Daan van
04:54
created

ConfigurationInstitutionParamConverter   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 0
cbo 5
dl 0
loc 38
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B getInstitutionFromRequest() 0 18 5
A apply() 0 4 1
A supports() 0 5 2
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
 */
18
19
namespace Surfnet\StepupMiddleware\ApiBundle\Request;
20
21
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
22
use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterInterface;
23
use Surfnet\Stepup\Configuration\Value\Institution;
24
use Surfnet\StepupMiddleware\ApiBundle\Exception\BadApiRequestException;
25
use Symfony\Component\HttpFoundation\Request;
26
27
class ConfigurationInstitutionParamConverter implements ParamConverterInterface
28
{
29
    const INSTITUTION = 'institution';
30
31
    public function apply(Request $request, ParamConverter $configuration)
32
    {
33
        $request->attributes->set(self::INSTITUTION, new Institution($this->getInstitutionFromRequest($request)));
34
    }
35
36
    public function supports(ParamConverter $configuration)
37
    {
38
        return $configuration->getName() === self::INSTITUTION
39
            && $configuration->getClass() === 'Surfnet\Stepup\Configuration\Value\Institution';
40
    }
41
42
    /**
43
     * @param Request $request
44
     * @return string
45
     */
46
    private function getInstitutionFromRequest(Request $request)
47
    {
48
        $institution = $request->attributes->get(self::INSTITUTION, false);
49
        $request->attributes->remove(self::INSTITUTION);
50
51
        if (is_string($institution) && !empty($institution)) {
52
            return $institution;
53
        }
54
55
        $institution = $request->query->get(self::INSTITUTION, false);
56
        $request->query->remove(self::INSTITUTION);
57
58
        if (is_string($institution) && !empty($institution)) {
59
            return $institution;
60
        }
61
62
        throw new BadApiRequestException(['This API-call MUST include the institution in the path or query parameters']);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 121 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
63
    }
64
}
65