AddBundleToKernelCommand   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 0
dl 0
loc 84
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 7 1
A getBundlesArgument() 0 4 1
A execute() 0 7 1
A getKernel() 0 3 1
A updateBundles() 0 14 4
A generateSummary() 0 18 5
1
<?php
2
3
namespace BWC\Share\Symfony\Command;
4
5
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
6
use Symfony\Component\Console\Input\InputArgument;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use Sensio\Bundle\GeneratorBundle\Manipulator\KernelManipulator;
10
use Symfony\Component\HttpKernel\KernelInterface;
11
12
13
abstract class AddBundleToKernelCommand extends ContainerAwareCommand
14
{
15
    protected function configure() {
16
        $this
17
            ->setName('bwc:composer:bundles')
18
            ->setDescription('Adds bundles from composer to kernel')
19
            ->addArgument('bundles', InputArgument::IS_ARRAY)
20
        ;
21
    }
22
23
    /**
24
     * @param \Symfony\Component\Console\Input\InputInterface $input
25
     * @return string[];
0 ignored issues
show
Documentation introduced by
The doc-type string[]; could not be parsed: Expected "|" or "end of type", but got ";" at position 8. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
26
     */
27
    protected function getBundlesArgument(InputInterface $input) {
28
        $arrBundles = $input->getArgument('bundles');
29
        return $arrBundles;
30
    }
31
32
    protected function execute(InputInterface $input, OutputInterface $output) {
33
        $arrBundles = $this->getBundlesArgument($input);
34
        $kernel = $this->getKernel();
35
        $alreadyAdded = array();
36
        $ok = $this->updateBundles($arrBundles, $kernel, $output, $alreadyAdded);
37
        $this->generateSummary($output, $ok, $arrBundles, $alreadyAdded);
38
    }
39
40
41
    /**
42
     * @return KernelInterface
43
     */
44
    protected function getKernel() {
45
        return $this->getContainer()->get('kernel');
46
    }
47
48
49
    /**
50
     * @param string[] $arrBundles  array of full class names, all with namespace, of the bundles to be added to the kernel
51
     * @param KernelInterface $kernel
52
     * @param OutputInterface $output
53
     * @param string[] $alreadyAdded
54
     * @return bool
55
     */
56
    protected function updateBundles(array $arrBundles, KernelInterface $kernel, OutputInterface $output, &$alreadyAdded) {
57
        $manipulator = new KernelManipulator($kernel);
58
        $alreadyAdded = array();
59
        $result = true;
60
        foreach ($arrBundles as $bundle) {
61
            try {
62
                $ok = $manipulator->addBundle($bundle);
63
                $result = $result && $ok;
64
            } catch (\RuntimeException $ex) {
65
                $alreadyAdded[] = $bundle;
66
            }
67
        }
68
        return $result;
69
    }
70
71
72
    /**
73
     * @param OutputInterface $output
74
     * @param bool $ok
75
     * @param string[] $arrBundles
76
     * @param string[] $alreadyAdded
77
     */
78
    protected function generateSummary(OutputInterface $output, $ok, array $arrBundles, array $alreadyAdded) {
79
        if (!$ok) {
80
            $output->writeln('FAIL - Was not able to add all bundles automatically');
81
            $output->writeln('Ensure following bundles are added to AppKernel::registerBundles()');
82
            foreach ($arrBundles as $bundle) {
83
                $output->writeln("    $bundle");
84
            }
85
        } else {
86
            $count = count($arrBundles);
87
            $output->writeln("OK - Kernel updated with $count bundles defined in composer");
88
        }
89
        if (!empty($alreadyAdded)) {
90
            $output->writeln('Following bundles were already added to the kernel:');
91
            foreach ($alreadyAdded as $bundle) {
92
                $output->writeln("    $bundle");
93
            }
94
        }
95
    }
96
}