Test Failed
Pull Request — master (#31)
by
unknown
04:02
created

UploadEntityStyle   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 34
c 1
b 0
f 0
dl 0
loc 113
rs 10
wmc 11

9 Methods

Rating   Name   Duplication   Size   Complexity  
A video() 0 5 1
A image() 0 5 1
A audio() 0 5 1
A getImageStyles() 0 3 1
A styleHasFile() 0 25 3
A stream() 0 5 1
A coverStyle() 0 5 1
A getAudioStyles() 0 3 1
A getVideoStyles() 0 3 1
1
<?php
2
3
namespace Mostafaznv\Larupload\Concerns\Storage\UploadEntity;
4
5
use FFMpeg\Format\Audio\Aac;
6
use FFMpeg\Format\Audio\Flac;
7
use FFMpeg\Format\Audio\Mp3;
8
use FFMpeg\Format\Audio\Wav;
9
use FFMpeg\Format\Video\X264;
10
use Mostafaznv\Larupload\Larupload;
11
use Mostafaznv\Larupload\UploadEntities;
12
use Mostafaznv\Larupload\DTOs\Style\AudioStyle;
13
use Mostafaznv\Larupload\DTOs\Style\ImageStyle;
14
use Mostafaznv\Larupload\DTOs\Style\VideoStyle;
15
use Mostafaznv\Larupload\DTOs\Style\StreamStyle;
16
use Mostafaznv\Larupload\Enums\LaruploadFileType;
17
use Mostafaznv\Larupload\Enums\LaruploadMediaStyle;
18
19
trait UploadEntityStyle
20
{
21
    /**
22
     * Styles for image files
23
     *
24
     * @var ImageStyle[]
25
     */
26
    protected array $imageStyles = [];
27
28
    /**
29
     * Styles for video files
30
     *
31
     * @var VideoStyle[]
32
     */
33
    protected array $videoStyles = [];
34
35
    /**
36
     * Styles for audio files
37
     *
38
     * @var AudioStyle[]
39
     */
40
    protected array $audioStyles = [];
41
42
    /**
43
     * Stream styles
44
     *
45
     * @var StreamStyle[]
46
     */
47
    protected array $streams = [];
48
49
    /**
50
     * Cover style
51
     *
52
     * @var ImageStyle
53
     */
54
    protected ImageStyle $coverStyle;
55
56
57
    public function getImageStyles(): array
58
    {
59
        return $this->imageStyles;
60
    }
61
62
    public function getVideoStyles(): array
63
    {
64
        return $this->videoStyles;
65
    }
66
67
    public function getAudioStyles(): array
68
    {
69
        return $this->audioStyles;
70
    }
71
72
    public function image(string $name, ?int $width = null, ?int $height = null, LaruploadMediaStyle $mode = LaruploadMediaStyle::AUTO): UploadEntities
73
    {
74
        $this->imageStyles[$name] = ImageStyle::make($name, $width, $height, $mode);
75
76
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Mostafaznv\Larupload\Con...ntity\UploadEntityStyle which includes types incompatible with the type-hinted return Mostafaznv\Larupload\UploadEntities.
Loading history...
77
    }
78
79
    public function video(string $name, ?int $width = null, ?int $height = null, LaruploadMediaStyle $mode = LaruploadMediaStyle::SCALE_HEIGHT, X264 $format = new X264, bool $padding = false): UploadEntities
80
    {
81
        $this->videoStyles[$name] = VideoStyle::make($name, $width, $height, $mode, $format, $padding);
82
83
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Mostafaznv\Larupload\Con...ntity\UploadEntityStyle which includes types incompatible with the type-hinted return Mostafaznv\Larupload\UploadEntities.
Loading history...
84
    }
85
86
    public function audio(string $name, Mp3|Aac|Wav|Flac $format = new Mp3, int $bitrate = 128): UploadEntities
87
    {
88
        $this->audioStyles[$name] = AudioStyle::make($name, $format, $bitrate);
89
90
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Mostafaznv\Larupload\Con...ntity\UploadEntityStyle which includes types incompatible with the type-hinted return Mostafaznv\Larupload\UploadEntities.
Loading history...
91
    }
92
93
    public function stream(string $name, int $width, int $height, X264 $format, LaruploadMediaStyle $mode = LaruploadMediaStyle::SCALE_HEIGHT, bool $padding = false): UploadEntities
94
    {
95
        $this->streams[$name] = StreamStyle::make($name, $width, $height, $format, $mode, $padding);
96
97
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Mostafaznv\Larupload\Con...ntity\UploadEntityStyle which includes types incompatible with the type-hinted return Mostafaznv\Larupload\UploadEntities.
Loading history...
98
    }
99
100
    public function coverStyle(string $name, ?int $width = null, ?int $height = null, LaruploadMediaStyle $mode = LaruploadMediaStyle::AUTO): UploadEntities
101
    {
102
        $this->coverStyle = ImageStyle::make($name, $width, $height, $mode);
103
104
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Mostafaznv\Larupload\Con...ntity\UploadEntityStyle which includes types incompatible with the type-hinted return Mostafaznv\Larupload\UploadEntities.
Loading history...
105
    }
106
107
    protected function styleHasFile(string $style): bool
108
    {
109
        if (in_array($style, [Larupload::ORIGINAL_FOLDER, Larupload::COVER_FOLDER])) {
110
            return true;
111
        }
112
113
        $type = $this->output['type'];
114
        $types = [
115
            LaruploadFileType::VIDEO->name,
0 ignored issues
show
Bug introduced by
The property name does not seem to exist on Mostafaznv\Larupload\Enums\LaruploadFileType.
Loading history...
116
            LaruploadFileType::IMAGE->name,
117
            LaruploadFileType::AUDIO->name
118
        ];
119
120
        if (in_array($type, $types)) {
121
            $styles = match ($type) {
122
                LaruploadFileType::IMAGE->name => $this->imageStyles,
123
                LaruploadFileType::VIDEO->name => $this->videoStyles,
124
                LaruploadFileType::AUDIO->name => $this->audioStyles,
125
                default => throw new \InvalidArgumentException("Unsupported file type: $type")
126
            };
127
128
            return array_key_exists($style, $styles);
129
        }
130
131
        return false;
132
    }
133
}
134