Passed
Push — master ( 517bd8...f7cc98 )
by Attila
06:55
created

EnumsCommand::handle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 8
rs 9.4285
c 1
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
/**
3
 * Contains the Enums Command class
4
 *
5
 * @copyright   Copyright (c) Attila Fulop
6
 * @author      Attila Fulop
7
 * @license     MIT
8
 * @since       2017-12-09
9
 */
10
11
namespace Konekt\Concord\Console\Commands;
12
13
use Illuminate\Console\Command;
14
use Illuminate\Support\Collection;
15
use Konekt\Concord\Contracts\Concord;
16
17
class EnumsCommand extends Command
18
{
19
    protected $signature = 'concord:enums';
20
21
    protected $description = 'List Enums';
22
23
    protected $headers = ['Shorthand', 'Contract', 'Concrete'];
24
25
    public function handle(Concord $concord)
26
    {
27
        $bindings = $concord->getEnumBindings();
28
29
        if ($bindings->count()) {
30
            $this->showBindings($bindings);
31
        } else {
32
            $this->line('No enums have been registered.');
33
        }
34
    }
35
36
    /**
37
     * Displays the list of enum bindings on the output
38
     *
39
     * @param Collection $bindings
40
     */
41
    protected function showBindings(Collection $bindings)
42
    {
43
        $table = [];
44
45
        $bindings->map(function ($item, $key) {
46
            return [
47
                'shortName' => shorten($key),
48
                'abstract'  => $key,
49
                'concrete'  => $item
50
            ];
51
        })->sort(function ($a, $b) {
52
            return $a['shortName'] > $b['shortName'];
53
        })->each(function ($binding) use (&$table) {
54
            $table[] = [
55
                $binding['shortName'],
56
                $binding['abstract'],
57
                $binding['concrete'],
58
            ];
59
        });
60
61
        $this->table($this->headers, $table);
62
    }
63
}
64