Completed
Push — master ( 4407aa...f0730d )
by ANTHONIUS
22s queued 14s
created

UserResourcePass::process()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 1
dl 0
loc 8
ccs 6
cts 6
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the DoyoUserBundle project.
5
 *
6
 * (c) Anthonius Munthi <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Doyo\UserBundle\Bridge\ApiPlatform;
15
16
use Symfony\Component\Config\ConfigCache;
17
use Symfony\Component\Config\Resource\FileResource;
18
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
19
use Symfony\Component\DependencyInjection\ContainerBuilder;
20
21
class UserResourcePass implements CompilerPassInterface
22
{
23 2
    public function process(ContainerBuilder $container)
24
    {
25 2
        if (!$container->getParameter('doyo_user.api_platform')) {
26 1
            return;
27
        }
28 1
        $this->generateApiResourceCache($container, 'user-resource.yaml', 'User.yaml');
29 1
        if ($container->hasParameter('doyo_user.model.group.class')) {
30 1
            $this->generateApiResourceCache($container, 'group-resource.yaml', 'Group.yaml');
31
        }
32
    }
33
34 1
    private function generateApiResourceCache(ContainerBuilder $container, $template, $path)
35
    {
36 1
        $cacheDir = __DIR__.'/../../Resources/config/api_resources';
37 1
        $debug    = $container->getParameter('kernel.debug');
38 1
        $path     = $cacheDir.'/'.$path;
39 1
        $cache    = new ConfigCache($path, $debug);
40
41 1
        if (!$cache->isFresh()) {
42 1
            $template = __DIR__.'/../../Resources/config/template/'.$template;
43 1
            $contents = file_get_contents($template);
44 1
            $contents = strtr($contents, [
45 1
                '%doyo_user.model.user.class%'  => $container->getParameter('doyo_user.model.user.class'),
46 1
                '%doyo_user.model.group.class%' => $container->getParameter('doyo_user.model.group.class'),
47
            ]);
48
49
            //file_put_contents($dir.'/user-resource.yaml', $contents, LOCK_EX);
50 1
            $resources = [new FileResource($template)];
51 1
            $cache->write($contents, $resources);
52
        }
53
54 1
        return $path;
55
    }
56
}
57