ImageValidator   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 3
dl 0
loc 48
c 0
b 0
f 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 12 2
A _checkWidth() 0 8 3
A _checkHeight() 0 8 3
A _checkType() 0 6 3
1
<?php
2
3
namespace HustleWorks\Chute;
4
5
use HustleWorks\Chute\DTO\ImageFile;
6
use HustleWorks\Chute\DTO\ImageRuleConfiguration;
7
use HustleWorks\Chute\Contracts\ImageValidatorInterface;
8
9
class ImageValidator implements ImageValidatorInterface
10
{
11
    private $reasons_to_fail = [];
12
13
    /**
14
     * @param ImageFile              $file
15
     * @param ImageRuleConfiguration $rules
16
     * @return \HustleWorks\Chute\ServiceResponse
17
     */
18
    public function validate(ImageFile $file, ImageRuleConfiguration $rules): ServiceResponse
19
    {
20
        $this->_checkWidth($file, $rules);
21
        $this->_checkHeight($file, $rules);
22
        $this->_checkType($file, $rules);
23
24
        return new StandardServiceResponse(
25
            !sizeof($this->reasons_to_fail),
26
            [],
27
            sizeof($this->reasons_to_fail) ? "Message was invalid for the following reasons: " . implode(', ', $this->reasons_to_fail) : "Valid image."
28
        );
29
    }
30
31
    private function _checkWidth(ImageFile $file, ImageRuleConfiguration $rules)
32
    {
33
        if ($file->width < $rules->min_width) {
34
            $this->reasons_to_fail[] = 'image too narrow';
35
        } elseif ($file->width > ($rules->max_width ?? PHP_INT_MAX)) {
36
            $this->reasons_to_fail[] = 'image too wide';
37
        }
38
    }
39
40
    private function _checkHeight(ImageFile $file, ImageRuleConfiguration $rules)
41
    {
42
        if ($file->height < $rules->min_height) {
43
            $this->reasons_to_fail[] = 'image too short';
44
        } elseif ($file->height > ($rules->max_height ?? PHP_INT_MAX)) {
45
            $this->reasons_to_fail[] = 'image too tall';
46
        }
47
    }
48
49
    private function _checkType(ImageFile $file, ImageRuleConfiguration $rules)
50
    {
51
        if ($rules->mime_types and !in_array($file->mime_type, $rules->mime_types)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
52
            $this->reasons_to_fail[] = 'file type not allowed';
53
        }
54
    }
55
56
}