1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2018 SURFnet B.V. |
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\Doctrine\Type; |
20
|
|
|
|
21
|
|
|
use Doctrine\DBAL\Platforms\AbstractPlatform; |
22
|
|
|
use Doctrine\DBAL\Types\ConversionException; |
23
|
|
|
use Doctrine\DBAL\Types\Type; |
24
|
|
|
use Surfnet\Stepup\Configuration\Value\UseRaaOption; |
25
|
|
|
use Surfnet\Stepup\Configuration\Value\UseRaLocationsOption; |
26
|
|
|
use Surfnet\Stepup\Exception\InvalidArgumentException; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Custom Type for the UseRaaOptionType Value Object |
30
|
|
|
*/ |
31
|
|
View Code Duplication |
class UseRaaOptionType extends Type |
|
|
|
|
32
|
|
|
{ |
33
|
|
|
const NAME = 'stepup_select_raa_option'; |
34
|
|
|
|
35
|
|
|
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform) |
36
|
|
|
{ |
37
|
|
|
return $platform->getJsonTypeDeclarationSQL($fieldDeclaration); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function convertToDatabaseValue($value, AbstractPlatform $platform) |
41
|
|
|
{ |
42
|
|
|
if (is_null($value)) { |
43
|
|
|
return $value; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if (!$value instanceof UseRaaOption) { |
47
|
|
|
throw new ConversionException( |
48
|
|
|
sprintf( |
49
|
|
|
"Encountered illegal location of type %s '%s', expected a UseRaaOption instance", |
50
|
|
|
is_object($value) ? get_class($value) : gettype($value), |
51
|
|
|
is_scalar($value) ? (string) $value : '' |
52
|
|
|
) |
53
|
|
|
); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return json_encode($value); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function convertToPHPValue($value, AbstractPlatform $platform) |
60
|
|
|
{ |
61
|
|
|
if (is_null($value)) { |
62
|
|
|
return $value; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
try { |
66
|
|
|
$useRaLocationsOption = new UseRaaOption(json_decode($value)); |
67
|
|
|
} catch (InvalidArgumentException $e) { |
68
|
|
|
// get nice standard message, so we can throw it keeping the exception chain |
69
|
|
|
$doctrineExceptionMessage = ConversionException::conversionFailed( |
70
|
|
|
$value, |
71
|
|
|
$this->getName() |
72
|
|
|
)->getMessage(); |
73
|
|
|
|
74
|
|
|
throw new ConversionException($doctrineExceptionMessage, 0, $e); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $useRaLocationsOption; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function getName() |
81
|
|
|
{ |
82
|
|
|
return self::NAME; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.