FileSystemCommandsLoader   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 6
c 1
b 1
f 0
lcom 1
cbo 3
dl 0
loc 57
ccs 0
cts 23
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B loadCommands() 0 28 5
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
  }