FileCheck::setWritable()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
namespace BretRZaun\StatusPage\Check;
3
4
use BretRZaun\StatusPage\Result;
5
6
class FileCheck extends AbstractCheck
7
{
8
9
    /**
10
     * @var string
11
     */
12
    protected $filename;
13
14
    /**
15
     * @var boolean
16
     */
17
    protected $writable;
18
19
    /**
20
     * @var integer
21
     */
22
    protected $maxage;
23
24
    /**
25
     * @var string
26
     */
27
    protected $unwantedRegex;
28
29
    /**
30
     * FileCheck constructor.
31
     * @param string $label
32
     * @param string $filename
33
     */
34
    public function __construct(string $label, string $filename)
35
    {
36
        parent::__construct($label);
37
        $this->filename = $filename;
38
    }
39
40
    /**
41
     * @param string $filename
42
     * @return FileCheck
43
     */
44
    public function setFilename(string $filename): self
45
    {
46
        $this->filename = $filename;
47
        return $this;
48
    }
49
50
    /**
51
     * @return FileCheck
52
     */
53
    public function setWritable(): self
54
    {
55
        $this->writable = true;
56
        return $this;
57
    }
58
59
    /**
60
     * @param int $maxage
61
     * @return FileCheck
62
     */
63
    public function setMaxage(int $maxage): self
64
    {
65
        $this->maxage = $maxage;
66
        return $this;
67
    }
68
69
    /**
70
     * @param string $unwantedRegex
71
     * @return FileCheck
72
     */
73
    public function setUnwantedRegex(string $unwantedRegex): self
74
    {
75
        $this->unwantedRegex = $unwantedRegex;
76
        return $this;
77
    }
78
79
    /**
80
     * @return Result
81
     */
82
    public function checkStatus(): Result
83
    {
84
        $result = new Result($this->label);
85
86
        if (!file_exists($this->filename)) {
87
            $result->setSuccess(false);
88
            $result->setError($this->filename." does not exist!");
89
            return $result;
90
        }
91
92
        if (null !== $this->maxage) {
93
            $mtime = filemtime($this->filename);
94
            if ($mtime === false) {
95
                $result->setError("mtime() returns error");
96
                return $result;
97
            }
98
            $age = (time() - $mtime);
99
            $age = round($age / 60); // sec-to-min
100
            if ($age > (int) $this->maxage) {
101
                $result->setError($this->filename." is to old!");
102
                return $result;
103
            }
104
        }
105
106
        if (null !== $this->writable && !is_writable($this->filename)) {
107
            $result->setError($this->filename." is not writable!");
108
            return $result;
109
        }
110
111
        if (null !== $this->unwantedRegex) {
112
            $fp = fopen($this->filename, 'r');
113
            if ($fp === false) {
114
                $result->setError("fopen() returns error");
115
                return $result;
116
            }
117
            $linenr = 0;
118
            while ($line = fgets($fp)) {
119
                $linenr++;
120
                if (preg_match('~'.$this->unwantedRegex.'~i', $line)) {
121
                    $result->setError("Found '".$this->unwantedRegex."' in '".$line."' [".$this->filename.":".$linenr."]");
122
                    fclose($fp);
123
                    return $result;
124
                }
125
            }
126
            fclose($fp);
127
        }
128
129
        return $result;
130
    }
131
}
132