DbfakatorServiceFactory.php 1.53 KB
Newer Older
Bertrand Gauthier's avatar
Bertrand Gauthier committed
1
2
3
4
<?php

namespace UnicaenDbfakator\Service;

5
use Psr\Container\ContainerInterface;
6
use Webmozart\Assert\Assert;
Bertrand Gauthier's avatar
Bertrand Gauthier committed
7
8
9
10

class DbfakatorServiceFactory
{
    /**
11
     * @param \Psr\Container\ContainerInterface $container
Bertrand Gauthier's avatar
Bertrand Gauthier committed
12
13
14
15
16
17
18
19
20
21
     * @return \UnicaenDbfakator\Service\DbfakatorService
     * @throws \Doctrine\DBAL\Exception
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    public function __invoke(ContainerInterface $container): DbfakatorService
    {
        /** @var \Doctrine\ORM\EntityManager $em */
        $em = $container->get('doctrine.entitymanager.orm_default');

22
23
        $config = $this->getConfig($container);

Bertrand Gauthier's avatar
Bertrand Gauthier committed
24
        $service = new DbfakatorService();
25
        $service->setConfig($config);
Bertrand Gauthier's avatar
Bertrand Gauthier committed
26
27
28
29
        $service->setEntityManager($em);

        return $service;
    }
30
31
32
33
34
35
36
37
38
39
40

    /**
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    private function getConfig(ContainerInterface $container): array
    {
        /** @var array $appConfig */
        $appConfig = $container->get('Config');

        $config = $appConfig['unicaen-dbfakator'] ?? [];
41
        if (!$config) {
42
43
44
45
46
47
48
49
50
            return [];
        }

        Assert::isArray($config, "La config du module doit être un tableau");
        Assert::keyExists($config, $k = 'entities', "La config du module doit posséder la clé '$k'");
        Assert::keyExists($config, $k = 'output', "La config du module doit posséder la clé '$k'");

        return $config;
    }
Bertrand Gauthier's avatar
Bertrand Gauthier committed
51
}