Test Failed
Push — master ( 6cb9f0...7deac3 )
by Antonio Carlos
07:23 queued 02:51
created

src/Checkers/DirectoryAndFilePresence.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace PragmaRX\Health\Checkers;
4
5
use PragmaRX\Health\Support\Result;
6
7
class DirectoryAndFilePresence extends Base
8
{
9
    /**
10
     * File exists constant.
11
     */
12
    const FILE_EXISTS = 0;
13
14
    /**
15
     * File does not exists constant.
16
     */
17
    const FILE_DOES_NOT_EXISTS = 1;
18
19
    /**
20
     * Directory exists constant.
21
     */
22
    const DIRECTORY_EXISTS = 2;
23
24
    /**
25
     * Directory does not exists constant.
26
     */
27
    const DIRECTORY_DOES_NOT_EXISTS = 3;
28
29
    /**
30
     * Checker.
31
     *
32
     * @return Result
33
     */
34
    public function check()
35
    {
36
        list($messages, $result) = $this->checkPresence();
37
38
        if ($result->count() == 0) {
39
            return $this->makeHealthyResult();
40
        }
41
42
        return $this->makeResult(
43
            false,
44
            $this->target->getErrorMessage().' - '.implode(' ', $messages)
45
        );
46
    }
47
48
    /**
49
     * Check file or dir presence.
50
     *
51
     * @return static
52
     */
53
    protected function checkPresence()
54
    {
55
        $messages = [];
56
57
        $result = collect($this->getFiles())
58
            ->map(function ($files, $type) use (&$messages) {
59
                $isGood = true;
60
61
                $files = collect($files);
62
63
                foreach ($files as $file) {
64
                    if (! is_null($file)) {
65
                        foreach ($this->getCheckers($type) as $checker) {
66
                            if (is_string($message = $checker($file))) {
67
                                $messages[] = $message;
68
                                $isGood = false;
69
                            }
70
                        }
71
                    }
72
                }
73
74
                return $isGood;
75
            })
76
            ->filter(function ($value) {
77
                return $value === false;
78
            });
79
80
        return [$messages, $result];
81
    }
82
83
    public function getFiles()
84
    {
85
        return [
86
            static::FILE_EXISTS => $this->target->fileExists,
87
            static::FILE_DOES_NOT_EXISTS => $this->target->fileDoNotExists,
88
            static::DIRECTORY_EXISTS => $this->target->directoryExists,
89
            static::DIRECTORY_DOES_NOT_EXISTS => $this->target->directoryDoNotExists,
0 ignored issues
show
The property directoryDoNotExists does not seem to exist in PragmaRX\Health\Support\Target.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
90
        ];
91
    }
92
93
    /**
94
     * Build file exists checker.
95
     *
96
     * @return \Closure
97
     */
98
    public function buildFileExistsChecker()
99
    {
100
        return function ($file) {
101
            return $this->fileExists($file);
102
        };
103
    }
104
105
    /**
106
     * Build file does not exists checker.
107
     *
108
     * @return \Closure
109
     */
110
    public function buildFileDoesNotExistsChecker()
111
    {
112
        return function ($file) {
113
            return $this->fileDoesNotExists($file);
114
        };
115
    }
116
117
    /**
118
     * Build is directory checker.
119
     *
120
     * @return \Closure
121
     */
122
    public function buildIsDirectoryChecker()
123
    {
124
        return function ($file) {
125
            return $this->isDirectory($file);
126
        };
127
    }
128
129
    /**
130
     * Get checkers.
131
     *
132
     * @return array
133
     */
134
    public function getCheckers($checker)
135
    {
136
        switch ($checker) {
137
            case static::FILE_EXISTS:
138
                return [$this->buildFileExistsChecker()];
139
            case static::FILE_DOES_NOT_EXISTS:
140
                return [$this->buildFileDoesNotExistsChecker()];
141
            case static::DIRECTORY_EXISTS:
142
                return [
143
                    $this->buildFileExistsChecker(),
144
                    $this->buildIsDirectoryChecker(),
145
                ];
146
            case static::DIRECTORY_DOES_NOT_EXISTS:
147
                return [
148
                    $this->buildFileDoesNotExistsChecker(),
149
                    $this->buildIsDirectoryChecker(),
150
                ];
151
        }
152
153
        return [];
154
    }
155
156
    /**
157
     * Check if a file exists.
158
     *
159
     * @param $file
160
     * @return bool|string
161
     */
162
    public function fileExists($file)
163
    {
164
        if (file_exists($file)) {
165
            return true;
166
        }
167
168
        return sprintf('File "%s" does not exists.', $file);
169
    }
170
171
    /**
172
     * Check if a file does not exists.
173
     *
174
     * @param $file
175
     * @return bool|string
176
     */
177
    public function fileDoesNotExists($file)
178
    {
179
        if (! file_exists($file)) {
180
            return true;
181
        }
182
183
        return sprintf('File "%s" exists.', $file);
184
    }
185
186
    /**
187
     * Check if a path is a directory.
188
     *
189
     * @param $file
190
     * @return bool|string
191
     */
192
    public function isDirectory($file)
193
    {
194
        if (is_dir($file)) {
195
            return true;
196
        }
197
198
        return sprintf('"%s" is not a directory.', $file);
199
    }
200
}
201