FileSystemCommandsLoader::loadCommands()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 28
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 28
ccs 0
cts 19
cp 0
rs 8.439
cc 5
eloc 14
nc 5
nop 1
crap 30
1
<?php
2
3
  namespace Funivan\Console\CommandsLoader;
4
5
  use Funivan\Console\NameResolver\NameResolverInterface;
6
  use Symfony\Component\Console\Application;
7
  use Symfony\Component\Console\Command\Command;
8
  use Symfony\Component\Finder\Finder;
9
  use Symfony\Component\Finder\SplFileInfo;
10
11
  /**
12
   * @author Ivan Shcherbak <[email protected]>
13
   */
14
  class FileSystemCommandsLoader implements CommandsLoaderInterface {
15
16
    /**
17
     * @var Finder
18
     */
19
    private $filesFinder;
20
21
    /**
22
     * @var NameResolverInterface
23
     */
24
    private $commandNameResolver;
25
26
27
    /**
28
     * @param Finder $filesFinder
29
     * @param NameResolverInterface $commandNameResolver
30
     */
31
    public function __construct(Finder $filesFinder, NameResolverInterface $commandNameResolver) {
32
      $this->filesFinder = $filesFinder;
33
      $this->commandNameResolver = $commandNameResolver;
34
    }
35
36
37
    /**
38
     * @param Application $app
39
     */
40
    public function loadCommands(Application $app) {
41
42
      foreach ($this->filesFinder as $file) {
43
        /** @var SplFileInfo $file */
44
        $relCommandFile = $file->getRelativePathname();
45
46
        $commandClass = $this->commandNameResolver->getClassNameFromFile($relCommandFile);
47
48
        if (!class_exists($commandClass)) {
49
          continue;
50
        }
51
52
        $reflection = new \ReflectionClass($commandClass);
53
        if (!$reflection->isInstantiable()) {
54
          continue;
55
        }
56
57
        if (!$reflection->isSubclassOf(Command::class)) {
58
          continue;
59
        }
60
61
        $name = $this->commandNameResolver->getNameFromClass($commandClass);
62
        $command = new $commandClass($name);
63
64
        $app->add($command);
65
      }
66
67
    }
68
69
70
  }