|
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']); |
|
|
|
|
|
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
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.