File   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 21
c 2
b 0
f 0
dl 0
loc 60
rs 10
wmc 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getDisk() 0 3 1
A getPath() 0 3 1
A __destruct() 0 3 1
A cleanup() 0 5 1
A getLocalPath() 0 20 4
A make() 0 3 1
A __construct() 0 4 1
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