Completed
Branch master (50a96e)
by ANTHONIUS
05:47
created

PermissionsFixer::getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
ccs 2
cts 2
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 Symfony\Component\Filesystem\Filesystem;
13
use Yawik\Composer\Event\ConfigureEvent;
14
15
/**
16
 * Class        PermissionsFixer
17
 * @package     Yawik\Composer
18
 * @author      Anthonius Munthi <http://itstoni.com>
19
 * @since       0.32.0
20
 */
21
class PermissionsFixer
22
{
23
    use LogTrait;
24
25
    /**
26
     * @var Filesystem
27
     */
28
    protected $filesystem;
29
30 7
    public function __construct()
31
    {
32 7
        $this->filesystem = new Filesystem();
33 7
    }
34
35 1
    public function onConfigureEvent(ConfigureEvent $event)
36
    {
37 1
        $modules        = $event->getModules();
38 1
        $options        = $event->getOptions();
39 1
        $files          = [];
40 1
        $directories    = [];
41
42 1
        foreach ($modules as $module) {
43 1
            if ($module instanceof PermissionsFixerModuleInterface) {
44 1
                $modDirLists    = $module->getDirectoryPermissionLists($options);
45 1
                $modFileLists   = $module->getFilePermissionLists($options);
46
47 1
                if (!is_null($modDirLists) && !is_array($modDirLists)) {
48 1
                    $this->logError(sprintf(
49 1
                        '<comment>%s::getDirectoryPermissionList()</comment> should return an array.',
50 1
                        get_class($module)
51
                    ));
52
                } else {
53 1
                    $directories    = array_merge($directories, $modDirLists);
54
                }
55
56 1
                if (!is_null($modFileLists) && !is_array($modFileLists)) {
57 1
                    $this->logError(sprintf(
58 1
                        '<comment>%s::getFilePermissionList()</comment> should return an array.',
59 1
                        get_class($module)
60
                    ));
61
                } else {
62 1
                    $files = array_merge($files, $modFileLists);
63
                }
64
            }
65
        }
66
67 1
        foreach ($directories as $directory) {
68 1
            if (!is_dir($directory)) {
69 1
                $this->mkdir($directory);
70
            }
71 1
            $this->chmod($directory);
72
        }
73
74 1
        foreach ($files as $file) {
75 1
            if (!is_file($file)) {
76 1
                $this->touch($file);
77
            }
78 1
            $this->chmod($file, 0666);
79
        }
80 1
    }
81
82 2
    public function touch($file)
83
    {
84
        try {
85 2
            $this->filesystem->touch($file);
86 1
            $this->log('created <info>'.$file.'</info>', 'touch');
87 1
        } catch (\Exception $exception) {
88 1
            $this->logError($exception->getMessage(), 'touch');
89
        }
90 2
    }
91
92 2
    public function chmod($dir, $mode = 0777)
93
    {
94
        try {
95 2
            $this->filesystem->chmod($dir, $mode);
96 1
            $fileperms  = decoct(@fileperms($dir) & 0777);
97 1
            $message    = sprintf('<comment>%s</comment> with %s', $dir, $fileperms);
98 1
            $this->log($message, 'chmod');
99 1
        } catch (\Exception $exception) {
100 1
            $this->logError($exception->getMessage(), 'chmod');
101
        }
102 2
    }
103
104 2
    public function mkdir($dir, $mode = 0777)
105
    {
106
        try {
107 2
            $this->filesystem->mkdir($dir, $mode);
108 1
            $this->log(sprintf('<comment>%s</comment>', $dir), 'mkdir');
109 1
        } catch (\Exception $e) {
110 1
            $this->logError($e->getMessage(), 'mkdir');
111
        }
112 2
    }
113
}
114