Completed
Push — master ( 823e67...71a230 )
by ANTHONIUS
03:16
created

PermissionsFixer::chmod()   A

Complexity

Conditions 2
Paths 5

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 5
nop 2
dl 0
loc 9
ccs 7
cts 7
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Yawik project.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Yawik\Composer;
11
12
use Core\Options\ModuleOptions as CoreOptions;
13
use Symfony\Component\Filesystem\Filesystem;
14
use Yawik\Composer\Event\ConfigureEvent;
15
16
/**
17
 * Class        PermissionsFixer
18
 * @package     Yawik\Composer
19
 * @author      Anthonius Munthi <http://itstoni.com>
20
 * @since       0.32.0
21
 */
22
class PermissionsFixer
23
{
24
    use LogTrait;
25
26
    /**
27
     * @var Filesystem
28
     */
29
    protected $filesystem;
30
31 7
    public function __construct()
32
    {
33 7
        $this->filesystem = new Filesystem();
34 7
    }
35
36 1
    public function onConfigureEvent(ConfigureEvent $event)
37
    {
38 1
        $modules        = $event->getModules();
39 1
        $options        = $event->getOptions();
40
41 1
        $this->fix($options, $modules);
42 1
    }
43
44 1
    public function fix(CoreOptions $options, array $modules)
45
    {
46 1
        $files          = [];
47 1
        $directories    = [];
48
49 1
        foreach ($modules as $module) {
50 1
            if ($module instanceof RequireFilePermissionInterface) {
51 1
                $modFileLists   = $module->getRequiredFileLists($options);
52 1
                if (!is_null($modFileLists) && !is_array($modFileLists)) {
53 1
                    $this->logError(sprintf(
54 1
                        '<comment>%s::getRequiredFileList()</comment> should return an array.',
55 1
                        get_class($module)
56
                    ));
57
                } else {
58 1
                    foreach ($modFileLists as $file) {
59 1
                        if (!in_array($file, $files)) {
60 1
                            $files[] = $file;
61
                        }
62
                    }
63
                }
64
            }
65
66 1
            if ($module instanceof RequireDirectoryPermissionInterface) {
67 1
                $modDirLists = $module->getRequiredDirectoryLists($options);
68 1
                if (!is_null($modDirLists) && !is_array($modDirLists)) {
69 1
                    $this->logError(sprintf(
70 1
                        '<comment>%s::getRequiredDirectoryList()</comment> should return an array.',
71 1
                        get_class($module)
72
                    ));
73
                } else {
74 1
                    foreach ($modDirLists as $directory) {
75 1
                        if (!in_array($directory, $directories)) {
76 1
                            $directories[] = $directory;
77
                        }
78
                    }
79
                }
80
            }
81
        }
82
83 1
        foreach ($directories as $directory) {
84
            try {
85 1
                if (!is_dir($directory)) {
86 1
                    $this->mkdir($directory);
87
                }
88 1
                $this->chmod($directory);
89
            } catch (\Exception $exception) {
90 1
                $this->logError($exception->getMessage());
91
            }
92
        }
93
94 1
        foreach ($files as $file) {
95
            try {
96 1
                if (!is_file($file)) {
97 1
                    $this->touch($file);
98
                }
99 1
                $this->chmod($file, 0666);
100
            } catch (\Exception $e) {
101 1
                $this->logError($e->getMessage());
102
            }
103
        }
104 1
    }
105
106 2
    public function touch($file)
107
    {
108
        try {
109 2
            $this->filesystem->touch($file);
110 1
            $this->log('created <info>'.$file.'</info>', 'touch');
111 1
        } catch (\Exception $exception) {
112 1
            $this->logError($exception->getMessage(), 'touch');
113
        }
114 2
    }
115
116 2
    public function chmod($dir, $mode = 0777)
117
    {
118
        try {
119 2
            $this->filesystem->chmod($dir, $mode);
120 1
            $fileperms  = decoct(@fileperms($dir) & 0777);
121 1
            $message    = sprintf('<comment>%s</comment> with %s', $dir, $fileperms);
122 1
            $this->log($message, 'chmod');
123 1
        } catch (\Exception $exception) {
124 1
            $this->logError($exception->getMessage(), 'chmod');
125
        }
126 2
    }
127
128 2
    public function mkdir($dir, $mode = 0777)
129
    {
130
        try {
131 2
            $this->filesystem->mkdir($dir, $mode);
132 1
            $this->log(sprintf('<comment>%s</comment>', $dir), 'mkdir');
133 1
        } catch (\Exception $e) {
134 1
            $this->logError($e->getMessage(), 'mkdir');
135
        }
136 2
    }
137
}
138