Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Completed
Pull Request — master (#1034)
by Henrique
02:29
created

Size::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 4
nop 2
crap 3
1
<?php
2
3
/*
4
 * This file is part of Respect/Validation.
5
 *
6
 * (c) Alexandre Gomes Gaigalas <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the "LICENSE.md"
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Respect\Validation\Rules;
15
16
use Respect\Validation\Exceptions\ComponentException;
17
use SplFileInfo;
18
19
/**
20
 * Validate file size.
21
 *
22
 * @author Henrique Moody <[email protected]>
23
 */
24
class Size extends AbstractRule
25
{
26
    /**
27
     * @var string
28
     */
29
    public $minSize;
30
31
    /**
32
     * @var int
33
     */
34
    public $minValue;
35
36
    /**
37
     * @var string
38
     */
39
    public $maxSize;
40
41
    /**
42
     * @var int
43
     */
44
    public $maxValue;
45
46
    /**
47
     * @param string $minSize
48
     * @param string $maxSize
49
     */
50 30
    public function __construct($minSize = null, $maxSize = null)
51
    {
52 30
        $this->minSize = $minSize;
53 30
        $this->minValue = $minSize ? $this->toBytes($minSize) : null;
54 29
        $this->maxSize = $maxSize;
55 29
        $this->maxValue = $maxSize ? $this->toBytes($maxSize) : null;
56 29
    }
57
58
    /**
59
     * @todo Move it to a trait
60
     *
61
     * @param mixed $size
62
     *
63
     * @return int
64
     */
65 30
    private function toBytes($size)
66
    {
67 30
        $value = $size;
68 30
        $units = ['b', 'kb', 'mb', 'gb', 'tb', 'pb', 'eb', 'zb', 'yb'];
69 30
        foreach ($units as $exponent => $unit) {
70 30
            if (!preg_match("/^(\d+(.\d+)?){$unit}$/i", (string) $size, $matches)) {
71 28
                continue;
72
            }
73 28
            $value = $matches[1] * 1024 ** $exponent;
74 28
            break;
75
        }
76
77 30
        if (!is_numeric($value)) {
78 1
            throw new ComponentException(sprintf('"%s" is not a recognized file size.', $size));
79
        }
80
81 29
        return $value;
82
    }
83
84
    /**
85
     * @param int $size
86
     *
87
     * @return bool
88
     */
89 19
    private function isValidSize($size)
90
    {
91 19
        if (null !== $this->minValue && null !== $this->maxValue) {
92 5
            return $size >= $this->minValue && $size <= $this->maxValue;
93
        }
94
95 14
        if (null !== $this->minValue) {
96 8
            return $size >= $this->minValue;
97
        }
98
99 6
        return $size <= $this->maxValue;
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105 19
    public function validate($input): bool
106
    {
107 19
        if ($input instanceof SplFileInfo) {
108 1
            return $this->isValidSize($input->getSize());
109
        }
110
111 18
        if (is_string($input)) {
112 18
            return $this->isValidSize(filesize($input));
113
        }
114
115
        return false;
116
    }
117
}
118