LegacyRouteLoader::createLegacyRouteName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Basster\LegacyBridgeBundle\Routing;
4
5
use Symfony\Component\Config\Loader\Loader;
6
use Symfony\Component\Finder\Finder;
7
use Symfony\Component\Finder\SplFileInfo;
8
use Symfony\Component\Routing\Route;
9
use Symfony\Component\Routing\RouteCollection;
10
11
class LegacyRouteLoader extends Loader
12
{
13
    /** @var bool */
14
    private $loaded = false;
15
16
    /**
17
     * @var \Symfony\Component\Finder\Finder
18
     */
19
    private $finder;
20
21
    /**
22
     * @var string
23
     */
24
    private $legacyPath;
25
26
    /**
27
     * LegacyRouteLoader constructor.
28
     *
29
     * @param string                                $legacyPath
30
     * @param \Symfony\Component\Finder\Finder|null $finder
31
     */
32 5
    public function __construct($legacyPath, Finder $finder = null)
33
    {
34 5
        $this->finder     = $finder ?: new Finder();
35 5
        $this->legacyPath = $legacyPath;
36 5
    }
37
38
    /**
39
     * {@inheritdoc}
40
     * @throws \InvalidArgumentException
41
     * @throws \RuntimeException
42
     */
43 2
    public function load($resource, $type = null)
44
    {
45 2
        if (true === $this->loaded) {
46 1
            throw new \RuntimeException('Do not add the "legacy" loader twice');
47
        }
48
49 2
        $routes = new RouteCollection();
50 2
        $this->initFinder();
51
52
        $defaults = array(
53 2
          '_controller' => 'basster_legacy_bridge.legacy_controller:runLegacyScript',
54
        );
55
56
        /** @var SplFileInfo $file */
57 2
        foreach ($this->finder as $file) {
58 2
            $defaults['legacyScript'] = $file->getPathname();
59 2
            $defaults['requestPath']  = '/'.$file->getRelativePathname();
60
61 2
            $route = new Route($file->getRelativePathname(), $defaults);
62 2
            $routes->add($this->createLegacyRouteName($file), $route);
63
        }
64
65 2
        $this->loaded = true;
66
67 2
        return $routes;
68
    }
69
70
71
    /** {@inheritdoc} */
72 3
    public function supports($resource, $type = null)
73
    {
74 3
        return 'legacy' === $type && \is_dir($this->legacyPath) && \is_readable($this->legacyPath);
75
    }
76
77
    /**
78
     * @throws \InvalidArgumentException
79
     */
80 2
    private function initFinder()
81
    {
82 2
        $this->finder->ignoreDotFiles(true)
83 2
                     ->files()
84 2
                     ->name('*.php')
85 2
                     ->in($this->legacyPath);
86 2
    }
87
    /**
88
     * @param SplFileInfo $file
89
     *
90
     * @return string
91
     */
92 2
    private function createLegacyRouteName(SplFileInfo $file)
93
    {
94
        return 'basster.legacy.'.
95 2
          str_replace(
96 2
            '/',
97 2
            '__',
98 2
            substr($file->getRelativePathname(), 0, -4)
99
          );
100
    }
101
}
102