CommandsDebugIo   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 187
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 19
lcom 3
cbo 0
dl 0
loc 187
ccs 45
cts 45
cp 1
rs 10
c 0
b 0
f 0

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A hasArgument() 0 4 1
A getArgument() 0 4 1
A getArguments() 0 4 1
A hasOption() 0 4 1
A getOption() 0 4 1
A getOptions() 0 4 1
A writeInfo() 0 4 1
A writeWarning() 0 4 1
A writeError() 0 4 1
A getRecords() 0 4 1
A getInfoRecords() 0 4 1
A getWarningRecords() 0 4 1
A getErrorRecords() 0 4 1
A clearRecords() 0 6 1
A addRecord() 0 11 1
A filterRecordsByType() 0 12 3
1
<?php declare(strict_types=1);
2
3
namespace Limoncello\Testing;
4
5
/**
6
 * Copyright 2015-2020 [email protected]
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 * http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
21
use DateTimeImmutable;
22
use Exception;
23
use Limoncello\Contracts\Commands\IoInterface;
24
25
/**
26
 * @package Limoncello\Testing
27
 */
28
class CommandsDebugIo implements CommandsDebugIoInterface
29
{
30
    /**
31
     * @var array
32
     */
33
    private $arguments;
34
35
    /**
36
     * @var array
37
     */
38
    private $options;
39
40
    /**
41
     * @var array
42
     */
43
    private $records = [];
44
45
    /**
46
     * @param array $arguments
47
     * @param array $options
48
     */
49 2
    public function __construct(array $arguments = [], array $options = [])
50
    {
51 2
        $this->arguments = $arguments;
52 2
        $this->options   = $options;
53
    }
54
55
    /**
56
     * @inheritdoc
57
     */
58 1
    public function hasArgument(string $name): bool
59
    {
60 1
        return array_key_exists($name, $this->arguments);
61
    }
62
63
    /**
64
     * @inheritdoc
65
     */
66 1
    public function getArgument(string $name)
67
    {
68 1
        return $this->arguments[$name];
69
    }
70
71
    /**
72
     * @inheritdoc
73
     */
74 1
    public function getArguments(): array
75
    {
76 1
        return $this->arguments;
77
    }
78
79
    /**
80
     * @inheritdoc
81
     */
82 1
    public function hasOption(string $name): bool
83
    {
84 1
        return array_key_exists($name, $this->options);
85
    }
86
87
    /**
88
     * @inheritdoc
89
     */
90 1
    public function getOption(string $name)
91
    {
92 1
        return $this->options[$name];
93
    }
94
95
    /**
96
     * @inheritdoc
97
     */
98 1
    public function getOptions(): array
99
    {
100 1
        return $this->options;
101
    }
102
103
    /**
104
     * @inheritdoc
105
     *
106
     * @throws Exception
107
     */
108 1
    public function writeInfo(string $message, int $verbosity = self::VERBOSITY_NORMAL): IoInterface
109
    {
110 1
        return $this->addRecord(static::TYPE_INFO, $verbosity, $message);
111
    }
112
113
    /**
114
     * @inheritdoc
115
     *
116
     * @throws Exception
117
     */
118 1
    public function writeWarning(string $message, int $verbosity = self::VERBOSITY_NORMAL): IoInterface
119
    {
120 1
        return $this->addRecord(static::TYPE_WARNING, $verbosity, $message);
121
    }
122
123
    /**
124
     * @inheritdoc
125
     *
126
     * @throws Exception
127
     */
128 1
    public function writeError(string $message, int $verbosity = self::VERBOSITY_NORMAL): IoInterface
129
    {
130 1
        return $this->addRecord(static::TYPE_ERROR, $verbosity, $message);
131
    }
132
133
    /**
134
     * @inheritdoc
135
     */
136 1
    public function getRecords(): array
137
    {
138 1
        return $this->records;
139
    }
140
141
    /**
142
     * @inheritdoc
143
     */
144 1
    public function getInfoRecords(): array
145
    {
146 1
        return $this->filterRecordsByType($this->records, static::TYPE_INFO);
147
    }
148
149
    /**
150
     * @inheritdoc
151
     */
152 1
    public function getWarningRecords(): array
153
    {
154 1
        return $this->filterRecordsByType($this->records, static::TYPE_WARNING);
155
    }
156
157
    /**
158
     * @inheritdoc
159
     */
160 1
    public function getErrorRecords(): array
161
    {
162 1
        return $this->filterRecordsByType($this->records, static::TYPE_ERROR);
163
    }
164
165
    /**
166
     * @inheritdoc
167
     */
168 1
    public function clearRecords(): CommandsDebugIoInterface
169
    {
170 1
        $this->records = [];
171
172 1
        return $this;
173
    }
174
175
    /**
176
     * @param int    $type
177
     * @param int    $verbosity
178
     * @param string $message
179
     *
180
     * @return self
181
     *
182
     * @throws Exception
183
     */
184 1
    private function addRecord(int $type, int $verbosity, string $message): self
185
    {
186 1
        $this->records[] = [
187 1
            static::RECORD_KEY_TYPE      => $type,
188 1
            static::RECORD_KEY_VERBOSITY => $verbosity,
189 1
            static::RECORD_KEY_DATE_TIME => new DateTimeImmutable(),
190 1
            static::RECORD_KEY_MESSAGE   => $message,
191
        ];
192
193 1
        return $this;
194
    }
195
196
    /**
197
     * @param array $records
198
     * @param int   $type
199
     *
200
     * @return array
201
     */
202 1
    private function filterRecordsByType(array $records, int $type): array
203
    {
204 1
        $result = [];
205
206 1
        foreach ($records as $record) {
207 1
            if ($type === $record[static::RECORD_KEY_TYPE]) {
208 1
                $result[] = $record;
209
            }
210
        }
211
212 1
        return $result;
213
    }
214
}
215