Completed
Push — master ( 7e70c2...f499f2 )
by ANTHONIUS
03:01
created

PermissionsFixer::chmod()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 2
nop 2
dl 0
loc 8
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\Application;
13
use Core\Options\ModuleOptions as CoreOptions;
14
use Symfony\Component\Filesystem\Filesystem;
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
    private $filesystem;
30
31
    public function __construct()
32
    {
33
        $this->filesystem = new Filesystem();
34
    }
35
36
    /**
37
     * @param Filesystem $filesystem
38
     * @return PermissionsFixer
39
     */
40
    public function setFilesystem($filesystem)
41
    {
42
        $this->filesystem = $filesystem;
43
        return $this;
44
    }
45
46
    /**
47
     *
48
     */
49
    public function fix()
50
    {
51
        /* @var CoreOptions $options */
52
        $app        = Application::init();
53
        $options    = $app->getServiceManager()->get('Core/Options');
54
55
        $logDir     = $options->getLogDir();
56
        $cacheDir   = $options->getCacheDir();
57
        $configDir  = realpath(Application::getConfigDir());
58
59
        $dirs = [
60
            $configDir.'/autoload',
61
            $cacheDir,
62
            $logDir,
63
            $logDir.'/tracy',
64
        ];
65
        foreach ($dirs as $dir) {
66
            try {
67
                if (!is_dir($dir)) {
68
                    $this->mkdir($dir);
69
                }
70
                $this->chmod($dir);
71
            } catch (\Exception $exception) {
72
                $this->logError($exception->getMessage());
73
            }
74
        }
75
76
        if (!is_file($logFile = $logDir.'/yawik.log')) {
77
            touch($logFile);
78
        }
79
        $this->chmod($logFile, 0666);
80
    }
81
82
    private function chmod($dir, $mode = 0777)
83
    {
84
        if (is_dir($dir) || is_file($dir)) {
85
            $this->filesystem->chmod($dir, $mode);
86
            $this->log(sprintf(
87
                '<info>chmod: <comment>%s</comment> with %s</info>',
88
                $dir,
89
                decoct(fileperms($dir) & 0777)
90
            ));
91
        }
92
    }
93
94
    private function mkdir($dir)
95
    {
96
        $this->filesystem->mkdir($dir, 0777);
97
        $this->log(sprintf('<info>mkdir: </info><comment>%s</comment>', $dir));
98
    }
99
}
100