Completed
Push — app ( d2c122...aa71e8 )
by Akihito
05:34
created

Unlink::__invoke()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
ccs 7
cts 7
cp 1
rs 8.8571
cc 5
eloc 6
nc 4
nop 1
crap 5
1
<?php
2
/**
3
 * This file is part of the BEAR.Package package.
4
 *
5
 * @license http://opensource.org/licenses/MIT MIT
6
 */
7
namespace BEAR\Package;
8
9
final class Unlink
10
{
11
    /**
12
     * @var array
13
     */
14
    private static $unlinkedPath = [];
15
16
    /**
17
     * @var bool
18
     */
19
    private $isOptional = true;
20
21 12
    public function __invoke(string $path)
22
    {
23 12
        if ($this->isOptional && file_exists($path . '/.do_not_clear')) {
24 7
            return;
25
        }
26 12
        foreach (\glob(\rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . '*') as $file) {
27 12
            \is_dir($file) ? $this->__invoke($file) : \unlink($file);
28 12
            @\rmdir($file);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
29
        }
30 12
    }
31
32 29
    public function once(string $path) : bool
33
    {
34 29
        if (in_array($path, self::$unlinkedPath, true)) {
35 24
            return true;
36
        }
37 7
        self::$unlinkedPath[] = $path;
38 7
        $this->__invoke($path);
39
40 7
        return false;
41
    }
42
43 9
    public function force(string $path) : bool
44
    {
45 9
        $this->isOptional = false;
46 9
        ($this)($path);
47
48 9
        return true;
49
    }
50
}
51