PickValidator::IsPickValidForUpdate()   B
last analyzed

Complexity

Conditions 10
Paths 64

Size

Total Lines 40
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 40
rs 7.6666
c 0
b 0
f 0
cc 10
nc 64
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Pick;
8
use PhpDraft\Domain\Models\PhpDraftResponse;
9
10
class PickValidator {
11
  private $app;
12
13
  public function __construct(Application $app) {
14
    $this->app = $app;
15
  }
16
17
  public function IsPickValidForAdd(Draft $draft, Pick $pick) {
18
    $valid = true;
19
    $errors = array();
20
    $teams = $this->app['phpdraft.DraftDataRepository']->GetTeams($draft->draft_sport);
21
    $positions = $this->app['phpdraft.DraftDataRepository']->GetPositions($draft->draft_sport);
22
23
    if (empty($pick->first_name)
24
      || empty($pick->last_name)
25
      || empty($pick->team)
26
      || empty($pick->position)) {
27
      $errors[] = "One or more missing fields.";
28
      $valid = false;
29
    }
30
31
    if ($pick->draft_id != $draft->draft_id) {
32
      $errors[] = "Pick does not belong to draft #$draft->draft_id.";
33
      $valid = false;
34
    }
35
36
    if (strlen($pick->first_name) > 255) {
37
      $errors[] = "First name is above maximum length.";
38
      $valid = false;
39
    }
40
41
    if (strlen($pick->last_name) > 255) {
42
      $errors[] = "Last name is above maximum length.";
43
      $valid = false;
44
    }
45
46
    if ($draft->draft_current_pick != $pick->player_pick) {
47
      $errors[] = "Pick #$pick->player_pick is not the current pick for draft #$draft->draft_id.";
48
      $valid = false;
49
    }
50
51
    if (!array_key_exists($pick->team, $teams)) {
52
      $errors[] = "Team $pick->team is an invalid value.";
53
      $valid = false;
54
    }
55
56
    if (!array_key_exists($pick->position, $positions)) {
57
      $errors[] = "Position $pick->position is an invalid value.";
58
      $valid = false;
59
    }
60
61
    return $this->app['phpdraft.ResponseFactory']($valid, $errors);
62
  }
63
64
  public function IsPickValidForUpdate(Draft $draft, Pick $pick) {
65
    $valid = true;
66
    $errors = array();
67
    $teams = $this->app['phpdraft.DraftDataRepository']->GetTeams($draft->draft_sport);
68
    $positions = $this->app['phpdraft.DraftDataRepository']->GetPositions($draft->draft_sport);
69
70
    if (empty($pick->first_name)
71
      || empty($pick->last_name)
72
      || empty($pick->team)
73
      || empty($pick->position)) {
74
      $errors[] = "One or more missing fields.";
75
      $valid = false;
76
    }
77
78
    if ($pick->draft_id != $draft->draft_id) {
79
      $errors[] = "Pick does not belong to draft #$draft->draft_id.";
80
      $valid = false;
81
    }
82
83
    if (strlen($pick->first_name) > 255) {
84
      $errors[] = "First name is above maximum length.";
85
      $valid = false;
86
    }
87
88
    if (strlen($pick->last_name) > 255) {
89
      $errors[] = "Last name is above maximum length.";
90
      $valid = false;
91
    }
92
93
    if (!array_key_exists($pick->team, $teams)) {
94
      $errors[] = "Team $pick->team is an invalid value.";
95
      $valid = false;
96
    }
97
98
    if (!array_key_exists($pick->position, $positions)) {
99
      $errors[] = "Position $pick->position is an invalid value.";
100
      $valid = false;
101
    }
102
103
    return $this->app['phpdraft.ResponseFactory']($valid, $errors);
104
  }
105
106
  public function IsPickValidForDepthChartUpdate(Draft $draft, Pick $pick) {
107
    $valid = true;
108
    $errors = array();
109
110
    if ($pick->draft_id != $draft->draft_id) {
111
      $errors[] = "Pick does not belong to draft #$draft->draft_id.";
112
      $valid = false;
113
    }
114
115
    return $this->app['phpdraft.ResponseFactory']($valid, $errors);
116
  }
117
}