Completed
Branch master (7a7471)
by ANTHONIUS
05:27
created

PermissionsFixer::fix()   A

Complexity

Conditions 5
Paths 14

Size

Total Lines 31
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

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