Completed
Push — master ( 07ed5b...7116f3 )
by Beñat
15s
created

SqlServicesPass   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 0
cbo 3
dl 0
loc 55
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B process() 0 49 4
1
<?php
2
3
/*
4
 * This file is part of the BenGorFile package.
5
 *
6
 * (c) Beñat Espiña <[email protected]>
7
 * (c) Gorka Laucirica <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace BenGorFile\FileBundle\DependencyInjection\Compiler;
14
15
use BenGorFile\File\Infrastructure\Persistence\Sql\SqlFileRepository;
16
use BenGorFile\File\Infrastructure\Persistence\Sql\SqlFileSpecificationFactory;
17
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
18
use Symfony\Component\DependencyInjection\ContainerBuilder;
19
use Symfony\Component\DependencyInjection\Definition;
20
use Symfony\Component\DependencyInjection\Reference;
21
22
/**
23
 * Register Sql services compiler pass.
24
 *
25
 * Service declaration via PHP allows more
26
 * flexibility with customization extend files.
27
 *
28
 * @author Mikel Etxebarria <[email protected]>
29
 */
30
class SqlServicesPass implements CompilerPassInterface
31
{
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function process(ContainerBuilder $container)
36
    {
37
        $config = $container->getParameter('bengor_file.config');
38
        foreach ($config['file_class'] as $key => $file) {
39
            if ('sql' !== $file['persistence']) {
40
                continue;
41
            }
42
43
            if (!$container->hasDefinition('bengor.file.infrastructure.persistence.pdo')) {
44
                $container->setDefinition(
45
                    'bengor.file.infrastructure.persistence.pdo',
46
                    (new Definition(
47
                        \PDO::class, [
48
                            "mysql:host=%database_host%;dbname=%database_name%",
49
                            "%database_user%",
50
                            "%database_password%",
51
                            [],
52
                        ]
53
                    ))->setPublic(false)
54
                );
55
            }
56
57
            $container->setDefinition(
58
                'bengor.file.infrastructure.persistence.' . $key . '_repository',
59
                (new Definition(
60
                    SqlFileRepository::class, [
61
                        new Reference('bengor.file.infrastructure.persistence.pdo'),
62
                    ]
63
                ))->setPublic(false)
64
            );
65
66
            $container->setAlias(
67
                'bengor_file.' . $key . '.repository',
68
                'bengor.file.infrastructure.persistence.' . $key . '_repository'
69
            );
70
71
            $container->setDefinition(
72
                'bengor.file.infrastructure.persistence.' . $key . '_specification_factory',
73
                (new Definition(
74
                    SqlFileSpecificationFactory::class
75
                ))->setPublic(false)
76
            );
77
78
            $container->setAlias(
79
                'bengor_file.' . $key . '.specification_factory',
80
                'bengor.file.infrastructure.persistence.' . $key . '_specification_factory'
81
            );
82
        }
83
    }
84
}
85
86