Extension::retrieveConsoleWriter()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
/**
4
 * This file is part of the phpspec-behavior package.
5
 * (c) 2017 Timo Michna <timomichna/yahoo.de>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Tidal\PhpSpec\BehaviorExtension;
12
13
use PhpSpec\Extension as ExtensionInterface;
14
use PhpSpec\ServiceContainer;
15
use PhpSpec\Console\ConsoleIO;
16
use Tidal\PhpSpec\BehaviorExtension\Console\Command\{
17
    ImplementCommand
18
};
19
use Tidal\PhpSpec\ConsoleExtension\Writer;
20
use Tidal\PhpSpec\ConsoleExtension\Command\InlineConfigurator;
21
use Symfony\Component\Console\Command\Command;
22
23
/**
24
 * class Tidal\PhpSpec\BehaviorExtension\Behavior\Extension
25
 */
26
class Extension implements ExtensionInterface
27
{
28
    private const IMPLEMENT_KEY = 'implement';
29
30
    public const COMMAND_IDS = [
31
        self::IMPLEMENT_KEY => 'console.commands.behavior_implement'
32
    ];
33
34
    private const IO_ID = 'console.io';
35
36
    /**
37
     * @var ImplementCommand
38
     */
39
    private $implementCommand;
40
41
    /**
42
     * @param ServiceContainer $container
43
     * @param array            $params
44
     */
45
    public function load(ServiceContainer $container, array $params)
46
    {
47
        $this->registerCommands($container);
48
    }
49
50
    private function registerCommands(ServiceContainer $container)
51
    {
52
        $this->registerImplementCommand($container);
53
    }
54
55
    private function registerImplementCommand(ServiceContainer $container)
56
    {
57
        $container->define(
58
            self::COMMAND_IDS[ self::IMPLEMENT_KEY ],
59
            function() use ($container) {
60
                /** @var ImplementCommand $command */
61
                $command = $this->getImplementCommand(
62
                    self::retrieveConsoleWriter($container),
63
                    self::createConfigurator()
64
                );
65
                $command->setContainer($container);
66
67
                return $command;
68
            },
69
            [ 'console.commands' ]
70
        );
71
    }
72
73
    /**
74
     * @param Command $implementCommand
75
     */
76
    public function setImplementCommand(Command $implementCommand)
77
    {
78
        $this->implementCommand = $implementCommand;
0 ignored issues
show
Documentation Bug introduced by
It seems like $implementCommand of type object<Symfony\Component\Console\Command\Command> is incompatible with the declared type object<ImplementCommand> of property $implementCommand.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
79
    }
80
81
    /**
82
     * @param Writer $writer
83
     * @return Console\Command\ImplementCommand
84
     */
85
    public function getImplementCommand(Writer $writer, InlineConfigurator $configurator): ImplementCommand
86
    {
87
        return isset($this->implementCommand)
88
            ? $this->implementCommand
89
            : $this->implementCommand = self::createImplementCommand($writer, $configurator);
0 ignored issues
show
Documentation Bug introduced by
It seems like self::createImplementCom...$writer, $configurator) of type object<Tidal\PhpSpec\Beh...mmand\ImplementCommand> is incompatible with the declared type object<ImplementCommand> of property $implementCommand.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
90
    }
91
92
    /**
93
     * @param Writer $writer
94
     * @return Console\Command\ImplementCommand
95
     */
96
    private static function createImplementCommand(Writer $writer, InlineConfigurator $configurator): ImplementCommand
97
    {
98
        return new ImplementCommand($writer, $configurator);
99
    }
100
101
    /**
102
     * @param ServiceContainer $container
103
     * @return Writer
104
     */
105
    private static function retrieveConsoleWriter(ServiceContainer $container)
106
    {
107
        return self::createConsoleWriter(
108
            self::retrieveConsoleIo($container)
109
        );
110
    }
111
112
    /**
113
     * @param ConsoleIO $io
114
     * @return Writer
115
     */
116
    private static function createConsoleWriter(ConsoleIO $io)
117
    {
118
        $writer = new Writer();
119
        $writer->setConsoleIO($io);
120
121
        return $writer;
122
    }
123
124
    /**
125
     * @return InlineConfigurator
126
     */
127
    private static function createConfigurator()
128
    {
129
        return new InlineConfigurator();
130
    }
131
132
    /**
133
     * @param ServiceContainer $container
134
     * @return ConsoleIO
135
     */
136
    private static function retrieveConsoleIo(ServiceContainer $container)
137
    {
138
        return $container->get(self::IO_ID);
139
    }
140
}
141
142