Test Failed
Pull Request — master (#3)
by Ole
08:31
created

LegacyRouteLoader::supports()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
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
     * LegacyRouteLoader constructor.
23
     *
24
     * @param string $legacyPath
25
     *
26
     * @throws \InvalidArgumentException
27
     */
28
    public function __construct($legacyPath, Finder $finder = null)
29
    {
30
        $this->finder = $finder ?: new Finder();
31
        $this->finder->ignoreDotFiles(true)
32
                     ->files()
33
                     ->name('*.php')
34
                     ->in($legacyPath)
35
        ;
36
    }
37
38
    /** {@inheritdoc} */
39
    public function load($resource, $type = null)
40
    {
41
        if (true === $this->loaded) {
42
            throw new \RuntimeException('Do not add the "legacy" loader twice');
43
        }
44
45
        $routes = new RouteCollection();
46
47
        $defaults = array(
48
          '_controller' => 'basster_legacy_bridge.legacy_controller:runLegacyScript',
49
        );
50
51
        /** @var SplFileInfo $file */
52
        foreach ($this->finder as $file) {
53
            $defaults['legacyScript'] = $file->getPathname();
54
            $defaults['requestPath']  = '/' . $file->getRelativePathname();
55
56
            $route = new Route($file->getRelativePathname(), $defaults);
57
            $routes->add($this->createLegacyRouteName($file), $route);
58
        }
59
60
        $this->loaded = true;
61
62
        return $routes;
63
    }
64
65
    /**
66
     * @param SplFileInfo $file
67
     *
68
     * @return string
69
     */
70
    private function createLegacyRouteName(SplFileInfo $file)
71
    {
72
        return 'basster.legacy.' .
73
        str_replace(
74
          '/',
75
          '__',
76
          substr($file->getRelativePathname(), 0, -4)
77
        );
78
    }
79
80
    /** {@inheritdoc} */
81
    public function supports($resource, $type = null)
82
    {
83
        return 'legacy' === $type;
84
    }
85
}
86