Completed
Push — develop ( fbe5dc...09dd13 )
by Vlad
01:48
created

Filesystem::testContain()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 3
1
<?php
2
3
namespace Simplario\Checker\Checker;
4
5
use Simplario\Checker\ResultException\FailException;
6
use Simplario\Checker\ResultException\SuccessException;
7
8
/**
9
 * Class Filesystem
10
 *
11
 * @package Simplario\Checker\Checker
12
 */
13
class Filesystem extends AbstractChecker
14
{
15
    /**
16
     * @var string
17
     */
18
    protected $target = 'path';
19
20
    /**
21
     * @param string  $path
22
     * @param boolean $expectExists
23
     * @param array   $task
24
     *
25
     * @throws FailException
26
     * @throws SuccessException
27
     */
28
    protected function testExists($path, $expectExists, array $task)
29
    {
30
        $exists = is_file($path) || is_dir($path);
31
32
        if ($exists === $expectExists) {
33
            throw new SuccessException('Ok', $task);
34
        }
35
36
        $msg = $expectExists ? "Path '{$path}' is not exists'" : "Path '{$path}' is exists";
37
38
        throw new FailException($msg, $task);
39
    }
40
41
    /**
42
     * @param string  $path
43
     * @param boolean $expectFile
44
     * @param array   $task
45
     *
46
     * @throws FailException
47
     * @throws SuccessException
48
     */
49
    protected function testFile($path, $expectFile, array $task)
50
    {
51
        if (is_file($path) === $expectFile) {
52
            throw new SuccessException('Ok', $task);
53
        }
54
55
        $msg = $expectFile ? "Path '{$path}' is not a file'" : "Path '{$path}' is a file";
56
57
        throw new FailException($msg, $task);
58
    }
59
60
    /**
61
     * @param string  $path
62
     * @param boolean $expectFolder
63
     * @param array   $task
64
     *
65
     * @throws FailException
66
     * @throws SuccessException
67
     */
68
    protected function testFolder($path, $expectFolder, array $task)
69
    {
70
        if (is_dir($path) === $expectFolder) {
71
            throw new SuccessException('Ok', $task);
72
        }
73
74
        $msg = $expectFolder ? "Path '{$path}' is not a folder'" : "Path '{$path}' is a folder";
75
76
        throw new FailException($msg, $task);
77
    }
78
79
    /**
80
     * @param string  $path
81
     * @param boolean $expectWritable
82
     * @param array   $task
83
     *
84
     * @throws FailException
85
     * @throws SuccessException
86
     */
87
    protected function testWritable($path, $expectWritable, array $task)
88
    {
89
        if (is_writeable($path) === $expectWritable) {
90
            throw new SuccessException('Ok', $task);
91
        }
92
93
        $msg = $expectWritable ? "Path '{$path}' is not writable'" : "Path '{$path}' is writable";
94
95
        throw new FailException($msg, $task);
96
    }
97
98
    /**
99
     * @param string  $path
100
     * @param boolean $expectReadable
101
     * @param array   $task
102
     *
103
     * @throws FailException
104
     * @throws SuccessException
105
     */
106
    protected function testReadable($path, $expectReadable, array $task)
107
    {
108
        if (is_readable($path) === $expectReadable) {
109
            throw new SuccessException('Ok', $task);
110
        }
111
112
        $msg = $expectReadable ? "Path '{$path}' is not readable'" : "Path '{$path}' is readable";
113
114
        throw new FailException($msg, $task);
115
    }
116
117
    /**
118
     * @param string  $path
119
     * @param boolean $expectChmod
120
     * @param array   $task
121
     *
122
     * @throws FailException
123
     * @throws SuccessException
124
     */
125
    protected function testChmod($path, $expectChmod, array $task)
126
    {
127
        $chmod = substr(sprintf('%o', fileperms($path)), -4);
128
129
        if ($chmod === $expectChmod) {
130
            throw new SuccessException('Ok', $task);
131
        }
132
133
        $msg = "Path '{$path}' chmod expected: '{$expectChmod}', but have: '{$chmod}'";
134
135
        throw new FailException($msg, $task);
136
    }
137
138
    /**
139
     * @param string $path
140
     * @param string $find
141
     * @param array  $task
142
     *
143
     * @throws FailException
144
     * @throws SuccessException
145
     */
146
    protected function testContain($path, $find, array $task)
147
    {
148
        $source = file_get_contents($path);
149
150
        if (strpos($source, $find) !== false) {
151
            throw new SuccessException('Ok', $task);
152
        }
153
154
        $msg = "Path '{$path}' must contain '{$find}'";
155
156
        throw new FailException($msg, $task);
157
    }
158
}
159