Completed
Push — master ( 57b7f2...4f95ff )
by Grégoire
28:13 queued 01:46
created

RoutesCache   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 5
dl 0
loc 56
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A load() 0 28 5
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\AdminBundle\Route;
15
16
use Sonata\AdminBundle\Admin\AdminInterface;
17
use Symfony\Component\Config\ConfigCache;
18
use Symfony\Component\Config\Resource\FileResource;
19
20
/**
21
 * @author Thomas Rabaix <[email protected]>
22
 */
23
final class RoutesCache
24
{
25
    /**
26
     * @var string
27
     */
28
    private $cacheFolder;
29
30
    /**
31
     * @var bool
32
     */
33
    private $debug;
34
35
    /**
36
     * @param string $cacheFolder
37
     * @param bool   $debug
38
     */
39
    public function __construct($cacheFolder, $debug)
40
    {
41
        $this->cacheFolder = $cacheFolder;
42
        $this->debug = $debug;
43
    }
44
45
    /**
46
     * @throws \RuntimeException
47
     *
48
     * @return mixed
49
     */
50
    public function load(AdminInterface $admin)
51
    {
52
        $filename = sprintf('%s/route_%s', $this->cacheFolder, md5($admin->getCode()));
53
54
        $cache = new ConfigCache($filename, $this->debug);
55
        if (!$cache->isFresh()) {
56
            $resources = [];
57
            $routes = [];
58
59
            $reflection = new \ReflectionObject($admin);
60
            if (file_exists($reflection->getFileName())) {
61
                $resources[] = new FileResource($reflection->getFileName());
62
            }
63
64
            foreach ($admin->getRoutes()->getElements() as $code => $route) {
65
                $routes[$code] = $route->getDefault('_sonata_name');
66
            }
67
68
            foreach ($admin->getExtensions() as $extension) {
69
                $reflection = new \ReflectionObject($extension);
70
                $resources[] = new FileResource($reflection->getFileName());
71
            }
72
73
            $cache->write(serialize($routes), $resources);
74
        }
75
76
        return unserialize(file_get_contents($filename));
77
    }
78
}
79