Test Failed
Push — master ( f043ce...f1785b )
by Antonio Carlos
04:03
created

src/Checkers/DirectoryAndFilePresence.php (4 issues)

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 1
    public function check()
35
    {
36 1
        list($messages, $result) = $this->checkPresence();
37
38 1
        if ($result->count() == 0) {
39 1
            return $this->makeHealthyResult();
40
        }
41
42 1
        return $this->makeResult(
43 1
            false,
44 1
            $this->target->getErrorMessage().' - '.implode(' ', $messages)
45
        );
46
    }
47
48
    /**
49
     * Check file or dir presence.
50
     *
51
     * @return static
52
     */
53 1
    protected function checkPresence()
54
    {
55 1
        $messages = [];
56
57 1
        $result = collect($this->getFiles())
58
            ->map(function ($files, $type) use (&$messages) {
59 1
                $isGood = true;
60
61 1
                $files = collect($files);
62
63 1
                foreach ($files as $file) {
64 1
                    if (! is_null($file)) {
65 1
                        foreach ($this->getCheckers($type) as $checker) {
66 1
                            if (is_string($message = $checker($file))) {
67 1
                                $messages[] = $message;
68 1
                                $isGood = false;
69
                            }
70
                        }
71
                    }
72
                }
73
74 1
                return $isGood;
75 1
            })
76
            ->filter(function ($value) {
77 1
                return $value === false;
78 1
            });
79
80 1
        return [$messages, $result];
81
    }
82
83 1
    public function getFiles()
84
    {
85
        return [
86 1
            static::FILE_EXISTS => $this->target->fileExists,
0 ignored issues
show
The property fileExists 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...
87 1
            static::FILE_DOES_NOT_EXISTS => $this->target->fileDoNotExists,
0 ignored issues
show
The property fileDoNotExists 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...
88 1
            static::DIRECTORY_EXISTS => $this->target->directoryExists,
0 ignored issues
show
The property directoryExists 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...
89 1
            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 1
    public function buildFileExistsChecker()
99
    {
100
        return function ($file) {
101 1
            return $this->fileExists($file);
102 1
        };
103
    }
104
105
    /**
106
     * Build file does not exists checker.
107
     *
108
     * @return \Closure
109
     */
110 1
    public function buildFileDoesNotExistsChecker()
111
    {
112
        return function ($file) {
113 1
            return $this->fileDoesNotExists($file);
114 1
        };
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 1
    public function getCheckers($checker)
135
    {
136
        switch ($checker) {
137 1
            case static::FILE_EXISTS:
138 1
                return [$this->buildFileExistsChecker()];
139 1
            case static::FILE_DOES_NOT_EXISTS:
140 1
                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 1
    public function fileExists($file)
163
    {
164 1
        if (file_exists($file)) {
165
            return true;
166
        }
167
168 1
        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 1
    public function fileDoesNotExists($file)
178
    {
179 1
        if (! file_exists($file)) {
180 1
            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