Passed
Push — master ( 17b9f0...6c0286 )
by Mr
02:04
created

JMSSerializerServiceProvisioner::provision()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 12
rs 10
1
<?php declare(strict_types=1);
2
/**
3
 * This file is part of the daikon-cqrs/boot project.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Daikon\Boot\Service\Provisioner;
10
11
use Auryn\Injector;
12
use Daikon\Boot\Middleware\Action\SerializerInterface;
13
use Daikon\Boot\Service\ServiceDefinitionInterface;
14
use Daikon\Config\ConfigProviderInterface;
15
use Daikon\ValueObject\ValueObjectInterface;
16
use Doctrine\Common\Annotations\SimpleAnnotationReader;
0 ignored issues
show
Bug introduced by
The type Doctrine\Common\Annotations\SimpleAnnotationReader was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use JMS\Serializer\GraphNavigatorInterface;
0 ignored issues
show
Bug introduced by
The type JMS\Serializer\GraphNavigatorInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use JMS\Serializer\Handler\HandlerRegistry;
0 ignored issues
show
Bug introduced by
The type JMS\Serializer\Handler\HandlerRegistry was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
use JMS\Serializer\Naming\IdenticalPropertyNamingStrategy;
0 ignored issues
show
Bug introduced by
The type JMS\Serializer\Naming\Id...lPropertyNamingStrategy was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
use JMS\Serializer\SerializerBuilder;
0 ignored issues
show
Bug introduced by
The type JMS\Serializer\SerializerBuilder was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
22
class JMSSerializerServiceProvisioner implements ProvisionerInterface
23
{
24
    public function provision(
25
        Injector $injector,
26
        ConfigProviderInterface $configProvider,
27
        ServiceDefinitionInterface $serviceDefinition
28
    ): void {
29
        $className = $serviceDefinition->getServiceClass();
30
        $settings = $serviceDefinition->getSettings();
31
32
        $injector
33
            ->alias(SerializerInterface::class, $className)
34
            ->share($className)
35
            ->delegate($className, $this->factory($className, $configProvider, $settings));
36
    }
37
38
    private function factory(
39
        string $className,
40
        ConfigProviderInterface $configProvider,
41
        array $settings
42
    ): callable {
43
        return function () use ($className, $configProvider, $settings): SerializerInterface {
44
            return new $className(SerializerBuilder::create()
45
                ->setPropertyNamingStrategy(new IdenticalPropertyNamingStrategy)
46
                ->setAnnotationReader(new SimpleAnnotationReader)
47
                ->setMetadataDirs((array)($settings['metadata_dirs'] ?? []))
48
                ->configureHandlers(function (HandlerRegistry $registry): void {
49
                    $registry->registerHandler(
50
                        GraphNavigatorInterface::DIRECTION_SERIALIZATION,
51
                        'ValueObject',
52
                        'json',
53
                        /**
54
                         * @param mixed $visitor
55
                         * @return mixed
56
                         */
57
                        fn($visitor, ValueObjectInterface $valueObject, array $type) => $valueObject->toNative()
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

57
                        fn($visitor, ValueObjectInterface $valueObject, /** @scrutinizer ignore-unused */ array $type) => $valueObject->toNative()

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
58
                    );
59
                })
60
                ->setCacheDir($configProvider->get('app.cache_dir'))
61
                ->setDebug($configProvider->get('app.debug'))
62
                ->build());
63
        };
64
    }
65
}
66