Completed
Push — master ( a9e755...6c6dd7 )
by Bret R.
07:17
created

FileCheck::isWritable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
namespace BretRZaun\StatusPage\Check;
3
4
use BretRZaun\StatusPage\Result;
5
6
/**
7
 * checks if the given file exists
8
 *
9
 * Options:
10
 * - *writable*: checks if the file is writable
11
 *
12
 * @package BretRZaun\StatusPage\Check
13
 */
14
class FileCheck extends AbstractCheck
15
{
16
17
    /**
18
     * @var string
19
     */
20
    protected $filename;
21
22
    /**
23
     * @var array
24
     */
25
    protected $options;
26
27
    /**
28
     * FileCheck constructor.
29
     * @param $label
30
     * @param $filename
31
     * @param array $options
32
     */
33
    public function __construct($label, $filename, array $options = [])
34
    {
35
        parent::__construct($label);
36
        $this->filename = $filename;
37
        $this->options = $options;
38
    }
39
40
    public function isWritable()
41
    {
42
        $this->options['writable'] = true;
43
        return $this;
44
    }
45
46
    public function check(): Result
47
    {
48
        $result = new Result($this->label);
49
50 View Code Duplication
        if (!file_exists($this->filename)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
51
            $result->setSuccess(false);
52
            $result->setError("$this->filename does not exist!");
53
            return $result;
54
        }
55
56
        if (array_key_exists('writable', $this->options)) {
57
            if (!is_writable($this->filename)) {
58
                $result->setSuccess(false);
59
                $result->setError("$this->filename ist not writable!");
60
            }
61
        }
62
63
        return $result;
64
    }
65
}
66