Passed
Push — master ( 04a567...992e53 )
by Kirill
05:40 queued 19s
created

LazyTrait::supportsLazyLoading()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Vladislav Gorenkin (vladgorenkin)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Console\Traits;
13
14
use Psr\Container\ContainerInterface;
15
use Spiral\Console\Command as SpiralCommand;
16
use Symfony\Component\Console\Command\Command as SymfonyCommand;
17
use Symfony\Component\Console\Command\LazyCommand;
18
19
trait LazyTrait
20
{
21
    /** @var ContainerInterface */
22
    private $container;
23
24
    /**
25
     * Check if command can be lazy-loaded.
26
     *
27
     * @param class-string $class
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string.
Loading history...
28
     * @return bool
29
     */
30
    private function supportsLazyLoading(string $class): bool
31
    {
32
        return is_subclass_of($class, SpiralCommand::class)
33
            && \defined(sprintf('%s::%s', $class, 'NAME'));
34
    }
35
36
    /**
37
     * Wrap given command into LazyCommand which will be executed only on run.
38
     *
39
     * @param class-string $class
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string.
Loading history...
40
     * @return LazyCommand
41
     */
42
    private function createLazyCommand(string $class): LazyCommand
43
    {
44
        return new LazyCommand(
45
            $class::NAME,
46
            [],
47
            \defined(sprintf('%s::%s', $class, 'DESCRIPTION'))
48
                ? $class::DESCRIPTION
49
                : '',
50
            false,
51
            function () use ($class): SymfonyCommand {
52
                $command = $this->container->get($class);
53
                $command->setContainer($this->container);
54
55
                return $command;
56
            }
57
        );
58
    }
59
}
60