Passed
Push — master ( ce1e4f...1e5872 )
by Josh
04:31
created

ListMODXPackages::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 16
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 19
rs 9.7333
1
<?php
2
3
namespace LCI\MODX\Orchestrator\Console\Command;
4
5
use LCI\Blend\Exception\TransportException;
6
use LCI\Blend\Exception\TransportNotFoundException;
7
use LCI\Blend\Transport\MODXPackages;
8
use LCI\MODX\Console\Command\BaseCommand;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Symfony\Component\Console\Style\SymfonyStyle;
13
14
class ListMODXPackages extends BaseCommand
15
{
16
    public $loadMODX = true;
17
18
    protected function configure()
19
    {
20
        $this
21
            ->setName('modx:list-packages')
22
            ->setAliases(['extras'])
23
            ->setDescription('Require a MODX Extra Package')
24
            ->addOption(
25
                'update',
26
                'u',
27
                InputOption::VALUE_OPTIONAL,
28
                'Update all packages',
29
                false
30
            )
31
            ->addOption(
32
                'latest',
33
                'l',
34
                InputOption::VALUE_OPTIONAL,
35
                'Get the latest version of this extra. Only used when updating all packages',
36
                true
37
            );
38
    }
39
40
    /**
41
     * Runs the command.
42
     *
43
     * @param InputInterface $input
44
     * @param OutputInterface $output
45
     * @return int|null|void
46
     */
47
    protected function execute(InputInterface $input, OutputInterface $output)
48
    {
49
        $update_all = (bool)$input->getOption('update');
50
        $latest = (bool)$input->getOption('latest');
51
52
        $modx = $this->console->loadMODX();
53
        $modxPackages = new MODXPackages($modx, $this->consoleUserInteractionHandler);
54
55
        $io = new SymfonyStyle($input, $output);
56
57
58
        $io->title('Installed MODX Extras');
59
60
        $current_packages = $modxPackages->getList(100);
61
        $packages = [];
62
63
        if ($current_packages['total'] > 0) {
64
            foreach ($current_packages['packages'] as $package) {
65
                $update = 'No';
66
                if (isset($package['updates']) && isset($package['updates']['count']) && $package['updates']['count'] > 0) {
67
                    $update = '';
68
                    foreach ($package['updates']['versions'] as $key => $version) {
69
                        $update .= ', ' . $version['version'];
70
                    }
71
                    $update = 'Yes ' . trim(trim($update, ','));
72
                }
73
74
                $installed = 'No';
75
                if (!is_null($package['installed']) && $package['installed'] != '0000-00-00 00:00:00') {
76
                    $installed = utf8_encode(date($modx->getOption('manager_date_format') . ', ' . $modx->getOption('manager_time_format'), strtotime($package['installed'])));
77
                }
78
79
                if ($update_all && $update != 'No') {
80
                    // update
81
                    $output->writeln('### Orchestrator is attempting to update the MODX Extra Package: ' . $package['signature'] .' ###');
82
83
                    try {
84
                        $modxPackages->requirePackage($package['signature'], $latest);
85
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
86
                    } catch (TransportNotFoundException $exception) {
87
                        $output->writeln('Error: '.$exception->getMessage());
88
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
89
                    } catch (TransportException $exception) {
90
                        $output->writeln('Error: '.$exception->getMessage());
91
                    }
92
                }
93
94
                $packages[] = [
95
                    $package['package_name'],
96
                    $package['signature'],
97
                    $package['metadata']['version'],
98
                    $installed,
99
                    $update
100
                ];
101
            }
102
        }
103
        $table_header = ['Name', 'Signature', 'Version', 'Installed', 'Update Available'];
104
105
        $io->table($table_header, $packages);
106
107
108
        $output->writeln($this->getRunStats());
109
    }
110
}
111