ExtensionsCommand::execute()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 12
cts 12
cp 1
rs 9.52
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 3
1
<?php
2
3
namespace SilverLeague\Console\Command\Object;
4
5
use SilverLeague\Console\Command\SilverStripeCommand;
6
use SilverLeague\Console\Framework\Utility\ObjectUtilities;
7
use SilverStripe\Core\Config\Config;
8
use SilverStripe\Core\Injector\Injector;
9
use Symfony\Component\Console\Helper\Table;
10
use Symfony\Component\Console\Input\InputArgument;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Output\OutputInterface;
13
14
/**
15
 * List all extensions of a given Object, e.g. "Page"
16
 *
17
 * @package silverstripe-console
18
 * @author  Robbie Averill <[email protected]>
19
 */
20
class ExtensionsCommand extends SilverStripeCommand
21
{
22
    use ObjectUtilities;
23
24 3
    /**
25
     * {@inheritDoc}
26
     */
27 3
    protected function configure()
28 3
    {
29 3
        $this
30 3
            ->setName('object:extensions')
31
            ->setDescription('List all Extensions of a given Object, e.g. "Page"')
32
            ->addArgument('object', InputArgument::REQUIRED, 'The Object to find Extensions for');
33
    }
34
35 1
    /**
36
     * {@inheritDoc}
37 1
     */
38 1
    protected function execute(InputInterface $input, OutputInterface $output)
39 1
    {
40
        $object = $input->getArgument('object');
41
42
        $instance = Injector::inst()->create($object);
43 1
        // Not all classes implement Extensible
44
        if (!method_exists($instance, 'get_extensions')) {
45 1
            $output->writeln('<error>' . $object . ' doesn\'t allow extensions (implement Extensible)</error>');
46 1
            return;
47
        }
48 1
49 1
        $extensions = $instance->get_extensions();
50
        if (!$extensions) {
51 1
            $output->writeln('<error>There are no Extensions registered for ' . $object . '</error>');
52 1
            return;
53 1
        }
54 1
        sort($extensions);
55
56
        $output->writeln('<info>Extensions for ' . $object . ':</info>');
57
        $table = new Table($output);
58
        $table
59
            ->setHeaders($this->getHeaders())
60
            ->setRows($this->getRows($extensions))
61
            ->render();
62 2
    }
63
64 2
    /**
65 2
     * Return the header cells for the output table. CMS classes have an extra column.
66 1
     *
67
     * @return string[]
68
     */
69 2
    public function getHeaders()
70
    {
71
        return ['Class name', 'Module', 'Added DB fields'];
72
    }
73
74
    /**
75
     * Return the rows for the output table containing extension statistics.
76
     *
77
     * @return string[]
78 2
     */
79
    public function getRows($extensions)
80 2
    {
81 2
        $tableRows = [];
82
        foreach ($extensions as $extensionClass) {
83 2
            $row = [
84
                $extensionClass,
85 2
                // Add the module name
86
                $this->getModuleName($extensionClass),
87
                // Add the number of DB fields that the class adds
88 2
                count((array) Config::inst()->get($extensionClass, 'db', Config::UNINHERITED)),
89
            ];
90 1
91
            $tableRows[] = $row;
92
        }
93 2
        return $tableRows;
94
    }
95
}
96