DepthChartPositionValidator   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 11
eloc 28
dl 0
loc 51
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A AreDepthChartPositionsValid() 0 31 6
A IsDraftSportValid() 0 11 4
1
<?php
2
namespace PhpDraft\Domain\Validators;
3
4
use \Silex\Application;
5
use PhpDraft\Domain\Models\PhpDraftResponse;
6
use PhPDraft\Domain\Models\DepthChartPositionCreateModel;
7
8
class DepthChartPositionValidator {
9
  private $app;
10
11
  public function __construct(Application $app) {
12
    $this->app = $app;
13
  }
14
15
  public function IsDraftSportValid($draft_sport) {
16
    $valid = true;
17
    $errors = array();
18
    $draft_sports = $this->app['phpdraft.DraftDataRepository']->GetSports();
19
20
    if (strlen($draft_sport) < 3 || strlen($draft_sport) > 4 || strlen($draft_sports[$draft_sport]) == 0) {
21
      $errors[] = "Draft sport is an invalid value.";
22
      $valid = false;
23
    }
24
25
    return $this->app['phpdraft.ResponseFactory']($valid, $errors);
26
  }
27
28
  public function AreDepthChartPositionsValid(DepthChartPositionCreateModel $depthChartPositionCreateModel) {
29
    $valid = true;
30
    $errors = array();
31
32
    if (count($depthChartPositionCreateModel->positions) !== count(array_unique($depthChartPositionCreateModel->positions))) {
33
      $valid = false;
34
      $errors[] = "One or more of the positions are not unique.";
35
    }
36
37
    $rounds = 0;
38
39
    foreach ($depthChartPositionCreateModel->positions as $depthChartPosition) {
40
      $rounds += (int)$depthChartPosition->slots;
41
    }
42
43
    if ($rounds == 0) {
0 ignored issues
show
introduced by
The condition $rounds == 0 is always true.
Loading history...
44
      $valid = false;
45
      $errors[] = "The depth chart positions must specify at least 1 slot";
46
    }
47
48
    if ($rounds > 30) {
49
      $valid = false;
50
      $errors[] = "The depth chart positions cannot specify more than 30 slots in total.";
51
    }
52
53
    if (count($depthChartPositionCreateModel->positions) > 30) {
54
      $valid = false;
55
      $errors[] = "Too many depth chart positions have been provided - 30 is the maximum.";
56
    }
57
58
    return $this->app['phpdraft.ResponseFactory']($valid, $errors);
59
  }
60
}