MaxTotalSizeInKbRule   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 28
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A message() 0 7 1
A __construct() 0 2 1
A passes() 0 11 1
1
<?php
2
3
namespace Spatie\MediaLibraryPro\Rules\GroupRules;
4
5
use Illuminate\Contracts\Validation\Rule;
6
use Spatie\MediaLibrary\MediaCollections\Models\Media;
0 ignored issues
show
Bug introduced by
The type Spatie\MediaLibrary\MediaCollections\Models\Media was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Spatie\MediaLibrary\Support\File;
0 ignored issues
show
Bug introduced by
The type Spatie\MediaLibrary\Support\File was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
9
class MaxTotalSizeInKbRule implements Rule
10
{
11
    protected int $actualTotalSizeInKb;
12
13
    public function __construct(protected int $maxTotalSizeInKb)
14
    {
15
    }
16
17
    public function passes($attribute, $uploadedItems): bool
18
    {
19
        $uuids = collect($uploadedItems)
20
            ->map(fn (array $uploadedItemAttributes) => $uploadedItemAttributes['uuid'])
21
            ->toArray();
22
23
        $media = Media::findWithTemporaryUploadInCurrentSession($uuids);
24
25
        $this->actualTotalSizeInKb = $media->totalSizeInBytes();
26
27
        return $this->actualTotalSizeInKb <= ($this->maxTotalSizeInKb * 1024);
28
    }
29
30
    public function message(): string
31
    {
32
        return __('media-library::validation.total_upload_size_too_high', [
0 ignored issues
show
Bug Best Practice introduced by
The expression return __('media-library...->actualTotalSizeInKb)) could return the type array which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
33
            'max' => File::getHumanReadableSize($this->maxTotalSizeInKb * 1024),
34
            'maxInKb' => $this->maxTotalSizeInKb,
35
            'actual' => File::getHumanReadableSize($this->actualTotalSizeInKb * 1024),
36
            'actualInKb' => $this->actualTotalSizeInKb,
37
        ]);
38
    }
39
}
40