Completed
Push — master ( 82aca0...096f3e )
by Iurii
01:41
created

Library::getListLibrary()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 3
nop 0
1
<?php
2
3
/**
4
 * @package CLI
5
 * @author Iurii Makukh <[email protected]>
6
 * @copyright Copyright (c) 2018, Iurii Makukh <[email protected]>
7
 * @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0+
8
 */
9
10
namespace gplcart\modules\cli\controllers\commands;
11
12
use gplcart\core\Library as CoreLibrary;
13
use gplcart\modules\cli\controllers\Command;
14
15
/**
16
 * Handles commands related to 3-d party libraries
17
 */
18
class Library extends Command
19
{
20
21
    /**
22
     * Library class instance
23
     * @var \gplcart\core\Library $library
24
     */
25
    protected $library;
26
27
    /**
28
     * @param CoreLibrary $library
29
     */
30
    public function __construct(CoreLibrary $library)
31
    {
32
        parent::__construct();
33
34
        $this->library = $library;
35
    }
36
37
    /**
38
     * Callback for "library-clear" command
39
     */
40
    public function cmdClearLibrary()
41
    {
42
        $this->library->clearCache();
43
        $this->output();
44
    }
45
46
    /**
47
     * Callback for "library-get" command
48
     */
49
    public function cmdGetLibrary()
50
    {
51
        $list = $this->getListLibrary();
52
        $this->outputFormat($list);
53
        $this->outputFormatTableLibrary($list);
54
        $this->output();
55
    }
56
57
    /**
58
     * Returns an array of libraries
59
     * @return array
60
     */
61
    protected function getListLibrary()
62
    {
63
        $id = $this->getParam(0);
64
65
        if (!isset($id)) {
66
            $list = $this->library->getList();
67
            $this->limitArray($list);
68
            return $list;
69
        }
70
71
72
        $library = $this->library->get($id);
73
74
        if (empty($library)) {
75
            $this->errorAndExit($this->text('Unexpected result'));
76
        }
77
78
        return array($library);
79
80
    }
81
82
    /**
83
     * Output libraries in a table
84
     * @param array $items
85
     */
86
    protected function outputFormatTableLibrary(array $items)
87
    {
88
        $header = array(
89
            $this->text('ID'),
90
            $this->text('Name'),
91
            $this->text('Type'),
92
            $this->text('Version')
93
        );
94
95
        $rows = array();
96
97
        foreach ($items as $item) {
98
            $rows[] = array(
99
                $item['id'],
100
                $this->text($item['name']),
101
                $this->text($item['type']),
102
                $item['version']
103
            );
104
        }
105
106
        $this->outputFormatTable($rows, $header);
107
    }
108
109
}
110