BaseUploadEntity   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 49
c 2
b 0
f 1
dl 0
loc 102
rs 10
wmc 13

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getMode() 0 3 1
A getBasePath() 0 12 3
A make() 0 3 1
A outputToObject() 0 15 2
A setOutput() 0 15 5
A __construct() 0 26 1
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
        // todo - remove it and store original file name by default in the next major version
36
        $this->storeOriginalFileName = $config['store-original-file-name'] ?? false;
0 ignored issues
show
Bug Best Practice introduced by
The property storeOriginalFileName does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
37
        $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...
38
        $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...
39
        $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...
40
    }
41
42
    public static function make(string $name, LaruploadMode $mode = LaruploadMode::HEAVY): UploadEntities
43
    {
44
        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...
45
    }
46
47
48
    public function getMode(): LaruploadMode
49
    {
50
        return $this->mode;
51
    }
52
53
    public function setOutput(Model $model): void
54
    {
55
        $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...
56
57
        if ($this->mode === LaruploadMode::HEAVY) {
58
            foreach ($this->output as $key => $value) {
59
                $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...
60
            }
61
        }
62
        else {
63
            $meta = json_decode($model->{"{$this->name}_file_meta"}, true);
64
65
            if (is_array($meta)) {
66
                foreach ($meta as $key => $value) {
67
                    $this->output[$key] = $value;
68
                }
69
            }
70
        }
71
    }
72
73
    /**
74
     * Prepare output array to response
75
     *
76
     * @return object
77
     */
78
    protected function outputToObject(): object
79
    {
80
        $output = (object)$this->output;
81
82
        if ($this->camelCaseResponse) {
83
            $output->mimeType = $output->mime_type;
84
            $output->dominantColor = $output->dominant_color;
85
            $output->originalName = $output->original_name;
86
87
            unset($output->mime_type);
88
            unset($output->dominant_color);
89
            unset($output->original_name);
90
        }
91
92
        return $output;
93
    }
94
95
    /**
96
     * Path Helper to generate relative path string
97
     *
98
     * @param string $id
99
     * @param string|null $folder
100
     * @return string
101
     */
102
    protected function getBasePath(string $id, ?string $folder = null): string
103
    {
104
        $path = $this->mode == LaruploadMode::STANDALONE ? "$this->folder/$this->nameKebab" : "$this->folder/$id/$this->nameKebab";
105
        $path = trim($path, '/');
106
107
        if ($folder) {
108
            $folder = strtolower(str_replace('_', '-', trim($folder)));
109
110
            return "$path/$folder";
111
        }
112
113
        return $path;
114
    }
115
}
116