ManagerValidator::AreManagersValidForCreate()   B
last analyzed

Complexity

Conditions 10
Paths 54

Size

Total Lines 42
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 42
rs 7.6666
c 0
b 0
f 0
cc 10
nc 54
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\Manager;
8
use PhpDraft\Domain\Models\PhpDraftResponse;
9
10
class ManagerValidator {
11
  private $app;
12
13
  public function __construct(Application $app) {
14
    $this->app = $app;
15
  }
16
17
  public function IsManagerValidForCreate($draft_id, Manager $manager) {
18
    $valid = true;
19
    $errors = array();
20
21
    try {
22
      $current_manager_count = $this->app['phpdraft.ManagerRepository']->GetNumberOfCurrentManagers($draft_id);
23
    } catch (\Exception $e) {
24
      $errors[] = $e->getMessage();
25
      $valid = false;
26
      $current_manager_count = 0;
27
    }
28
    
29
    if (empty($manager->draft_id)
30
      || empty($manager->manager_name)) {
31
      $errors[] = "One or more missing fields.";
32
      $valid = false;
33
    }
34
35
    if (!empty($manager->manager_name) && strlen($manager->manager_name) > 255) {
36
      $errors[] = "Manager name is above maximum length.";
37
      $valid = false;
38
    }
39
40
    if ($current_manager_count > 19) {
41
      $errors[] = "Unable to add more managers - 20 is the maximum number of managers allowed.";
42
      $valid = false;
43
    }
44
45
    if (!$this->app['phpdraft.ManagerRepository']->NameIsUnique($manager->manager_name)) {
46
      $errors[] = "Manager name '$manager->manager_name' already exists - please choose another name.";
47
      $valid = false;
48
    }
49
50
    return new PhpDraftResponse($valid, $errors);
51
  }
52
53
  public function AreManagersValidForCreate($draft_id, $managersArray) {
54
    $valid = true;
55
    $errors = array();
56
    $current_manager_count = $this->app['phpdraft.ManagerRepository']->GetNumberOfCurrentManagers($draft_id);
57
    $numberOfNewManagers = count($managersArray);
58
    $maxNumberOfManagers = 20 - $current_manager_count;
59
    $numberOfManagersOver = $numberOfNewManagers - $maxNumberOfManagers;
60
    $uniqueManagersCount = count(array_unique($managersArray));
61
62
    if ($numberOfNewManagers > $maxNumberOfManagers) {
63
      $manager_noun = $numberOfManagersOver > 1 ? "managers" : "manager";
64
      $errors[] = "Unable to add $numberOfNewManagers new managers - the maximum number of managers is 20. Draft currently has $current_manager_count, so please remove $numberOfManagersOver $manager_noun to continue.";
65
      $valid = false;
66
    }
67
68
    if ($numberOfNewManagers != $uniqueManagersCount) {
69
      $errors[] = "Two or more managers had the same name - please ensure all managers have unique names.";
70
      $valid = false;
71
    }
72
73
    $manager_number = 1;
74
    foreach ($managersArray as $manager) {
75
      if (empty($manager->draft_id)
76
        || empty($manager->manager_name)) {
77
        $errors[] = "One or more missing fields for manager #$manager_number.";
78
        $valid = false;
79
      }
80
81
      if (!empty($manager->manager_name) && strlen($manager->manager_name) > 255) {
82
        $errors[] = "Manager #$manager_number's name is above the maximum length.";
83
        $valid = false;
84
      }
85
86
      if (!$this->app['phpdraft.ManagerRepository']->NameIsUnique($manager->manager_name, $draft_id)) {
87
        $errors[] = "Manager name '$manager->manager_name' already exists - please choose another name.";
88
        $valid = false;
89
      }
90
91
      $manager_number++;
92
    }
93
94
    return new PhpDraftResponse($valid, $errors);
95
  }
96
97
  public function AreManagerIdsValidForOrdering($draft_id, $managersIdArray) {
98
    $valid = true;
99
    $errors = array();
100
    $current_manager_count = $this->app['phpdraft.ManagerRepository']->GetNumberOfCurrentManagers($draft_id);
101
    $ids_given = count($managersIdArray);
102
103
    if (count($managersIdArray) == 0) {
104
      $errors[] = "One or more missing fields.";
105
      $valid = false;
106
    }
107
108
    if ($ids_given > 20) {
109
      $errors[] = "Too many managers given - maximum number of managers is 20";
110
      $valid = false;
111
    }
112
113
    if ($ids_given != $current_manager_count) {
114
      $manager_noun = $current_manager_count > 1 ? "managers" : "manager";
115
      $errors[] = "Incorrect number of managers given. Draft #$draft_id has $current_manager_count $manager_noun, but $ids_given were received.";
116
      $valid = false;
0 ignored issues
show
Unused Code introduced by
The assignment to $valid is dead and can be removed.
Loading history...
117
    }
118
119
    //Save ourself a little processing if we've been handled a faulty request:
120
    if ($valid = true) {
121
      foreach ($managersIdArray as $manager_id) {
122
        if (!$this->app['phpdraft.ManagerRepository']->ManagerExists($manager_id, $draft_id)) {
123
          $errors[] = "Manager #$manager_id does not exist.";
124
          $valid = false;
125
        }
126
      }
127
    }
128
129
    return new PhpDraftResponse($valid, $errors);
130
  }
131
132
  public function IsManagerValidForUpdate(Draft $draft, Manager $manager) {
133
    $valid = true;
134
    $errors = array();
135
    
136
    if (empty($manager->manager_id)
137
      || empty($manager->draft_id)
138
      || empty($manager->manager_name)) {
139
      $errors[] = "One or more missing fields.";
140
      $valid = false;
141
    }
142
143
    if ($draft->draft_id != $manager->draft_id) {
144
      $errors[] = "Manager does not belong to draft #$draft_id";
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $draft_id does not exist. Did you maybe mean $draft?
Loading history...
145
      $valid = false;
146
    }
147
148
    if (!empty($manager->manager_name) && strlen($manager->manager_name) > 255) {
149
      $errors[] = "Manager name is above maximum length.";
150
      $valid = false;
151
    }
152
153
    if (!$this->app['phpdraft.ManagerRepository']->NameIsUnique($manager->manager_name, $draft->draft_id, $manager->manager_id)) {
154
      $errors[] = "Manager name '$manager->manager_name' already exists - please choose another name.";
155
      $valid = false;
156
    }
157
158
    return new PhpDraftResponse($valid, $errors);
159
  }
160
}