Completed
Push — master ( 88f534...73040e )
by Neomerx
01:22
created

CommandsDebugIo::writeWarning()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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