Completed
Branch master (5a33e7)
by ANTHONIUS
02:09
created

PermissionsFixer::onConfigureEvent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
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
                    $files = array_merge($files, $modFileLists);
59
                }
60
            }
61
62 1
            if ($module instanceof RequireDirectoryPermissionInterface) {
63 1
                $modDirLists = $module->getRequiredDirectoryLists($options);
64 1
                if (!is_null($modDirLists) && !is_array($modDirLists)) {
65 1
                    $this->logError(sprintf(
66 1
                        '<comment>%s::getRequiredDirectoryList()</comment> should return an array.',
67 1
                        get_class($module)
68
                    ));
69
                } else {
70 1
                    $directories    = array_merge($directories, $modDirLists);
71
                }
72
            }
73
        }
74
75 1
        foreach ($directories as $directory) {
76 1
            if (!is_dir($directory)) {
77 1
                $this->mkdir($directory);
78
            }
79 1
            $this->chmod($directory);
80
        }
81
82 1
        foreach ($files as $file) {
83 1
            if (!is_file($file)) {
84 1
                $this->touch($file);
85
            }
86 1
            $this->chmod($file, 0666);
87
        }
88 1
    }
89
90 2
    public function touch($file)
91
    {
92
        try {
93 2
            $this->filesystem->touch($file);
94 1
            $this->log('created <info>'.$file.'</info>', 'touch');
95 1
        } catch (\Exception $exception) {
96 1
            $this->logError($exception->getMessage(), 'touch');
97
        }
98 2
    }
99
100 2
    public function chmod($dir, $mode = 0777)
101
    {
102
        try {
103 2
            $this->filesystem->chmod($dir, $mode);
104 1
            $fileperms  = decoct(@fileperms($dir) & 0777);
105 1
            $message    = sprintf('<comment>%s</comment> with %s', $dir, $fileperms);
106 1
            $this->log($message, 'chmod');
107 1
        } catch (\Exception $exception) {
108 1
            $this->logError($exception->getMessage(), 'chmod');
109
        }
110 2
    }
111
112 2
    public function mkdir($dir, $mode = 0777)
113
    {
114
        try {
115 2
            $this->filesystem->mkdir($dir, $mode);
116 1
            $this->log(sprintf('<comment>%s</comment>', $dir), 'mkdir');
117 1
        } catch (\Exception $e) {
118 1
            $this->logError($e->getMessage(), 'mkdir');
119
        }
120 2
    }
121
}
122