Passed
Push — master ( bea9fb...617266 )
by Siim
11:02
created

Sf4ApiExtensionTrait::addRequestHandlerRoutes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 2
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: siim
5
 * Date: 28.02.19
6
 * Time: 8:47
7
 */
8
9
namespace Sf4\Api\DependencyInjection\Traits;
10
11
use Sf4\Api\DependencyInjection\Configuration;
12
use Symfony\Component\DependencyInjection\ContainerBuilder;
13
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
14
use Symfony\Component\Config\FileLocator;
15
use Sf4\Api\Repository\RepositoryFactory;
16
use Sf4\Api\RequestHandler\RequestHandlerInterface;
17
18
trait Sf4ApiExtensionTrait
19
{
20
21
    /**
22
     * @param ContainerBuilder $container
23
     * @param string $dir
24
     * @throws \Exception
25
     */
26
    protected function loadServices(ContainerBuilder $container, string $dir)
27
    {
28
        $loader = new YamlFileLoader(
29
            $container,
30
            new FileLocator($dir.'/../Resources/config')
31
        );
32
        $loader->load('services.yaml');
33
    }
34
35
    /**
36
     * @param ContainerBuilder $container
37
     * @param array $configSites
38
     */
39
    protected function addRequestHandlerSites(ContainerBuilder $container, array $configSites)
40
    {
41
        $definition = $container->getDefinition(RequestHandlerInterface::class);
42
        $definition->addMethodCall(
43
            'addSites',
44
            [
45
                '$sites' => $configSites
46
            ]
47
        );
48
    }
49
50
    /**
51
     * @param ContainerBuilder $container
52
     * @param array $configRoutes
53
     */
54
    protected function addRequestHandlerRoutes(ContainerBuilder $container, array $configRoutes)
55
    {
56
        $availableRoutes = [];
57
        foreach ($configRoutes as $route) {
58
            $availableRoutes[$route[Configuration::ROUTES_ROUTE]] = $route[Configuration::ROUTES_REQUEST_CLASS];
59
        }
60
61
        $definition = $container->getDefinition(RequestHandlerInterface::class);
62
        $definition->addMethodCall(
63
            'addAvailableRoutes',
64
            [
65
                '$availableRoutes' => $availableRoutes
66
            ]
67
        );
68
    }
69
70
    /**
71
     * @param ContainerBuilder $container
72
     * @param array $configEntities
73
     */
74
    protected function addRepositoryFactoryEntities(ContainerBuilder $container, array $configEntities)
75
    {
76
        $definition = $container->getDefinition(RepositoryFactory::class);
77
        foreach ($configEntities as $entity) {
78
            $tableName = $entity[Configuration::ENTITIES_TABLE_NAME];
79
            $entityClass = $entity[Configuration::ENTITIES_ENTITY_CLASS];
80
            $definition->addMethodCall(
81
                'addEntity',
82
                [
83
                    '$tableName' => $tableName,
84
                    '$entityClass' => $entityClass
85
                ]
86
            );
87
        }
88
    }
89
}
90