RecurrenceValidator   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 8
eloc 10
dl 0
loc 24
ccs 9
cts 9
cp 1
rs 10
c 1
b 0
f 1

1 Method

Rating   Name   Duplication   Size   Complexity  
B validate() 0 19 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
class RecurrenceValidator
11
{
12
    /**
13
     * @throws InvalidRecurrenceException
14
     */
15
    public static function validate(Recurrence $recurrence): bool
16
    {
17 1
        if (!$recurrence->getFrequency()) {
18 1
            throw new InvalidRecurrenceException('Frequency is required');
19
        }
20
21 1
        if ($recurrence->hasCount() && $recurrence->getPeriodEndAt()) {
22 1
            throw new InvalidRecurrenceException('Recurrence cannot have [COUNT] and [UNTIL] option at the same time');
23
        }
24
25 1
        if (!$recurrence->hasCount() && !$recurrence->getPeriodEndAt()) {
26 1
            throw new InvalidRecurrenceException('Recurrence required [COUNT] or [UNTIL] option');
27
        }
28
29 1
        if ($recurrence->hasConstraint(EndOfMonthConstraint::class) && (string) $recurrence->getFrequency() != Frequency::FREQUENCY_MONTHLY) {
30 1
            throw new InvalidRecurrenceException('End of month constraint can be applied only with monthly frequency');
31
        }
32
33 1
        return true;
34
    }
35
}
36