Passed
Branch master (05c66d)
by WEBEWEB
26:22
created

CopySkeletonCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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 Symfony\Component\HttpKernel\Kernel;
19
use WBW\Bundle\CoreBundle\Helper\KernelHelper;
20
use WBW\Bundle\CoreBundle\Helper\SkeletonHelper;
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 help.
33
     *
34
     * @var string
35
     */
36
    const COMMAND_HELP = <<< EOT
37
The <info>%command.name%</info> command copy bundle skeleton into <comment>app/Resources</comment>.
38
39
    <info>php %command.full_name%</info>
40
41
EOT;
42
43
    /**
44
     * Service name.
45
     *
46
     * @var string
47
     */
48
    const SERVICE_NAME = "wbw.core.command.copy_skeleton";
49
50
    /**
51
     * {@inheritDoc}
52
     */
53 121
    protected function configure(): void {
54
        $this
55 121
            ->setName("wbw:core:copy-skeleton")
56 121
            ->setDescription("Copy skeleton under the app/Resources directory")
57 121
            ->setHelp(self::COMMAND_HELP);
58 121
    }
59
60
    /**
61
     * Display the footer.
62
     *
63
     * @param StyleInterface $io The I/O.
64
     * @param int $exitCode The exit code.
65
     * @param int $count The count.
66
     * @return void
67
     */
68 44
    protected function displayFooter(StyleInterface $io, int $exitCode, int $count): void {
69 44
        if (0 < $exitCode) {
70 11
            $io->error("Some errors occurred while copying skeletons");
71 11
            return;
72
        }
73 33
        if (0 === $count) {
74 22
            $io->success("No skeleton were provided by any bundle");
75 22
            return;
76
        }
77 11
        $io->success("All skeletons were successfully copied");
78 11
    }
79
80
    /**
81
     * Displays the result.
82
     *
83
     * @param StyleInterface $io The I/O.
84
     * @param array $results The results.
85
     * @return int Returns the exit code.
86
     */
87 33
    protected function displayResult(StyleInterface $io, array $results): int {
88
89 33
        $exitCode = 0;
90
91 33
        $rows = [];
92
93 33
        $success = $this->getCheckbox(true);
94 33
        $warning = $this->getCheckbox(false);
95
96 33
        foreach ($results as $bundle => $assets) {
97 11
            foreach ($assets as $asset => $result) {
98
99 11
                $rows[] = [
100 11
                    true === $result ? $success : $warning,
101 11
                    $bundle,
102 11
                    basename($asset),
103
                ];
104
105 11
                $exitCode += true === $result ? 0 : 1;
106
            }
107
        }
108 33
        $io->table(["", "Bundle", "Resource"], $rows);
109
110 33
        return $exitCode;
111
    }
112
113
    /**
114
     * {@inheritDoc}
115
     */
116 11
    protected function execute(InputInterface $input, OutputInterface $output): int {
117
118 11
        $io = $this->newStyle($input, $output);
119 11
        $this->displayTitle($io, $this->getDescription());
120 11
        $this->displayHeader($io, "Trying to copy skeletons");
121
122 11
        $results = [];
123
124 11
        $bundles = $this->getKernel()->getBundles();
125 11
        foreach ($bundles as $current) {
126
127 11
            if (false === ($current instanceof SkeletonProviderInterface)) {
128 11
                continue;
129
            }
130
131
            $bundlePath = $current->getPath();
0 ignored issues
show
Bug introduced by
The method getPath() does not seem to exist on object<WBW\Bundle\CoreBu...letonProviderInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
132
133
            $skeletonDirectory  = $bundlePath . $current->getSkeletonRelativeDirectory();
134
            $resourcesDirectory = $this->getResourcesDirectory();
135
136
            $results[$current->getName()] = SkeletonHelper::copySkeleton($skeletonDirectory, $resourcesDirectory);
0 ignored issues
show
Bug introduced by
The method getName() does not seem to exist on object<WBW\Bundle\CoreBu...letonProviderInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
137
        }
138
139 11
        $exitCode = $this->displayResult($io, $results);
140 11
        $this->displayFooter($io, $exitCode, count($results));
141
142 11
        return $exitCode;
143
    }
144
145
    /**
146
     * Get the resources directory.
147
     *
148
     * @return string Returns the resources directory.
149
     * @throws InvalidArgumentException Throws an invalid argument exception is the kernel is null.
150
     */
151 22
    protected function getResourcesDirectory(): string {
152
153 22
        $kernel = $this->getKernel();
154 22
        if (null === $kernel) {
155 11
            throw new InvalidArgumentException("The application kernel is null");
156
        }
157
158 11
        $rootDir = KernelHelper::getProjectDir($kernel);
159
160 11
        if (40000 <= Kernel::VERSION_ID) {
161 9
            return $rootDir . "/templates/bundles";
162
        }
163
164 2
        return $rootDir . "/app/Resources";
165
    }
166
}
167