Completed
Push — master ( 668b32...3793ef )
by WEBEWEB
01:37
created

UnzipAssetsCommand::displayFooter()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 3
nc 3
nop 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\Helper\AssetsHelper;
18
use WBW\Bundle\CoreBundle\Provider\AssetsProviderInterface;
19
20
/**
21
 * Unzip assets command.
22
 *
23
 * @author webeweb <https://github.com/webeweb/>
24
 * @package WBW\Bundle\CoreBundle\Command
25
 */
26
class UnzipAssetsCommand extends AbstractCommand {
27
28
    /**
29
     * Command help.
30
     *
31
     * @var string
32
     */
33
    const COMMAND_HELP = <<< 'EOT'
34
The <info>%command.name%</info> command unzips bundle assets into a given
35
directory (e.g. the <comment>public</comment> directory).
36
37
    <info>php %command.full_name% public</info>
38
39
EOT;
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    protected function configure() {
45
        $this
46
            ->setName("wbw:core:unzip-assets")
47
            ->setDescription("Unzip assets under a public directory")
48
            ->setHelp(self::COMMAND_HELP);
49
    }
50
51
    /**
52
     * Display the footer.
53
     *
54
     * @param StyleInterface $io The I/O.
55
     * @param int $exitCode The exit code.
56
     * @param int $count The count.
57
     * @return void
58
     */
59
    protected function displayFooter(StyleInterface $io, $exitCode, $count) {
60
        if (0 < $exitCode) {
61
            $io->error("Some errors occurred while unzipping assets");
62
            return;
63
        }
64
        if (0 === $count) {
65
            $io->success("No assets were provided by any bundle");
66
        }
67
        $io->success("All assets were succefully unzipped");
68
    }
69
70
    /**
71
     * Displays the header.
72
     *
73
     * @param StyleInterface $io The I/O.
74
     * @return void
75
     */
76
    protected function displayHeader(StyleInterface $io) {
77
        $io->newLine();
78
        $io->text("Trying to unzip assets");
79
        $io->newLine();
80
    }
81
82
    /**
83
     * Displays the result.
84
     *
85
     * @param StyleInterface $io The I/O.
86
     * @param array $results The results.
87
     * @return int Returns the exit code.
88
     */
89
    protected function displayResult(StyleInterface $io, array $results) {
90
91
        // Initialize.
92
        $success = $this->getCheckbox(true);
93
        $warning = $this->getCheckbox(false);
94
95
        $rows = [];
96
97
        $exitCode = 0;
98
99
        // Handle each result.
100
        foreach ($results as $bundle => $assets) {
101
            foreach ($assets as $asset => $result) {
102
103
                $rows[] = [
104
                    true === $result ? $success : $warning,
105
                    $bundle,
106
                    basename($asset),
107
                ];
108
109
                $exitCode += true === $result ? 0 : 1;
110
            }
111
        }
112
113
        // Display a table.
114
        $io->table(["", "Bundle", "Asset"], $rows);
115
116
        // Return the exit code.
117
        return $exitCode;
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123
    protected function execute(InputInterface $input, OutputInterface $output) {
124
125
        // Create an I/O.
126
        $io = $this->newStyle($input, $output);
127
        $this->displayHeader($io);
128
129
        // Initialize the results.
130
        $results = [];
131
132
        // Get the bundles.
133
        $bundles = $this->getApplication()->getKernel()->getBundles();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symfony\Component\Console\Application as the method getKernel() does only exist in the following sub-classes of Symfony\Component\Console\Application: Symfony\Bundle\FrameworkBundle\Console\Application. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
134
135
        // Handle each bundle.
136
        foreach ($bundles as $current) {
137
138
            // Check the bundle.
139
            if (false === ($current instanceOf AssetsProviderInterface)) {
140
                continue;
141
            }
142
143
            // Get the bundle path.
144
            $bundlePath = $current->getPath();
145
146
            // Initialize the directories.
147
            $assetsDirectory = $bundlePath . $current->getAssetsDirectory();
148
            $publicDirectory = $bundlePath . "/Resources/public";
149
150
            // Unzip the assets.
151
            $results[$current->getName()] = AssetsHelper::unzipAssets($assetsDirectory, $publicDirectory);
152
        }
153
154
        // Display the result.
155
        $exitCode = $this->displayResult($io, $results);
156
157
        // Display the footer.
158
        $this->displayFooter($io, $exitCode, count($results));
159
160
        // Return the exit code.
161
        return $exitCode;
162
    }
163
164
}
165