PermissionsFixer::fix()   F
last analyzed

Complexity

Conditions 18
Paths 784

Size

Total Lines 58
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 34
CRAP Score 18

Importance

Changes 4
Bugs 2 Features 1
Metric Value
cc 18
eloc 37
c 4
b 2
f 1
nc 784
nop 2
dl 0
loc 58
ccs 34
cts 34
cp 1
crap 18
rs 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 8
    public function __construct()
32
    {
33 8
        $this->filesystem = new Filesystem();
34 8
    }
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 2
    public function fix(CoreOptions $options, array $modules)
45
    {
46 2
        $files          = [];
47 2
        $directories    = [];
48
49 2
        foreach ($modules as $module) {
50 2
            if ($module instanceof RequireFilePermissionInterface) {
51 2
                $modFileLists   = $module->getRequiredFileLists($options);
52 2
                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 2
                    foreach ($modFileLists as $file) {
59 2
                        if (!in_array($file, $files)) {
60 2
                            $files[] = $file;
61
                        }
62
                    }
63
                }
64
            }
65
66 2
            if ($module instanceof RequireDirectoryPermissionInterface) {
67 2
                $modDirLists = $module->getRequiredDirectoryLists($options);
68 2
                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 2
                    foreach ($modDirLists as $directory) {
75 2
                        if (!in_array($directory, $directories)) {
76 2
                            $directories[] = $directory;
77
                        }
78
                    }
79
                }
80
            }
81
        }
82
83 2
        foreach ($directories as $directory) {
84
            try {
85 2
                if (!is_dir($directory)) {
86 2
                    $this->mkdir($directory);
87
                }
88 1
                $this->chmod($directory);
89 1
            } catch (\Exception $exception) {
90 1
                $this->logError($exception->getMessage());
91
            }
92
        }
93
94 2
        foreach ($files as $file) {
95
            try {
96 2
                if (!is_file($file)) {
97 2
                    $this->touch($file);
98
                }
99 1
                $this->chmod($file, 0666);
100 1
            } catch (\Exception $e) {
101 1
                $this->logError($e->getMessage());
102
            }
103
        }
104 2
    }
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