File::getPath()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Mostafaznv\PdfOptimizer\Laravel\Concerns;
4
5
6
class File
7
{
8
    private string  $path;
9
    private Disk    $diskInstance;
10
11
12
    public function __construct(string $path, ?string $disk = null)
13
    {
14
        $this->path = $path;
15
        $this->diskInstance = Disk::make($disk);
16
    }
17
18
    public function __destruct()
19
    {
20
        $this->cleanup();
21
    }
22
23
    public static function make(string $path, ?string $disk = null): self
24
    {
25
        return new static($path, $disk);
26
    }
27
28
29
    public function getDisk(): Disk
30
    {
31
        return $this->diskInstance;
32
    }
33
34
    public function getPath(): string
35
    {
36
        return $this->path;
37
    }
38
39
    public function getLocalPath(): string
40
    {
41
        $path = $this->getPath();
42
        $disk = $this->getDisk()->getAdapter();
43
44
        if ($disk) {
0 ignored issues
show
introduced by
$disk is of type Illuminate\Filesystem\FilesystemAdapter, thus it always evaluated to true.
Loading history...
45
            if ($this->getDisk()->isLocalDisk()) {
46
                return $disk->path($path);
47
            }
48
49
            $temporaryDisk = $this->getDisk()->getTemporaryDisk();
50
51
            if (!$temporaryDisk->exists($path)) {
52
                $temporaryDisk->writeStream($path, $disk->readStream($path));
53
            }
54
55
            return $temporaryDisk->path($path);
56
        }
57
58
        return $path;
59
    }
60
61
    public function cleanup(): self
62
    {
63
        $this->getDisk()->cleanupTemporaryDirectory();
64
65
        return $this;
66
    }
67
}
68