Completed
Push — master ( 98adac...89edb3 )
by Christian
05:26
created

DirectoryRouteLoader   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 94.12%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 42
ccs 16
cts 17
cp 0.9412
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B load() 0 21 5
A supports() 0 4 3
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\Routing\Loader;
13
14
use Symfony\Component\Config\Loader\Loader;
15
use Symfony\Component\Routing\RouteCollection;
16
17
/**
18
 * Parse annotated controller classes from all files of a directory.
19
 *
20
 * @author Christian Flothmann <[email protected]>
21
 */
22
class DirectoryRouteLoader extends Loader
23
{
24
    private $processor;
25
26 12
    public function __construct(RestRouteProcessor $processor)
27
    {
28 12
        $this->processor = $processor;
29 12
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 1
    public function load($resource, $type = null)
35
    {
36 1
        if (!is_dir($resource)) {
37
            throw new \InvalidArgumentException(sprintf('Given resource of type "%s" is no directory.', $resource));
38
        }
39
40 1
        $collection = new RouteCollection();
41
42 1
        $directoryIterator = new \DirectoryIterator($resource);
43
44 1
        foreach ($directoryIterator as $file) {
45 1
            if ($file->getFilename() === '.' || $file->getFilename() === '..') {
46 1
                continue;
47
            }
48
49 1
            $imported = $this->processor->importResource($this, ClassUtils::findClassInFile($file->getPathname()), array(), null, null, 'rest');
50 1
            $collection->addCollection($imported);
51 1
        }
52
53 1
        return $collection;
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 5
    public function supports($resource, $type = null)
60
    {
61 5
        return 'rest' === $type && is_string($resource) && is_dir($resource);
62
    }
63
}
64