Passed
Push — serialization ( 3b28d7 )
by David
02:25
created

getBlockAndAggregateUnserializer()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
4
namespace TheCodingMachine\CMS\DI;
5
6
7
use Interop\Container\Factories\Alias;
8
use Interop\Container\ServiceProviderInterface;
9
use Psr\Container\ContainerInterface;
10
use TheCodingMachine\CMS\Block\BlockRenderer;
11
use TheCodingMachine\CMS\Block\BlockRendererInterface;
12
use TheCodingMachine\CMS\Serializer\AggregateThemeUnserializer;
13
use TheCodingMachine\CMS\Serializer\BlockUnserializer;
14
use TheCodingMachine\CMS\Serializer\SubThemeUnserializer;
15
use TheCodingMachine\CMS\Serializer\ThemeUnserializerInterface;
16
use TheCodingMachine\CMS\Serializer\TwigThemeUnserializer;
17
use TheCodingMachine\CMS\Theme\AggregateThemeFactory;
18
use TheCodingMachine\CMS\Theme\SubThemeFactory;
19
use TheCodingMachine\CMS\Theme\ThemeFactoryInterface;
20
use TheCodingMachine\CMS\Theme\TwigThemeFactory;
21
22
class CMSUtilsServiceProvider implements ServiceProviderInterface
23
{
24
25
    public function getFactories()
26
    {
27
        $aggregateThemeFactory = null;
28
        $aggregateThemeUnserializer = null;
29
        return [
30 View Code Duplication
            AggregateThemeFactory::class => function(ContainerInterface $container) use (&$aggregateThemeFactory): AggregateThemeFactory
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
31
            {
32
                if ($aggregateThemeFactory !== null) {
33
                    return $aggregateThemeFactory;
34
                }
35
                $aggregateThemeFactory = new AggregateThemeFactory([]);
36
37
                $subThemeFactory = new SubThemeFactory($aggregateThemeFactory);
38
                $twigThemeFactory = new TwigThemeFactory($container->get(\Twig_Environment::class), $container->get(BlockRendererInterface::class));
39
40
                $aggregateThemeFactory->addThemeFactory($twigThemeFactory);
41
                $aggregateThemeFactory->addThemeFactory($subThemeFactory);
42
                return $aggregateThemeFactory;
43
            },
44
            ThemeFactoryInterface::class => new Alias(AggregateThemeFactory::class),
45
            BlockRendererInterface::class => new Alias(BlockRenderer::class),
46 View Code Duplication
            BlockRenderer::class => function(ContainerInterface $container) use (&$aggregateThemeFactory): BlockRenderer
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
47
            {
48
                if ($aggregateThemeFactory === null) {
49
                    $aggregateThemeFactory = new AggregateThemeFactory([]);
50
                }
51
52
                $blockRenderer = new BlockRenderer($aggregateThemeFactory);
53
54
                $subThemeFactory = new SubThemeFactory($aggregateThemeFactory);
55
                $twigThemeFactory = new TwigThemeFactory($container->get(\Twig_Environment::class), $blockRenderer);
56
57
                $aggregateThemeFactory->addThemeFactory($subThemeFactory);
58
                $aggregateThemeFactory->addThemeFactory($twigThemeFactory);
59
60
                return $blockRenderer;
61
            },
62
63
            AggregateThemeUnserializer::class => function(ContainerInterface $container) use (&$aggregateThemeUnserializer): AggregateThemeUnserializer
0 ignored issues
show
Unused Code introduced by
The parameter $container 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

63
            AggregateThemeUnserializer::class => function(/** @scrutinizer ignore-unused */ ContainerInterface $container) use (&$aggregateThemeUnserializer): AggregateThemeUnserializer

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...
64
            {
65
                list($aggregateThemeUnserializer, $blockUnserializer) = $this->getBlockAndAggregateUnserializer();
66
67
68
                $subThemeFactory = new SubThemeUnserializer($blockUnserializer, $aggregateThemeUnserializer);
69
                $twigThemeFactory = new TwigThemeUnserializer();
70
71
                $aggregateThemeUnserializer->addUnserializer('twig', $twigThemeFactory);
72
                $aggregateThemeUnserializer->addUnserializer('subTheme', $subThemeFactory);
73
                return $aggregateThemeUnserializer;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $aggregateThemeUnserializer returns the type null which is incompatible with the type-hinted return TheCodingMachine\CMS\Ser...regateThemeUnserializer.
Loading history...
74
            },
75
            ThemeUnserializerInterface::class => new Alias(AggregateThemeUnserializer::class),
76
            BlockUnserializer::class => function(ContainerInterface $container) use (&$aggregateThemeUnserializer): BlockUnserializer
77
            {
78
                list($aggregateThemeUnserializer, $blockUnserializer) = $this->getBlockAndAggregateUnserializer();
79
80
                // Force resolving the aggregateThemeUnserializer
81
                $container->get(AggregateThemeUnserializer::class);
82
                return $blockUnserializer;
83
            },
84
85
        ];
86
    }
87
88
    private $aggregateThemeUnserializer;
89
    private $blockUnserializer;
90
91
    /**
92
     * This method is needed to break the loop of dependencies.
93
     *
94
     * @return mixed[]
95
     */
96
    private function getBlockAndAggregateUnserializer(): array
97
    {
98
        if ($this->aggregateThemeUnserializer) {
99
            return [$this->aggregateThemeUnserializer, $this->blockUnserializer];
100
        }
101
102
        $this->aggregateThemeUnserializer = new AggregateThemeUnserializer();
103
        $this->blockUnserializer = new BlockUnserializer($this->aggregateThemeUnserializer);
104
105
        return [$this->aggregateThemeUnserializer, $this->blockUnserializer];
106
    }
107
108
    public function getExtensions()
109
    {
110
        return [];
111
    }
112
}
113