Passed
Push — master ( a90c04...25f10c )
by Attila
02:58
created

EnumsCommand::handle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 8
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 View Code Duplication
class EnumsCommand extends Command
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
18
{
19
    protected $signature = 'concord:enums';
20
21
    protected $description = 'List registered 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