FileValidator::validateOrientation()   B
last analyzed

Complexity

Conditions 7
Paths 8

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 7
eloc 16
nc 8
nop 2
dl 0
loc 23
ccs 0
cts 17
cp 0
crap 56
rs 8.8333
c 1
b 0
f 1
1
<?php
2
3
namespace ByTIC\MediaLibrary\Validation\Validators;
4
5
use ByTIC\MediaLibrary\Validation\Constraints\FileConstraint;
6
use ByTIC\MediaLibrary\Validation\Constraints\ImageConstraint;
7
use Nip\Logger\Exception;
8
use Symfony\Component\HttpFoundation\File\UploadedFile;
9
10
/**
11
 * Class FileValidator.
12
 */
13
class FileValidator extends AbstractValidator
14
{
15
    public const KB_BYTES = 1000;
16
    public const MB_BYTES = 1000000;
17
    public const KIB_BYTES = 1024;
18
    public const MIB_BYTES = 1048576;
19
20
    private static $suffices = [
0 ignored issues
show
introduced by
The private property $suffices is not used, and could be removed.
Loading history...
21
        1               => 'bytes',
22
        self::KB_BYTES  => 'kB',
23
        self::MB_BYTES  => 'MB',
24
        self::KIB_BYTES => 'KiB',
25
        self::MIB_BYTES => 'MiB',
26
    ];
27
28
    /**
29
     * @return bool
30
     */
31 2
    protected function contraintNeedsValidation(): bool
32
    {
33 2
        $constraint = $this->getConstraint();
34
35 2
        if (null === $constraint->mimeTypes && null === $constraint->maxSize) {
36
            return false;
37
        }
38
39 2
        return true;
40
    }
41
42
    /**
43
     * @throws Exception
44
     */
45 2
    protected function doValidation()
46
    {
47 2
        $this->validateMimeType();
48 2
    }
49
50
    /**
51
     * @return void
52
     */
53 2
    protected function validateMimeType()
54
    {
55 2
        $constraint = $this->getConstraint();
56
57 2
        if ($constraint->mimeTypes) {
58
            $mimeTypes = (array) $constraint->mimeTypes;
59
            $mime = $this->determineMime();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $mime is correct as $this->determineMime() targeting ByTIC\MediaLibrary\Valid...idator::determineMime() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
60
            foreach ($mimeTypes as $mimeType) {
61
                if ($mimeType === $mime) {
62
                    return;
63
                }
64
                if ($discrete = strstr($mimeType, '/*', true)) {
65
                    if (strstr($mime, '/', true) === $discrete) {
0 ignored issues
show
Bug introduced by
$mime of type null is incompatible with the type string expected by parameter $haystack of strstr(). ( Ignorable by Annotation )

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

65
                    if (strstr(/** @scrutinizer ignore-type */ $mime, '/', true) === $discrete) {
Loading history...
66
                        return;
67
                    }
68
                }
69
            }
70
            $this->addViolation($constraint, FileConstraint::INVALID_MIME_TYPE_ERROR, []);
71
        }
72 2
    }
73
74
    /**
75
     * @return string|null
76
     */
77
    protected function determineMime()
78
    {
79
        $file = $this->getValue();
80
        if ($file instanceof UploadedFile) {
0 ignored issues
show
introduced by
$file is never a sub-type of Symfony\Component\HttpFoundation\File\UploadedFile.
Loading history...
81
            return $file->getClientMimeType();
82
        }
83
    }
84
85
    /**
86
     * @param $width
87
     * @param $height
88
     */
89
    protected function validateOrientation($width, $height)
90
    {
91
        $constraint = $this->getConstraint();
92
93
        if (!$constraint->allowSquare && $width == $height) {
94
            $this->addViolation(
95
                $constraint,
96
                ImageConstraint::SQUARE_NOT_ALLOWED_ERROR,
97
                ['width' => $width, 'height' => $height]
98
            );
99
        }
100
        if (!$constraint->allowLandscape && $width > $height) {
101
            $this->addViolation(
102
                $constraint,
103
                ImageConstraint::LANDSCAPE_NOT_ALLOWED_ERROR,
104
                ['width' => $width, 'height' => $height]
105
            );
106
        }
107
        if (!$constraint->allowPortrait && $width < $height) {
108
            $this->addViolation(
109
                $constraint,
110
                ImageConstraint::PORTRAIT_NOT_ALLOWED_ERROR,
111
                ['width' => $width, 'height' => $height]
112
            );
113
        }
114
    }
115
}
116