Completed
Push — master ( 8ce34a...26baa3 )
by Marcus
03:49
created

Time::validateSeconds()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 12
loc 12
ccs 6
cts 6
cp 1
rs 9.8666
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
1
<?php
2
3
namespace Mbright\Validation\Rule\Validate\MySql;
4
5
use Mbright\Validation\Rule\Validate\ValidateRuleInterface;
6
7
/**
8
 * Validates that data can be inserted into one of the following column types:
9
 * - Time
10
 */
11
class Time implements ValidateRuleInterface
12
{
13
    use TimeTypeTrait;
14
15
    /**
16
     * Indicates if the given field's value is a MySql safe Time.
17
     *
18
     * @param object $subject
19
     * @param string $field
20
     *
21
     * @return bool
22
     */
23 36
    public function __invoke($subject, string $field): bool
24
    {
25 36
        $value = $subject->$field;
26 36
        $timeParts = $this->extractTimeParts($value);
27
28 36
        return ! is_null($timeParts)
29 36
            && $this->validateHours($timeParts->hours)
30 36
            && $this->validateMinutes($timeParts->minutes)
31 36
            && $this->validateSeconds($timeParts->seconds);
32
    }
33
34
    /**
35
     * Validates that the string can represent seconds.
36
     *
37
     * @param string $seconds
38
     *
39
     * @return bool
40
     */
41 27 View Code Duplication
    protected function validateSeconds(string $seconds): bool
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
42
    {
43 27
        $precision = strlen(substr(strrchr($seconds, '.'), 1));
44
45 27
        if ($precision > 6) {
46 6
            return false;
47
        }
48
49 21
        $seconds = (float) $seconds;
50
51 21
        return $seconds >= 0 && $seconds < 60;
52
    }
53
54
    /**
55
     * Validates that the string can represent minutes.
56
     *
57
     * @param string $minutes
58
     *
59
     * @return bool
60
     */
61 27
    protected function validateMinutes(string $minutes): bool
62
    {
63 27
        $minutes = (int) $minutes;
64
65 27
        return $minutes >= 0 && $minutes < 60;
66
    }
67
68
    /**
69
     * Validates tha the string can represent hours.
70
     *
71
     * @param string $hours
72
     *
73
     * @return bool
74
     */
75 33
    protected function validateHours(string $hours): bool
76
    {
77 33
        $hours = (int) $hours;
78
79 33
        return $hours >= -838 && $hours <= 838;
80
    }
81
}
82