Export::logger()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
namespace Mostafaznv\PdfOptimizer\Laravel\Concerns;
4
5
use Mostafaznv\PdfOptimizer\Actions\OptimizePdfAction;
6
use Mostafaznv\PdfOptimizer\Concerns\ExportsScript;
7
use Mostafaznv\PdfOptimizer\Concerns\PdfOptimizerProperties;
8
use Mostafaznv\PdfOptimizer\DTOs\OptimizeResult;
9
use Mostafaznv\PdfOptimizer\DTOs\PdfOptimizerJobData;
10
use Mostafaznv\PdfOptimizer\DTOs\QueueData;
11
use Mostafaznv\PdfOptimizer\Jobs\PdfOptimizerJob;
12
use Psr\Log\LoggerInterface;
13
14
15
class Export
16
{
17
    use PdfOptimizerProperties, ExportsScript;
0 ignored issues
show
introduced by
The trait Mostafaznv\PdfOptimizer\Concerns\ExportsScript requires some properties which are not provided by Mostafaznv\PdfOptimizer\Laravel\Concerns\Export: $name, $value
Loading history...
18
19
    private readonly File    $file;
20
    private string           $gsBinary;
21
    private ?string          $disk   = null;
22
    private ?LoggerInterface $logger = null;
23
    private QueueData        $queue;
24
25
26
    public function __construct(File $file, string $gsBinary = 'gs')
27
    {
28
        $this->file = $file;
0 ignored issues
show
Bug introduced by
The property file is declared read-only in Mostafaznv\PdfOptimizer\Laravel\Concerns\Export.
Loading history...
29
        $this->gsBinary = $gsBinary;
30
        $this->timeout = config('pdf-optimizer.timeout');
31
32
        $queue = config('pdf-optimizer.queue');
33
34
        $this->queue = QueueData::make(
35
            $queue['enabled'], $queue['name'], $queue['connection'], $queue['timeout']
36
        );
37
    }
38
39
40
    public function toDisk(string $disk): self
41
    {
42
        $this->disk = $disk;
43
44
        return $this;
45
    }
46
47
    public function setGsBinary(string $binary): self
48
    {
49
        $this->gsBinary = $binary;
50
51
        return $this;
52
    }
53
54
    public function onQueue(bool $enabled = true, string $name = 'default', ?string $connection = null, int $timeout = 900): self
55
    {
56
        $this->queue = QueueData::make($enabled, $name, $connection, $timeout);
57
58
        return $this;
59
    }
60
61
    public function logger(LoggerInterface $logger): self
62
    {
63
        $this->logger = $logger;
64
65
        return $this;
66
    }
67
68
    public function optimize(string $pathToOptimizedFile): OptimizeResult
69
    {
70
        $disk = Disk::make($this->disk);
71
72
        $commands = $this->command();
73
        $input = $this->file->getPath();
74
75
        if ($this->queue->enabled) {
76
            return $this->optimizeOnQueue(
77
                $commands, $input, $pathToOptimizedFile, $disk
78
            );
79
        }
80
81
        return OptimizePdfAction::init($this->file, $disk)
82
            ->logger($this->logger)
83
            ->setTimeout($this->timeout)
84
            ->execute(
85
                $commands, $input, $pathToOptimizedFile
86
            );
87
    }
88
89
90
    private function optimizeOnQueue(array $commands, string $input, string $output, Disk $disk): OptimizeResult
91
    {
92
        $data = PdfOptimizerJobData::make(
93
            commands: $commands,
94
            input: $input,
95
            output: $output,
96
            file: $this->file,
97
            disk: $disk,
98
            logger: $this->logger,
99
            timeout: $this->queue->timeout
100
        );
101
102
        PdfOptimizerJob::dispatch($data)
103
            ->onQueue($this->queue->name)
104
            ->onConnection($this->queue->connection);
105
106
        return OptimizeResult::make(true, true, $data->id);
107
108
    }
109
}
110