UnzipAssetsCommand::execute()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 27
rs 9.7666
c 0
b 0
f 0
ccs 15
cts 15
cp 1
cc 3
nc 3
nop 2
crap 3
1
<?php
2
3
/*
4
 * This file is part of the core-bundle package.
5
 *
6
 * (c) 2018 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 Symfony\Component\Console\Input\InputInterface;
15
use Symfony\Component\Console\Output\OutputInterface;
16
use Symfony\Component\Console\Style\StyleInterface;
17
use WBW\Bundle\CoreBundle\Console\ConsoleHelper;
18
use WBW\Bundle\CoreBundle\Provider\AssetsProviderInterface;
19
use WBW\Library\Symfony\Helper\AssetsHelper;
20
21
/**
22
 * Unzip assets command.
23
 *
24
 * @author webeweb <https://github.com/webeweb>
25
 * @package WBW\Bundle\CoreBundle\Command
26
 */
27
class UnzipAssetsCommand extends AbstractCommand {
28
29
    /**
30
     * Command name.
31
     *
32
     * @var string
33
     */
34
    const COMMAND_NAME = "wbw:core:unzip-assets";
35
36
    /**
37
     * Service name.
38
     *
39
     * @var string
40
     */
41
    const SERVICE_NAME = "wbw.core.command.unzip_assets";
42
43
    /**
44
     * {@inheritDoc}
45
     */
46
    protected function configure(): void {
47
        $this
48
            ->setDescription("Unzip assets under a public directory")
49
            ->setHelp(ConsoleHelper::formatCommandHelp("unzips bundle assets into <comment>public</comment>"))
50 90
            ->setName(self::COMMAND_NAME);
51
    }
52 90
53 90
    /**
54 90
     * Display the footer.
55 90
     *
56
     * @param StyleInterface $io The I/O.
57
     * @param int $exitCode The exit code.
58
     * @param int $count The count.
59
     * @return void
60
     */
61
    protected function displayFooter(StyleInterface $io, int $exitCode, int $count): void {
62
63
        if (0 < $exitCode) {
64
            $io->error("Some errors occurred while unzipping assets");
65 40
            return;
66 40
        }
67 10
68 10
        if (0 === $count) {
69
            $io->success("No assets were provided by any bundle");
70 30
            return;
71 10
        }
72 10
73
        $io->success("All assets were successfully unzipped");
74 20
    }
75 20
76
    /**
77
     * Display the result.
78
     *
79
     * @param StyleInterface $io The I/O.
80
     * @param array $results The results.
81
     * @return int Returns the exit code.
82
     */
83
    protected function displayResult(StyleInterface $io, array $results): int {
84 30
85
        $exitCode = 0;
86 30
87
        $rows = [];
88 30
89
        $success = $this->getCheckbox(true);
90 30
        $warning = $this->getCheckbox(false);
91 30
92
        foreach ($results as $bundle => $assets) {
93 30
            foreach ($assets as $asset => $result) {
94 20
95
                $rows[] = [
96 20
                    true === $result ? $success : $warning,
97 20
                    $bundle,
98 20
                    basename($asset),
99 20
                ];
100
101
                $exitCode += true === $result ? 0 : 1;
102 20
            }
103
        }
104
105
        $io->table(["", "Bundle", "Asset"], $rows);
106 30
107
        return $exitCode;
108 30
    }
109
110
    /**
111
     * {@inheritDoc}
112
     */
113
    protected function execute(InputInterface $input, OutputInterface $output): int {
114 10
115
        $io = $this->newStyle($input, $output);
116 10
        $this->displayTitle($io, $this->getDescription());
117 10
        $this->displayHeader($io, "Trying to unzip assets");
118 10
119
        $results = [];
120 10
121
        $bundles = $this->getKernel()->getBundles();
122 10
        foreach ($bundles as $current) {
123 10
124
            if (false === ($current instanceof AssetsProviderInterface)) {
125 10
                continue;
126 10
            }
127
128
            $bundlePath = $current->getPath();
129 10
130
            $assetsDirectory = $bundlePath . $current->getAssetsRelativeDirectory();
131 10
            $publicDirectory = $bundlePath . "/Resources/public";
132 10
133
            $results[$current->getName()] = AssetsHelper::unzipAssets($assetsDirectory, $publicDirectory);
134 10
        }
135
136
        $exitCode = $this->displayResult($io, $results);
137 10
        $this->displayFooter($io, $exitCode, count($results));
138 10
139
        return $exitCode;
140 10
    }
141
}
142