Completed
Push — master ( 665986...1514e7 )
by Guilh
08:09 queued 05:04
created

AllowedMethodsRouterLoader   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 10
c 2
b 1
f 0
lcom 1
cbo 3
dl 0
loc 78
ccs 39
cts 39
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A isOptional() 0 4 1
A __construct() 0 5 1
A getAllowedMethods() 0 8 2
B warmUp() 0 36 6
1
<?php
2
3
/*
4
 * This file is part of the FOSRestBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
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
namespace FOS\RestBundle\Response\AllowedMethodsLoader;
13
14
use Symfony\Component\Config\ConfigCache;
15
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
16
use Symfony\Component\Routing\RouterInterface;
17
18
/**
19
 * AllowedMethodsRouterLoader implementation using RouterInterface to fetch
20
 * allowed http methods.
21
 *
22
 * @author Boris Guéry <[email protected]>
23
 */
24
class AllowedMethodsRouterLoader implements AllowedMethodsLoaderInterface, CacheWarmerInterface
25
{
26
    private $router;
27
    private $cache;
28
29
    /**
30
     * Constructor.
31
     *
32
     * @param RouterInterface $router
33
     * @param string          $cacheDir
34
     * @param bool            $isDebug  Kernel debug flag
35
     */
36 3
    public function __construct(RouterInterface $router, $cacheDir, $isDebug)
37
    {
38 3
        $this->router = $router;
39 3
        $this->cache = new ConfigCache(sprintf('%s/allowed_methods.cache.php', $cacheDir), $isDebug);
40 3
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 2
    public function getAllowedMethods()
46
    {
47 2
        if (!$this->cache->isFresh()) {
48 2
            $this->warmUp(null);
49 2
        }
50
51 2
        return require $this->cache->getPath();
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 2
    public function isOptional()
58
    {
59 2
        return true;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 2
    public function warmUp($cacheDir)
66
    {
67 2
        $processedRoutes = [];
68
69 2
        $routeCollection = $this->router->getRouteCollection();
70
71 2
        foreach ($routeCollection->all() as $name => $route) {
72 2
            if (!isset($processedRoutes[$route->getPath()])) {
73 2
                $processedRoutes[$route->getPath()] = [
74 2
                    'methods' => [],
75 2
                    'names' => [],
76
                ];
77 2
            }
78
79 2
            $processedRoutes[$route->getPath()]['names'][] = $name;
80 2
            $processedRoutes[$route->getPath()]['methods'] = array_merge(
81 2
                $processedRoutes[$route->getPath()]['methods'],
82 2
                $route->getMethods()
83 2
            );
84 2
        }
85
86 2
        $allowedMethods = [];
87
88 2
        foreach ($processedRoutes as $processedRoute) {
89 2
            if (count($processedRoute['methods']) > 0) {
90 1
                foreach ($processedRoute['names'] as $name) {
91 1
                    $allowedMethods[$name] = array_unique($processedRoute['methods']);
92 1
                }
93 1
            }
94 2
        }
95
96 2
        $this->cache->write(
97 2
            sprintf('<?php return %s;', var_export($allowedMethods, true)),
98 2
            $routeCollection->getResources()
99 2
        );
100 2
    }
101
}
102