Test Failed
Branch master (df1c42)
by Mostafa
15:23
created

BaseUploadEntity::getBasePath()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
c 0
b 0
f 0
nc 4
nop 2
dl 0
loc 12
rs 10
1
<?php
2
3
namespace Mostafaznv\Larupload\Concerns\Storage\UploadEntity;
4
5
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Support\Str;
8
use Mostafaznv\Larupload\Actions\GenerateFileIdAction;
9
use Mostafaznv\Larupload\Enums\LaruploadMode;
10
use Mostafaznv\Larupload\UploadEntities;
11
12
trait BaseUploadEntity
13
{
14
    public function __construct(string $name, LaruploadMode $mode)
15
    {
16
        $config = config('larupload');
17
18
        $this->name = $name;
0 ignored issues
show
Bug Best Practice introduced by
The property name does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
19
        $this->nameKebab = str_replace('_', '-', Str::kebab($name));
0 ignored issues
show
Bug Best Practice introduced by
The property nameKebab does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
20
        $this->mode = $mode;
0 ignored issues
show
Bug Best Practice introduced by
The property mode does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
21
        $this->disk = $config['disk'];
0 ignored issues
show
Bug Best Practice introduced by
The property disk does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
22
        $this->localDisk = $config['local-disk'];
0 ignored issues
show
Bug Best Practice introduced by
The property localDisk does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
23
        $this->secureIdsMethod = $config['secure-ids'];
0 ignored issues
show
Bug Best Practice introduced by
The property secureIdsMethod does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
24
        $this->withMeta = $config['with-meta'];
0 ignored issues
show
Bug Best Practice introduced by
The property withMeta does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
25
        $this->camelCaseResponse = $config['camel-case-response'];
0 ignored issues
show
Bug Best Practice introduced by
The property camelCaseResponse does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
26
        $this->namingMethod = $config['naming-method'];
0 ignored issues
show
Bug Best Practice introduced by
The property namingMethod does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
27
        $this->lang = $config['lang'];
0 ignored issues
show
Bug Best Practice introduced by
The property lang does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
28
        $this->imageProcessingLibrary = $config['image-processing-library'];
0 ignored issues
show
Bug Best Practice introduced by
The property imageProcessingLibrary does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
29
        $this->generateCover = $config['generate-cover'];
0 ignored issues
show
Bug Best Practice introduced by
The property generateCover does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
30
        $this->coverStyle = $config['cover-style'];
0 ignored issues
show
Bug Best Practice introduced by
The property coverStyle does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
31
        $this->dominantColor = $config['dominant-color'];
0 ignored issues
show
Bug Best Practice introduced by
The property dominantColor does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
32
        $this->dominantColorQuality = $config['dominant-color-quality'];
0 ignored issues
show
Bug Best Practice introduced by
The property dominantColorQuality does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
33
        $this->keepOldFiles = $config['keep-old-files'];
0 ignored issues
show
Bug Best Practice introduced by
The property keepOldFiles does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
34
        $this->preserveFiles = $config['preserve-files'];
0 ignored issues
show
Bug Best Practice introduced by
The property preserveFiles does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
35
        $this->optimizeImage = $config['optimize-image']['enable'] ?? false;
0 ignored issues
show
Bug Best Practice introduced by
The property optimizeImage does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
36
        $this->ffmpegQueue = $config['ffmpeg']['queue'];
0 ignored issues
show
Bug Best Practice introduced by
The property ffmpegQueue does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
37
        $this->ffmpegMaxQueueNum = $config['ffmpeg']['max-queue-num'];
0 ignored issues
show
Bug Best Practice introduced by
The property ffmpegMaxQueueNum does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
38
    }
39
40
    public static function make(string $name, LaruploadMode $mode = LaruploadMode::HEAVY): UploadEntities
41
    {
42
        return new static($name, $mode);
0 ignored issues
show
Bug Best Practice introduced by
The expression return new static($name, $mode) returns the type Mostafaznv\Larupload\Con...Entity\BaseUploadEntity which includes types incompatible with the type-hinted return Mostafaznv\Larupload\UploadEntities.
Loading history...
43
    }
44
45
46
    public function getMode(): LaruploadMode
47
    {
48
        return $this->mode;
49
    }
50
51
    public function setOutput(Model $model): void
52
    {
53
        $this->id = GenerateFileIdAction::make($model, $this->secureIdsMethod, $this->mode, $this->name)->run();
0 ignored issues
show
Bug Best Practice introduced by
The property id does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
54
55
        if ($this->mode === LaruploadMode::HEAVY) {
56
            foreach ($this->output as $key => $value) {
57
                $this->output[$key] = $model->{"{$this->name}_file_$key"};
0 ignored issues
show
Bug Best Practice introduced by
The property output does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
58
            }
59
        }
60
        else {
61
            $meta = json_decode($model->{"{$this->name}_file_meta"}, true);
62
63
            if (is_array($meta)) {
64
                foreach ($meta as $key => $value) {
65
                    $this->output[$key] = $value;
66
                }
67
            }
68
        }
69
    }
70
71
    /**
72
     * Prepare output array to response
73
     *
74
     * @return object
75
     */
76
    protected function outputToObject(): object
77
    {
78
        $output = (object)$this->output;
79
80
        if ($this->camelCaseResponse) {
81
            $output->mimeType = $output->mime_type;
82
            $output->dominantColor = $output->dominant_color;
83
84
            unset($output->mime_type);
85
            unset($output->dominant_color);
86
        }
87
88
        return $output;
89
    }
90
91
    /**
92
     * Path Helper to generate relative path string
93
     *
94
     * @param string $id
95
     * @param string|null $folder
96
     * @return string
97
     */
98
    protected function getBasePath(string $id, string $folder = null): string
99
    {
100
        $path = $this->mode == LaruploadMode::STANDALONE ? "$this->folder/$this->nameKebab" : "$this->folder/$id/$this->nameKebab";
101
        $path = trim($path, '/');
102
103
        if ($folder) {
104
            $folder = strtolower(str_replace('_', '-', trim($folder)));
105
106
            return "$path/$folder";
107
        }
108
109
        return $path;
110
    }
111
}
112