FileWriteableCheck::check()   B
last analyzed

Complexity

Conditions 10
Paths 20

Size

Total Lines 54
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 37
nc 20
nop 0
dl 0
loc 54
rs 7.6666
c 0
b 0
f 0

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
namespace SilverStripe\EnvironmentCheck\Checks;
4
5
use SilverStripe\EnvironmentCheck\EnvironmentCheck;
6
7
/**
8
 * Check that the given file is writable.
9
 *
10
 * @package environmentcheck
11
 */
12
class FileWriteableCheck implements EnvironmentCheck
13
{
14
    /**
15
     * @var string
16
     */
17
    protected $path;
18
19
    /**
20
     * @param string $path The full path. If a relative path, it will relative to the BASE_PATH.
21
     */
22
    public function __construct($path)
23
    {
24
        $this->path = $path;
25
    }
26
27
    /**
28
     * {@inheritDoc}
29
     *
30
     * @return array
31
     */
32
    public function check()
33
    {
34
        if ($this->path[0] == '/') {
35
            $filename = $this->path;
36
        } else {
37
            $filename = BASE_PATH . DIRECTORY_SEPARATOR . str_replace('/', DIRECTORY_SEPARATOR, $this->path);
38
        }
39
40
        if (file_exists($filename)) {
41
            $isWriteable = is_writeable($filename);
42
        } else {
43
            $isWriteable = is_writeable(dirname($filename));
44
        }
45
46
        if (!$isWriteable) {
47
            if (function_exists('posix_getgroups')) {
48
                $userID = posix_geteuid();
49
                $user = posix_getpwuid($userID);
50
51
                $currentOwnerID = fileowner(file_exists($filename) ? $filename : dirname($filename));
52
                $currentOwner = posix_getpwuid($currentOwnerID);
53
54
                $message = "User '$user[name]' needs to be able to write to this file:\n$filename\n\nThe file is "
55
                    . "currently owned by '$currentOwner[name]'.  ";
56
57
                if ($user['name'] == $currentOwner['name']) {
58
                    $message .= 'We recommend that you make the file writeable.';
59
                } else {
60
                    $groups = posix_getgroups();
61
                    $groupList = [];
62
                    foreach ($groups as $group) {
63
                        $groupInfo = posix_getgrgid($group);
64
                        if (in_array($currentOwner['name'], $groupInfo['members'])) {
65
                            $groupList[] = $groupInfo['name'];
66
                        }
67
                    }
68
                    if ($groupList) {
69
                        $message .= "	We recommend that you make the file group-writeable and change the group to "
70
                            . "one of these groups:\n - " . implode("\n - ", $groupList)
71
                            . "\n\nFor example:\nchmod g+w $filename\nchgrp " . $groupList[0] . " $filename";
72
                    } else {
73
                        $message .= "  There is no user-group that contains both the web-server user and the owner "
74
                            . "of this file.  Change the ownership of the file, create a new group, or temporarily "
75
                            . "make the file writeable by everyone during the install process.";
76
                    }
77
                }
78
            } else {
79
                $message = "The webserver user needs to be able to write to this file:\n$filename";
80
            }
81
82
            return [EnvironmentCheck::ERROR, $message];
83
        }
84
85
        return [EnvironmentCheck::OK, ''];
86
    }
87
}
88