Completed
Push — npm-shrinkwrap ( 6d6cb0...13853e )
by Kamil
72:46 queued 46:53
created

CommandDirectoryChecker::changePermissions()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 3
nop 2
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
namespace Sylius\Bundle\CoreBundle\Installer\Checker;
13
14
use Symfony\Component\Console\Output\OutputInterface;
15
use Symfony\Component\Filesystem\Exception\IOException;
16
use Symfony\Component\Filesystem\Filesystem;
17
18
/**
19
 * @author Mateusz Zalewski <[email protected]>
20
 */
21
final class CommandDirectoryChecker
22
{
23
    /**
24
     * @var string
25
     */
26
    private $name;
27
28
    /**
29
     * @var Filesystem
30
     */
31
    private $filesystem;
32
33
    /**
34
     * @param Filesystem $filesystem
35
     */
36
    public function __construct(Filesystem $filesystem)
37
    {
38
        $this->filesystem = $filesystem;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function ensureDirectoryExists($directory, OutputInterface $output)
45
    {
46
        if (is_dir($directory)) {
47
            return;
48
        }
49
50
        try {
51
            $this->filesystem->mkdir($directory, 0755);
52
53
            $output->writeln(sprintf('<comment>Created "%s" directory.</comment>', realpath($directory)));
54
        } catch (IOException $exception) {
55
            $output->writeln('');
56
            $output->writeln('<error>Cannot run command due to unexisting directory (tried to create it automatically, failed).</error>');
57
            $output->writeln('');
58
59
            throw new \RuntimeException(sprintf(
60
                'Create directory "%s" and run command "<comment>%s</comment>"',
61
                realpath($directory),
62
                $this->name
63
            ));
64
        }
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function ensureDirectoryIsWritable($directory, OutputInterface $output)
71
    {
72
        if (is_writable($directory)) {
73
            return;
74
        }
75
76
        try {
77
            $this->filesystem->chmod($directory, 0755);
78
79
            $output->writeln(sprintf('<comment>Changed "%s" permissions to 0755.</comment>', realpath($directory)));
80
        } catch (IOException $exception) {
81
            $output->writeln('');
82
            $output->writeln('<error>Cannot run command due to bad directory permissions (tried to change permissions to 0755).</error>');
83
            $output->writeln('');
84
85
            throw new \RuntimeException(sprintf(
86
                'Set "%s" writable and run command "<comment>%s</comment>"',
87
                realpath(dirname($directory)),
88
                $this->name
89
            ));
90
        }
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    public function setCommandName($name)
97
    {
98
        $this->name = $name;
99
    }
100
}
101