MinItemSizeInKbRule   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A validateMediaItem() 0 9 2
A __construct() 0 3 1
A message() 0 7 1
1
<?php
2
3
namespace Spatie\MediaLibraryPro\Rules\ItemRules;
4
5
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...
6
7
class MinItemSizeInKbRule extends MediaItemRule
8
{
9
    protected int $minSizeInKb;
10
11
    protected int $actualSizeInBytes;
12
13
    public function __construct(int $minSizeInKb)
14
    {
15
        $this->minSizeInKb = $minSizeInKb;
16
    }
17
18
    public function validateMediaItem(): bool
19
    {
20
        if (! $media = $this->getTemporaryUploadMedia()) {
21
            return true;
22
        }
23
24
        $this->actualSizeInBytes = $media->size;
25
26
        return $this->actualSizeInBytes >= ($this->minSizeInKb * 1024);
27
    }
28
29
    public function message(): string
30
    {
31
        return __('media-library::validation.file_too_small', [
0 ignored issues
show
Bug Best Practice introduced by
The expression return __('media-library...alSizeInBytes / 1024))) 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...
32
            'min' => File::getHumanReadableSize($this->minSizeInKb * 1024),
33
            'minInKb' => $this->minSizeInKb,
34
            'actual' => File::getHumanReadableSize($this->actualSizeInBytes),
35
            'actualInKb' => round($this->actualSizeInBytes / 1024),
36
        ]);
37
    }
38
}
39