CopySkeletonCommand::displayFooter()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
dl 0
loc 13
rs 10
c 1
b 0
f 0
ccs 7
cts 7
cp 1
cc 3
nc 3
nop 3
crap 3
1
<?php
2
3
/*
4
 * This file is part of the core-bundle package.
5
 *
6
 * (c) 2019 WEBEWEB
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WBW\Bundle\CoreBundle\Command;
13
14
use InvalidArgumentException;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Output\OutputInterface;
17
use Symfony\Component\Console\Style\StyleInterface;
18
use WBW\Bundle\CoreBundle\Console\ConsoleHelper;
19
use WBW\Bundle\CoreBundle\Helper\SkeletonHelper;
20
use WBW\Bundle\CoreBundle\HttpKernel\KernelHelper;
21
use WBW\Bundle\CoreBundle\Provider\SkeletonProviderInterface;
22
23
/**
24
 * Copy skeleton command.
25
 *
26
 * @author webeweb <https://github.com/webeweb>
27
 * @package WBW\Bundle\CoreBundle\Command\Command
28
 */
29
class CopySkeletonCommand extends AbstractCommand {
30
31
    /**
32
     * Command name.
33
     *
34
     * @var string
35
     */
36
    const COMMAND_NAME = "wbw:core:copy-skeleton";
37
38
    /**
39
     * Service name.
40
     *
41
     * @var string
42
     */
43
    const SERVICE_NAME = "wbw.core.command.copy_skeleton";
44
45
    /**
46
     * {@inheritDoc}
47
     */
48
    protected function configure(): void {
49
        $this
50
            ->setDescription("Copy skeleton under the app/Resources directory")
51
            ->setHelp(ConsoleHelper::formatCommandHelp("copy bundle skeleton into <comment>app/Resources</comment>"))
52
            ->setName(self::COMMAND_NAME);
53 110
    }
54
55 110
    /**
56 110
     * Display the footer.
57 110
     *
58 110
     * @param StyleInterface $io The I/O.
59
     * @param int $exitCode The exit code.
60
     * @param int $count The count.
61
     * @return void
62
     */
63
    protected function displayFooter(StyleInterface $io, int $exitCode, int $count): void {
64
65
        if (0 < $exitCode) {
66
            $io->error("Some errors occurred while copying skeletons");
67
            return;
68 40
        }
69 40
70 10
        if (0 === $count) {
71 10
            $io->success("No skeleton were provided by any bundle");
72
            return;
73 30
        }
74 20
75 20
        $io->success("All skeletons were successfully copied");
76
    }
77 10
78 10
    /**
79
     * Display the result.
80
     *
81
     * @param StyleInterface $io The I/O.
82
     * @param array $results The results.
83
     * @return int Returns the exit code.
84
     */
85
    protected function displayResult(StyleInterface $io, array $results): int {
86
87 30
        $exitCode = 0;
88
89 30
        $rows = [];
90
91 30
        $success = $this->getCheckbox(true);
92
        $warning = $this->getCheckbox(false);
93 30
94 30
        foreach ($results as $bundle => $assets) {
95
            foreach ($assets as $asset => $result) {
96 30
97 10
                $rows[] = [
98
                    true === $result ? $success : $warning,
99 10
                    $bundle,
100 10
                    basename($asset),
101 10
                ];
102 10
103
                $exitCode += true === $result ? 0 : 1;
104
            }
105 10
        }
106
        $io->table(["", "Bundle", "Resource"], $rows);
107
108 30
        return $exitCode;
109
    }
110 30
111
    /**
112
     * {@inheritDoc}
113
     */
114
    protected function execute(InputInterface $input, OutputInterface $output): int {
115
116 10
        $io = $this->newStyle($input, $output);
117
        $this->displayTitle($io, $this->getDescription());
118 10
        $this->displayHeader($io, "Trying to copy skeletons");
119 10
120 10
        $results = [];
121
122 10
        $bundles = $this->getKernel()->getBundles();
123
        foreach ($bundles as $current) {
124 10
125 10
            if (false === ($current instanceof SkeletonProviderInterface)) {
126
                continue;
127 10
            }
128 10
129
            $bundlePath = $current->getPath();
130
131
            $skeletonDirectory  = $bundlePath . $current->getSkeletonRelativeDirectory();
132
            $resourcesDirectory = $this->getResourcesDirectory();
133
134
            $results[$current->getName()] = SkeletonHelper::copySkeleton($skeletonDirectory, $resourcesDirectory);
135
        }
136
137
        $exitCode = $this->displayResult($io, $results);
138
        $this->displayFooter($io, $exitCode, count($results));
139 10
140 10
        return $exitCode;
141
    }
142 10
143
    /**
144
     * Get the resources directory.
145
     *
146
     * @return string Returns the resources directory.
147
     * @throws InvalidArgumentException Throws an invalid argument exception is the kernel is null.
148
     */
149
    protected function getResourcesDirectory(): string {
150
151 20
        $kernel = $this->getKernel();
152
        if (null === $kernel) {
153 20
            throw new InvalidArgumentException("The application kernel is null");
154 20
        }
155 10
156
        $rootDir = KernelHelper::getProjectDir($kernel);
157
158 10
        return $rootDir . "/templates/bundles";
159
    }
160
}
161