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

PropertyNamingStrategyService   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 4
c 1
b 0
f 1
lcom 0
cbo 4
dl 0
loc 38
ccs 0
cts 21
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B __invoke() 0 27 4
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
}