Passed
Pull Request — master (#4)
by ANTHONIUS
04:55
created

UserResourcePass   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 41
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A generateApiResourceCache() 0 24 3
A process() 0 10 2
1
<?php
2
3
4
namespace Doyo\UserBundle\Bridge\ApiPlatform;
5
6
7
use Symfony\Component\Config\ConfigCache;
8
use Symfony\Component\Config\Resource\FileResource;
9
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
10
use Symfony\Component\DependencyInjection\ContainerBuilder;
11
12
class UserResourcePass implements CompilerPassInterface
13
{
14 2
    public function process(ContainerBuilder $container)
15
    {
16 2
        if(!$container->getParameter('doyo_user.api_platform')){
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after IF keyword; 0 found
Loading history...
17 1
            return;
18
        }
19 1
        $path = $this->generateApiResourceCache($container);
20 1
        $container->prependExtensionConfig('api_platform',[
21
            'mapping' => [
22
                'paths' => [
23 1
                    $path
24
                ]
25
            ]
26
        ]);
27
    }
28
29 1
    private function generateApiResourceCache(ContainerBuilder $container)
30
    {
31 1
        $cacheDir = $container->getParameter('kernel.cache_dir').'/doyo-user';
32 1
        if(!is_dir($cacheDir)){
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after IF keyword; 0 found
Loading history...
33 1
            mkdir($cacheDir, 0775, true);
34
        }
35
36 1
        $debug = $container->getParameter('kernel.debug');
37 1
        $path  = $cacheDir.'/user-resource.yaml';
38 1
        $cache = new ConfigCache($path, $debug);
39
40 1
        if (!$cache->isFresh()) {
41 1
            $template = __DIR__.'/../../Resources/config/template/user-resource.yaml';
42 1
            $contents = file_get_contents($template);
43 1
            $contents = strtr($contents, [
44 1
                '%doyo_user.user_class%' => $container->getParameter('doyo_user.user_class'),
45
            ]);
46
47
            //file_put_contents($dir.'/user-resource.yaml', $contents, LOCK_EX);
48 1
            $resources = [new FileResource($template)];
49 1
            $cache->write($contents, $resources);
50
        }
51
52 1
        return $path;
53
    }
54
}