TemporaryFilename::__destruct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpCfdi\RfcLinc\Util;
6
7
class TemporaryFilename
8
{
9
    /** @var string */
10
    private $filename;
11
12
    /** @var bool */
13
    private $deleteOnDestruct;
14
15 7
    public function __construct(string $dir = '', string $prefix = '', bool $deleteOnDestruct = true)
16
    {
17 7
        $this->deleteOnDestruct = $deleteOnDestruct;
18 7
        $this->filename = (string) tempnam($dir, $prefix);
19 7
        if ('' === $this->filename) {
20
            throw new \RuntimeException('Cannot create the temporary filename');
21
        }
22 7
    }
23
24 7
    public function __destruct()
25
    {
26 7
        if ($this->deleteOnDestruct) {
27 6
            $this->unlink();
28
        }
29 7
    }
30
31 2
    public function __toString()
32
    {
33 2
        return $this->filename();
34
    }
35
36 7
    public function filename(): string
37
    {
38 7
        return $this->filename;
39
    }
40
41 1
    public function deleteOnDestruct(): bool
42
    {
43 1
        return $this->deleteOnDestruct;
44
    }
45
46 1
    public function setDeleteOnDestruct(bool $deleteOnDestruct)
47
    {
48 1
        $this->deleteOnDestruct = $deleteOnDestruct;
49 1
    }
50
51 6
    public function unlink()
52
    {
53 6
        if (file_exists($this->filename) && ! is_dir($this->filename)) {
54 6
            unlink($this->filename);
55
        }
56 6
    }
57
}
58