Completed
Push — master ( 8ee6a5...a7d283 )
by Jason
04:00
created

PropertyNamingStrategyService::__invoke()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 27
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 27
ccs 0
cts 21
cp 0
rs 8.5806
cc 4
eloc 20
nc 4
nop 1
crap 20
1
<?php
2
3
namespace JDesrosiers\Silex\Provider;
4
5
use JMS\Serializer\Naming\CamelCaseNamingStrategy;
6
use JMS\Serializer\Naming\IdenticalPropertyNamingStrategy;
7
use JMS\Serializer\Naming\PropertyNamingStrategyInterface;
8
use JMS\Serializer\Naming\SerializedNameAnnotationStrategy;
9
use Pimple\Container;
10
use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;
11
12
class PropertyNamingStrategyService
13
{
14
    /**
15
     * Set the serialization naming strategy
16
     *
17
     * @param Container $app
18
     * @return PropertyNamingStrategyInterface
19
     *
20
     * @throws ServiceUnavailableHttpException
21
     */
22
    public function __invoke(Container $app)
23
    {
24
        if ($app["serializer.namingStrategy"] instanceof PropertyNamingStrategyInterface) {
25
            $namingStrategy = $app["serializer.namingStrategy"];
26
        } else {
27
            switch ($app["serializer.namingStrategy"]) {
28
                case "IdenticalProperty":
29
                    $namingStrategy = new IdenticalPropertyNamingStrategy();
30
                    break;
31
                case "CamelCase":
32
                    $separator = $app["serializer.namingStrategy.separator"];
33
                    $lowerCase = $app["serializer.namingStrategy.lowerCase"];
34
                    $namingStrategy = new CamelCaseNamingStrategy($separator, $lowerCase);
35
                    break;
36
                default:
37
                    throw new ServiceUnavailableHttpException(
38
                        null,
39
                        "Unknown property naming strategy '{$app["serializer.namingStrategy"]}'.  " .
40
                        "Allowed values are 'IdenticalProperty' or 'CamelCase'"
41
                    );
42
            }
43
44
            $namingStrategy = new SerializedNameAnnotationStrategy($namingStrategy);
45
        }
46
47
        return $namingStrategy;
48
    }
49
}