Command::prepareRows()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 13
rs 10
1
<?php
2
3
namespace Orkhanahmadov\LaravelCurrencylayer\Commands;
4
5
use Illuminate\Console\Command as BaseCommand;
6
use Symfony\Component\Console\Helper\Table;
7
8
abstract class Command extends BaseCommand
9
{
10
    /**
11
     * Renders console output.
12
     *
13
     * @param string $title
14
     * @param array<mixed> $rows
15
     */
16
    protected function output(string $title, array $rows): void
17
    {
18
        $table = new Table($this->output);
19
20
        $table->setStyle('box-double')
21
            ->setColumnWidths([8, 16])
22
            ->setHeaderTitle($title)
23
            ->setHeaders(['Currency', 'Rate'])
24
            ->addRows($rows);
25
26
        $table->render();
27
    }
28
29
    /**
30
     * Prepare table rows.
31
     *
32
     * @param array<string> $currencies
33
     * @param array<float>|float $rates
34
     *
35
     * @return array<mixed>
36
     */
37
    protected function prepareRows(array $currencies, $rates): array
38
    {
39
        $rows = [];
40
41
        if (is_array($rates)) {
42
            foreach ($rates as $code => $rate) {
43
                $rows[] = [$code, $rate];
44
            }
45
        } else {
46
            $rows[] = [$currencies[0], $rates];
47
        }
48
49
        return $rows;
50
    }
51
}
52