TemporaryFilename   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 95.45%

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 48
ccs 21
cts 22
cp 0.9545
rs 10
c 0
b 0
f 0
wmc 11

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __destruct() 0 4 2
A unlink() 0 4 3
A deleteOnDestruct() 0 3 1
A __toString() 0 3 1
A filename() 0 3 1
A setDeleteOnDestruct() 0 3 1
A __construct() 0 6 2
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