ModelsCommand::handle()   A
last analyzed

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