Test Failed
Push — fix/media-validation ( 3351fe )
by Ben
09:34
created

ValidatesExistingAssetAttributes   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 15
eloc 26
c 1
b 0
f 0
dl 0
loc 73
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A existingAsset() 0 3 1
A validateAssetDimensions() 0 12 2
A lowlevelDimensionsCheck() 0 10 3
A validateAssetMimetypes() 0 4 2
A isValidJson() 0 9 2
A validateAssetMax() 0 6 1
A refersToExistingAsset() 0 6 3
A validateAssetMin() 0 6 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Thinktomorrow\Chief\Fields\ValidationRules;
5
6
use Thinktomorrow\AssetLibrary\Asset;
7
8
trait ValidatesExistingAssetAttributes
9
{
10
    protected function refersToExistingAsset($value): bool
11
    {
12
        // Faster check to see if there is an asset id passed or not.
13
        if(is_string($value) && $this->isValidJson($value)) return false;
14
15
        return !is_null($this->existingAsset($value));
16
    }
17
18
    protected function existingAsset($value): ?Asset
19
    {
20
        return Asset::where('id', $value)->first();
21
    }
22
23
    private function isValidJson($string): bool
24
    {
25
        try {
26
            json_decode($string);
27
        } catch (\Exception $e) {
28
            return false;
29
        }
30
31
        return (json_last_error() == JSON_ERROR_NONE);
32
    }
33
34
    protected function validateAssetDimensions(Asset $asset, $parameters)
35
    {
36
        $filepath = $asset->media->first()->getPath();
0 ignored issues
show
Bug introduced by
The property media does not seem to exist on Thinktomorrow\AssetLibrary\Asset. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
37
38
        if (! $sizeDetails = @getimagesize($filepath)) {
39
            return false;
40
        }
41
42
        $this->requireParameterCount(1, $parameters, 'dimensions');
0 ignored issues
show
Bug introduced by
It seems like requireParameterCount() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

42
        $this->/** @scrutinizer ignore-call */ 
43
               requireParameterCount(1, $parameters, 'dimensions');
Loading history...
43
        [$width, $height] = $sizeDetails;
44
45
        return $this->lowlevelDimensionsCheck($width, $height, $parameters);
46
    }
47
48
    /** Taken from the Laravel ValidatedAttributes::validateDimensions */
49
    private function lowlevelDimensionsCheck($width, $height, $parameters): bool
50
    {
51
        $parameters = $this->parseNamedParameters($parameters);
0 ignored issues
show
Bug introduced by
It seems like parseNamedParameters() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

51
        /** @scrutinizer ignore-call */ 
52
        $parameters = $this->parseNamedParameters($parameters);
Loading history...
52
53
        if ($this->failsBasicDimensionChecks($parameters, $width, $height) ||
0 ignored issues
show
Bug introduced by
It seems like failsBasicDimensionChecks() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

53
        if ($this->/** @scrutinizer ignore-call */ failsBasicDimensionChecks($parameters, $width, $height) ||
Loading history...
54
            $this->failsRatioCheck($parameters, $width, $height)) {
0 ignored issues
show
Bug introduced by
It seems like failsRatioCheck() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

54
            $this->/** @scrutinizer ignore-call */ 
55
                   failsRatioCheck($parameters, $width, $height)) {
Loading history...
55
            return false;
56
        }
57
58
        return true;
59
    }
60
61
    protected function validateAssetMimetypes(Asset $asset, $parameters)
62
    {
63
        return (in_array($asset->getMimeType(), $parameters) ||
64
            in_array(explode('/', $asset->getMimeType())[0].'/*', $parameters));
65
    }
66
67
    protected function validateAssetMax(Asset $asset, $parameters)
68
    {
69
        // Asset size in bytes
70
        $assetSize = $asset->media->first()->size;
0 ignored issues
show
Bug introduced by
The property media does not seem to exist on Thinktomorrow\AssetLibrary\Asset. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
71
72
        return ($assetSize / 1024) <= $parameters[0];
73
    }
74
75
    protected function validateAssetMin(Asset $asset, $parameters)
76
    {
77
        // Asset size in bytes
78
        $assetSize = $asset->media->first()->size;
0 ignored issues
show
Bug introduced by
The property media does not seem to exist on Thinktomorrow\AssetLibrary\Asset. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
79
80
        return ($assetSize / 1024) >= $parameters[0];
81
    }
82
}
83