Passed
Push — master ( ed2711...5bfb66 )
by Samuel
02:22
created

RecurrenceValidator::validate()   B

Complexity

Conditions 8
Paths 5

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 8

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 9
c 1
b 0
f 1
dl 0
loc 19
ccs 9
cts 9
cp 1
rs 8.4444
cc 8
nc 5
nop 1
crap 8
1
<?php
2
3
namespace Recurrence\Validator;
4
5
use Recurrence\Model\Frequency;
6
use Recurrence\Model\Recurrence;
7
use Recurrence\Constraint\ProviderConstraint\EndOfMonthConstraint;
8
use Recurrence\Model\Exception\InvalidRecurrenceException;
9
10
/**
11
 * Class RecurrenceValidator
12
 * @package Recurrence\Validator
13
 */
14
class RecurrenceValidator
15
{
16
    /**
17
     * @param Recurrence $recurrence
18
     * @throws InvalidRecurrenceException
19
     * @return bool
20
     */
21
    public static function validate(Recurrence $recurrence)
22
    {
23 1
        if (!$recurrence->getFrequency()) {
24 1
            throw new InvalidRecurrenceException('Frequency is required');
25
        }
26
27 1
        if ($recurrence->hasCount() && $recurrence->getPeriodEndAt()) {
28 1
            throw new InvalidRecurrenceException('Recurrence cannot have [COUNT] and [UNTIL] option at the same time');
29
        }
30
31 1
        if (!$recurrence->hasCount() && !$recurrence->getPeriodEndAt()) {
32 1
            throw new InvalidRecurrenceException('Recurrence required [COUNT] or [UNTIL] option');
33
        }
34
35 1
        if ($recurrence->hasConstraint(EndOfMonthConstraint::class) && (string) $recurrence->getFrequency() != Frequency::FREQUENCY_MONTHLY) {
36 1
            throw new InvalidRecurrenceException('End of month constraint can be applied only with monthly frequency');
37
        }
38
39 1
        return true;
40
    }
41
}
42