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

ModelsCommand   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
dl 0
loc 63
c 0
b 0
f 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A showBindings() 0 21 1
A handle() 0 8 2
1
<?php
2
/**
3
 * Contains the Models Command class
4
 *
5
 * @copyright   Copyright (c) Lajos Fazakas
6
 * @author      Lajos Fazakas
7
 * @license     MIT
8
 * @since       2017-04-06
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 ModelsCommand extends Command
18
{
19
    /**
20
     * The name and signature of the console command.
21
     *
22
     * @var string
23
     */
24
    protected $signature = 'concord:models';
25
26
    /**
27
     * The console command description.
28
     *
29
     * @var string
30
     */
31
    protected $description = 'List Models';
32
33
    /** @var array */
34
    protected $headers = ['Entity', 'Contract', 'Model'];
35
36
    /**
37
     * Execute the console command.
38
     *
39
     * @param Concord $concord
40
     *
41
     * @return mixed
42
     */
43
    public function handle(Concord $concord)
44
    {
45
        $bindings = $concord->getModelBindings();
46
47
        if ($bindings->count()) {
48
            $this->showBindings($bindings);
49
        } else {
50
            $this->line('No model bindings have been registered.');
51
        }
52
    }
53
54
    /**
55
     * Displays the list of model bindings on the output
56
     *
57
     * @param Collection $bindings
58
     */
59
    protected function showBindings(Collection $bindings)
60
    {
61
        $table = [];
62
63
        $bindings->map(function ($item, $key) {
64
            return [
65
                'shortName' => substr(strrchr($key, '\\'), 1),
66
                'abstract'  => $key,
67
                'concrete'  => $item
68
            ];
69
        })->sort(function ($a, $b) {
70
            return $a['shortName'] > $b['shortName'];
71
        })->each(function ($binding) use (&$table) {
72
            $table[] = [
73
                $binding['shortName'],
74
                $binding['abstract'],
75
                $binding['concrete'],
76
            ];
77
        });
78
79
        $this->table($this->headers, $table);
80
    }
81
}
82