RoundTimeValidator::AreRoundTimesValid()   B
last analyzed

Complexity

Conditions 9
Paths 2

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 28
rs 8.0555
c 0
b 0
f 0
cc 9
nc 2
nop 1
1
<?php
2
namespace PhpDraft\Domain\Validators;
3
4
use \Silex\Application;
5
use Symfony\Component\HttpFoundation\Request;
6
use PhpDraft\Domain\Entities\Draft;
7
use PhpDraft\Domain\Entities\RoundTime;
8
use PhpDraft\Domain\Models\PhpDraftResponse;
9
use PhpDraft\Domain\Models\RoundTimeCreateModel;
10
11
class RoundTimeValidator {
12
  private $app;
13
14
  public function __construct(Application $app) {
15
    $this->app = $app;
16
  }
17
18
  public function AreRoundTimesValid(RoundTimeCreateModel $model) {
19
    $valid = true;
20
    $errors = array();
21
22
    if ($model->isRoundTimesEnabled) {
23
      $roundTimeNumber = 0;
24
      foreach ($model->roundTimes as $roundTime) {
25
        $roundTimeNumber++;
26
27
        if (empty($roundTime->draft_id) ||
28
          empty($roundTime->round_time_seconds)) {
29
          $errors[] = "Round time #$roundTimeNumber has one or more missing fields.";
30
          $valid = false;
31
        }
32
33
        if ($roundTime->round_time_seconds <= 0) {
34
          $errors[] = "Round time #$roundTimeNumber must have 1 or more seconds specified.";
35
          $valid = false;
36
        }
37
38
        if (!$roundTime->is_static_time && ($roundTime->draft_round < 1 || $roundTime->draft_round > 30)) {
39
          $errors[] = "Round time #$roundTimeNumber cannot have a round less than 1 or greater than 30.";
40
          $valid = false;
41
        }
42
      }
43
    }
44
45
    return new PhpDraftResponse($valid, $errors);
46
  }
47
}