TemplatesCommand::separateRows()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Arrilot\BitrixMigrations\Commands;
4
5
use Arrilot\BitrixMigrations\TemplatesCollection;
6
use Symfony\Component\Console\Helper\Table;
7
use Symfony\Component\Console\Helper\TableSeparator;
8
9
class TemplatesCommand extends AbstractCommand
10
{
11
    /**
12
     * TemplatesCollection instance.
13
     *
14
     * @var TemplatesCollection
15
     */
16
    protected $collection;
17
18
    protected static $defaultName = 'templates';
19
20
    /**
21
     * Constructor.
22
     *
23
     * @param TemplatesCollection $collection
24
     * @param string|null         $name
25
     */
26
    public function __construct(TemplatesCollection $collection, $name = null)
27
    {
28
        $this->collection = $collection;
29
30
        parent::__construct($name);
31
    }
32
33
    /**
34
     * Configures the current command.
35
     */
36
    protected function configure()
37
    {
38
        $this->setDescription('Show the list of available migration templates');
39
    }
40
41
    /**
42
     * Execute the console command.
43
     *
44
     * @return null|int
45
     */
46
    protected function fire()
47
    {
48
        $table = new Table($this->output);
49
        $table->setHeaders(['Name', 'Path', 'Description'])->setRows($this->collectRows());
50
        $table->setStyle('borderless');
51
        $table->render();
52
    }
53
54
    /**
55
     * Collect and return templates from a Migrator.
56
     *
57
     * @return array
58
     */
59
    protected function collectRows()
60
    {
61
        $rows = collect($this->collection->all())
62
            ->filter(function ($template) {
63
                return $template['is_alias'] == false;
64
            })
65
            ->sortBy('name')
66
            ->map(function ($template) {
67
                $row = [];
68
69
                $names = array_merge([$template['name']], $template['aliases']);
70
                $row[] = implode("\n/ ", $names);
71
                $row[] = wordwrap($template['path'], 65, "\n", true);
72
                $row[] = wordwrap($template['description'], 25, "\n", true);
73
74
                return $row;
75
            });
76
77
        return $this->separateRows($rows);
78
    }
79
80
    /**
81
     * Separate rows with a separator.
82
     *
83
     * @param $templates
84
     *
85
     * @return array
86
     */
87
    protected function separateRows($templates)
88
    {
89
        $rows = [];
90
        foreach ($templates as $template) {
91
            $rows[] = $template;
92
            $rows[] = new TableSeparator();
93
        }
94
        unset($rows[count($rows) - 1]);
95
96
        return $rows;
97
    }
98
}
99