Completed
Push — master ( d81c19...f57266 )
by Kamil
20s
created

Asset/Installer/OutputAwareAssetsInstaller.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
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
declare(strict_types=1);
13
14
namespace Sylius\Bundle\ThemeBundle\Asset\Installer;
15
16
use Symfony\Component\Console\Output\NullOutput;
17
use Symfony\Component\Console\Output\OutputInterface;
18
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
19
20
final class OutputAwareAssetsInstaller implements AssetsInstallerInterface, OutputAwareInterface
21
{
22
    /**
23
     * @var AssetsInstallerInterface
24
     */
25
    private $assetsInstaller;
26
27
    /**
28
     * @var OutputInterface
29
     */
30
    private $output;
31
32
    /**
33
     * @param AssetsInstallerInterface $assetsInstaller
34
     */
35
    public function __construct(AssetsInstallerInterface $assetsInstaller)
36
    {
37
        $this->assetsInstaller = $assetsInstaller;
38
        $this->output = new NullOutput();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Symfony\Component\Console\Output\NullOutput() of type object<Symfony\Component...sole\Output\NullOutput> is incompatible with the declared type object<Symfony\Component...Output\OutputInterface> of property $output.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function setOutput(OutputInterface $output): void
45
    {
46
        $this->output = $output;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function installAssets(string $targetDir, int $symlinkMask): int
53
    {
54
        $this->output->writeln($this->provideExpectationComment($symlinkMask));
55
56
        return $this->assetsInstaller->installAssets($targetDir, $symlinkMask);
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function installBundleAssets(BundleInterface $bundle, string $targetDir, int $symlinkMask): int
63
    {
64
        $this->output->writeln(sprintf(
65
            'Installing assets for <comment>%s</comment> into <comment>%s</comment>',
66
            $bundle->getNamespace(),
67
            $targetDir
68
        ));
69
70
        $effectiveSymlinkMask = $this->assetsInstaller->installBundleAssets($bundle, $targetDir, $symlinkMask);
71
72
        $this->output->writeln($this->provideResultComment($symlinkMask, $effectiveSymlinkMask));
73
74
        return $effectiveSymlinkMask;
75
    }
76
77
    /**
78
     * @param int $symlinkMask
79
     * @param int $effectiveSymlinkMask
80
     *
81
     * @return string
82
     */
83
    private function provideResultComment(int $symlinkMask, int $effectiveSymlinkMask): string
84
    {
85
        if ($effectiveSymlinkMask === $symlinkMask) {
86
            switch ($symlinkMask) {
87
                case AssetsInstallerInterface::HARD_COPY:
88
                    return 'The assets were copied.';
89
                case AssetsInstallerInterface::SYMLINK:
90
                    return 'The assets were installed using symbolic links.';
91
                case AssetsInstallerInterface::RELATIVE_SYMLINK:
92
                    return 'The assets were installed using relative symbolic links.';
93
            }
94
        }
95
96
        switch ($symlinkMask + $effectiveSymlinkMask) {
97
            case AssetsInstallerInterface::SYMLINK:
98
            case AssetsInstallerInterface::RELATIVE_SYMLINK:
99
                return 'It looks like your system doesn\'t support symbolic links, so the assets were copied.';
100
            case AssetsInstallerInterface::RELATIVE_SYMLINK + AssetsInstallerInterface::SYMLINK:
101
                return 'It looks like your system doesn\'t support relative symbolic links, so the assets were installed by using absolute symbolic links.';
102
        }
103
104
        return 'Something gone bad, can\'t provide the result of assets installing!';
105
    }
106
107
    /**
108
     * @param int $symlinkMask
109
     *
110
     * @return string
111
     */
112
    private function provideExpectationComment(int $symlinkMask): string
113
    {
114
        if (AssetsInstallerInterface::HARD_COPY === $symlinkMask) {
115
            return 'Installing assets as <comment>hard copies</comment>.';
116
        }
117
118
        return 'Trying to install assets as <comment>symbolic links</comment>.';
119
    }
120
}
121