Completed
Push — master ( 5c40ca...712b76 )
by WEBEWEB
01:59
created

UnzipAssetsCommand::execute()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
c 0
b 0
f 0
rs 9.408
cc 4
nc 4
nop 2
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\Bundle\FrameworkBundle\Console\Application;
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\Helper\AssetsHelper;
19
use WBW\Bundle\CoreBundle\Provider\AssetsProviderInterface;
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 help.
31
     *
32
     * @var string
33
     */
34
    const COMMAND_HELP = <<< EOT
35
The <info>%command.name%</info> command unzips bundle assets into <comment>public</comment>.
36
37
    <info>php %command.full_name%</info>
38
39
EOT;
40
41
    /**
42
     * Service name.
43
     *
44
     * @var string
45
     */
46
    const SERVICE_NAME = "wbw.core.command.unzip_assets";
47
48
    /**
49
     * {@inheritDoc}
50
     */
51
    protected function configure() {
52
        $this
53
            ->setName("wbw:core:unzip-assets")
54
            ->setDescription("Unzip assets under a public directory")
55
            ->setHelp(self::COMMAND_HELP);
56
    }
57
58
    /**
59
     * Display the footer.
60
     *
61
     * @param StyleInterface $io The I/O.
62
     * @param int $exitCode The exit code.
63
     * @param int $count The count.
64
     * @return void
65
     */
66
    protected function displayFooter(StyleInterface $io, $exitCode, $count) {
67
        if (0 < $exitCode) {
68
            $io->error("Some errors occurred while unzipping assets");
69
            return;
70
        }
71
        if (0 === $count) {
72
            $io->success("No assets were provided by any bundle");
73
            return;
74
        }
75
        $io->success("All assets were successfully unzipped");
76
    }
77
78
    /**
79
     * Displays 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) {
86
87
        $exitCode = 0;
88
89
        $rows = [];
90
91
        $success = $this->getCheckbox(true);
92
        $warning = $this->getCheckbox(false);
93
94
        // Handle each result.
95
        foreach ($results as $bundle => $assets) {
96
            foreach ($assets as $asset => $result) {
97
98
                $rows[] = [
99
                    true === $result ? $success : $warning,
100
                    $bundle,
101
                    basename($asset),
102
                ];
103
104
                $exitCode += true === $result ? 0 : 1;
105
            }
106
        }
107
108
        // Displays a table.
109
        $io->table(["", "Bundle", "Asset"], $rows);
110
111
        return $exitCode;
112
    }
113
114
    /**
115
     * {@inheritDoc}
116
     */
117
    protected function execute(InputInterface $input, OutputInterface $output) {
118
119
        if (false === ($this->getApplication() instanceof Application)) {
120
            return -1;
121
        }
122
123
        $io = $this->newStyle($input, $output);
124
        $this->displayHeader($io, "Trying to unzip assets");
125
126
        $results = [];
127
128
        $bundles = $this->getApplication()->getKernel()->getBundles();
129
130
        foreach ($bundles as $current) {
131
132
            if (false === ($current instanceof AssetsProviderInterface)) {
133
                continue;
134
            }
135
136
            $bundlePath = $current->getPath();
137
138
            $assetsDirectory = $bundlePath . $current->getAssetsRelativeDirectory();
139
            $publicDirectory = $bundlePath . "/Resources/public";
140
141
            $results[$current->getName()] = AssetsHelper::unzipAssets($assetsDirectory, $publicDirectory);
142
        }
143
144
        $exitCode = $this->displayResult($io, $results);
145
        $this->displayFooter($io, $exitCode, count($results));
146
147
        return $exitCode;
148
    }
149
}
150