ValidateDirectoryCommand   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 2
cbo 3
dl 0
loc 69
ccs 0
cts 37
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setDefaultPath() 0 4 1
A configure() 0 17 2
A loadDefinitions() 0 21 3
1
<?php
2
/**
3
 * ValidateDirectoryCommand class file
4
 */
5
6
namespace Graviton\JsonSchemaBundle\Command;
7
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Input\InputOption;
10
use Symfony\Component\Finder\Finder;
11
use Symfony\Component\Finder\SplFileInfo;
12
13
/**
14
 * Validate JSON definition in directory
15
 *
16
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
17
 * @license  https://opensource.org/licenses/MIT MIT License
18
 * @link     http://swisscom.ch
19
 */
20
class ValidateDirectoryCommand extends AbstractValidateCommand
21
{
22
    /**
23
     * @var string
24
     */
25
    private $defaultPath;
26
27
    /**
28
     * Set default scan path
29
     *
30
     * @param string $path Path
31
     * @return void
32
     */
33
    public function setDefaultPath($path)
34
    {
35
        $this->defaultPath = $path;
36
    }
37
38
    /**
39
     * Configures the current command.
40
     *
41
     * @return void
42
     */
43
    protected function configure()
44
    {
45
        parent::configure();
46
47
        if ($this->getName() === null) {
48
            $this->setName('graviton:validate:definition:directory');
49
        }
50
51
        $this
52
            ->setDescription('Validate a JSON definition')
53
            ->addOption(
54
                'path',
55
                '',
56
                InputOption::VALUE_OPTIONAL,
57
                'Path to the json definition.'
58
            );
59
    }
60
61
    /**
62
     * Load JSON definitions
63
     *
64
     * @param InputInterface $input Command input
65
     * @return SplFileInfo[]
66
     */
67
    protected function loadDefinitions(InputInterface $input)
68
    {
69
        $path = $input->getOption('path');
70
        if ($path === null) {
71
            $path = $this->defaultPath;
72
        }
73
74
        // if we are vendorized we will search all vendor paths
75
        if (strpos($path, 'vendor/graviton/graviton')) {
76
            $path .= '/../../';
77
        }
78
79
        $finder = (new Finder())
80
            ->files()
81
            ->in($path)
82
            ->name('*.json')
83
            ->notName('_*')
84
            ->path('/(^|\/)resources\/definition($|\/)/i')
85
            ->notPath('/(^|\/)Tests($|\/)/i');
86
        return iterator_to_array($finder);
87
    }
88
}
89