Test Failed
Push — master ( bfe357...41d8e8 )
by Antonio Carlos
25:24
created

DirectoryAndFilePresence   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 195
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 195
rs 10
c 0
b 0
f 0
wmc 22
lcom 1
cbo 3

10 Methods

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