DraftController   A
last analyzed

Complexity

Total Complexity 25

Size/Duplication

Total Lines 191
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 25
eloc 107
dl 0
loc 191
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A Update() 0 32 5
A Delete() 0 7 1
A Get() 0 12 2
A _BuildDepthChartPositionModel() 0 18 2
A SetTimers() 0 31 6
A Create() 0 36 5
A GetTimers() 0 7 1
A UpdateStatus() 0 16 2
A GetCreate() 0 14 1
1
<?php
2
3
namespace PhpDraft\Controllers\Commish;
4
5
use \Silex\Application;
6
use Symfony\Component\HttpFoundation\Response;
7
use Symfony\Component\HttpFoundation\Request;
8
use PhpDraft\Domain\Models\PhpDraftResponse;
9
use PhpDraft\Domain\Entities\Draft;
10
11
class DraftController
12
{
13
  public function GetCreate(Application $app, Request $request) {
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

13
  public function GetCreate(Application $app, /** @scrutinizer ignore-unused */ Request $request) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
14
    $currentUser = $app['phpdraft.LoginUserService']->GetCurrentUser();
15
16
    $draft = new Draft();
17
18
    $draft->draft_style = 'serpentine';
19
    $draft->commish_id = $currentUser->id;
20
    $draft->commish_name = $currentUser->name;
21
    $draft->draft_sport = 'NFL';
22
23
    $draft->sports = $app['phpdraft.DraftDataRepository']->GetSports();
24
    $draft->styles = $app['phpdraft.DraftDataRepository']->GetStyles();
25
26
    return $app->json($draft, Response::HTTP_OK);
27
  }
28
29
  public function Create(Application $app, Request $request) {
30
    $currentUser = $app['phpdraft.LoginUserService']->GetCurrentUser();
31
    $draft = new Draft();
32
33
    $draft->commish_id = $currentUser->id;
34
    $draft->commish_name = $currentUser->name;
35
36
    $draft->draft_name = $request->get('name');
37
    $draft->draft_sport = $request->get('sport');
38
    $draft->draft_status = "undrafted";
39
    $draft->draft_style = $request->get('style');
40
    $draft->draft_rounds = (int)$request->get('rounds');
41
    $draft->draft_password = $request->get('password');
42
    $draft->using_depth_charts = $request->get('using_depth_charts') == true ? 1 : 0;
0 ignored issues
show
Documentation Bug introduced by
The property $using_depth_charts was declared of type boolean, but $request->get('using_dep...harts') == true ? 1 : 0 is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
43
44
    $validity = $app['phpdraft.DraftValidator']->IsDraftValidForCreateAndUpdate($draft);
45
46
    if (!$validity->success) {
47
      return $app->json($validity, Response::HTTP_BAD_REQUEST);
48
    }
49
50
    $createPositionsModel = null;
51
52
    if ($draft->using_depth_charts == 1) {
53
      $createPositionsModel = $this->_BuildDepthChartPositionModel($request);
54
55
      $positionValidity = $app['phpdraft.DepthChartPositionValidator']->AreDepthChartPositionsValid($createPositionsModel);
56
57
      if (!$positionValidity->success) {
58
        return $app->json($positionValidity, Response::HTTP_BAD_REQUEST);
59
      }
60
    }
61
62
    $response = $app['phpdraft.DraftService']->CreateNewDraft($draft, $createPositionsModel);
63
64
    return $app->json($response, $response->responseType(Response::HTTP_CREATED));
65
  }
66
67
  public function Get(Application $app, Request $request) {
68
    $draftId = (int)$request->get('draft_id');
69
    $draft = $app['phpdraft.DraftRepository']->Load($draftId);
70
71
    $draft->sports = $app['phpdraft.DraftDataRepository']->GetSports();
72
    $draft->styles = $app['phpdraft.DraftDataRepository']->GetStyles();
73
    $draft->statuses = $app['phpdraft.DraftDataRepository']->GetStatuses();
74
    $draft->depthChartPositions = $draft->using_depth_charts == 1
75
      ? $app['phpdraft.DepthChartPositionRepository']->LoadAll($draftId)
76
      : array();
77
78
    return $app->json($draft, Response::HTTP_OK);
79
  }
80
81
  public function Update(Application $app, Request $request) {
82
    $draftId = (int)$request->get('draft_id');
83
    $draft = $app['phpdraft.DraftRepository']->Load($draftId);
84
85
    $draft->draft_name = $request->get('name');
86
    $draft->draft_sport = $request->get('sport');
87
    $draft->draft_style = $request->get('style');
88
    $draft->draft_rounds = (int)$request->get('rounds');
89
    $draft->draft_password = $request->get('password');
90
    $draft->using_depth_charts = $request->get('using_depth_charts') == true ? 1 : 0;
91
92
    $validity = $app['phpdraft.DraftValidator']->IsDraftValidForCreateAndUpdate($draft);
93
94
    if (!$validity->success) {
95
      return $app->json($validity, Response::HTTP_BAD_REQUEST);
96
    }
97
98
    $createPositionsModel = null;
99
100
    if ($draft->using_depth_charts == 1) {
101
      $createPositionsModel = $this->_BuildDepthChartPositionModel($request);
102
103
      $positionValidity = $app['phpdraft.DepthChartPositionValidator']->AreDepthChartPositionsValid($createPositionsModel);
104
105
      if (!$positionValidity->success) {
106
        return $app->json($positionValidity, Response::HTTP_BAD_REQUEST);
107
      }
108
    }
109
110
    $response = $app['phpdraft.DraftService']->UpdateDraft($draft, $createPositionsModel);
111
112
    return $app->json($response, $response->responseType());
113
  }
114
115
  public function UpdateStatus(Application $app, Request $request) {
116
    $draftId = (int)$request->get('draft_id');
117
    $draft = $app['phpdraft.DraftRepository']->Load($draftId);
118
119
    $oldStatus = $draft->draft_status;
120
    $draft->draft_status = $request->get('status');
121
122
    $validity = $app['phpdraft.DraftValidator']->IsDraftStatusValid($draft, $oldStatus);
123
124
    if (!$validity->success) {
125
      return $app->json($validity, Response::HTTP_BAD_REQUEST);
126
    }
127
128
    $response = $app['phpdraft.DraftService']->UpdateDraftStatus($draft, $oldStatus);
129
130
    return $app->json($response, $response->responseType());
131
  }
132
133
  public function Delete(Application $app, Request $request) {
134
    $draftId = (int)$request->get('draft_id');
135
    $draft = $app['phpdraft.DraftRepository']->Load($draftId);
136
137
    $response = $app['phpdraft.DraftService']->DeleteDraft($draft);
138
139
    return $app->json($response, $response->responseType());
140
  }
141
142
  public function GetTimers(Application $app, Request $request) {
143
    $draftId = (int)$request->get('draft_id');
144
    $draft = $app['phpdraft.DraftRepository']->Load($draftId);
145
146
    $timers = $app['phpdraft.RoundTimeRepository']->GetDraftTimers($draft);
147
148
    return $app->json($timers, Response::HTTP_OK);
149
  }
150
151
  public function SetTimers(Application $app, Request $request) {
152
    $draftId = (int)$request->get('draft_id');
153
    $draft = $app['phpdraft.DraftRepository']->Load($draftId);
154
155
    $createModel = new \PhpDraft\Domain\Models\RoundTimeCreateModel();
156
    $createModel->isRoundTimesEnabled = (bool)$request->get('isRoundTimesEnabled');
157
158
    if ($createModel->isRoundTimesEnabled) {
159
      $roundTimesJson = $request->get('roundTimes');
160
161
      foreach ($roundTimesJson as $roundTimeRequest) {
162
        $newRoundTime = new \PhpDraft\Domain\Entities\RoundTime();
163
        $newRoundTime->draft_id = $draftId;
164
        $newRoundTime->is_static_time = $roundTimeRequest['is_static_time'] == "true" ? 1 : 0;
0 ignored issues
show
Documentation Bug introduced by
The property $is_static_time was declared of type boolean, but $roundTimeRequest['is_st...ime'] == 'true' ? 1 : 0 is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
165
        $newRoundTime->draft_round = $newRoundTime->is_static_time ? null : (int)$roundTimeRequest['draft_round'];
166
        $newRoundTime->round_time_seconds = (int)$roundTimeRequest['round_time_seconds'];
167
168
        $createModel->roundTimes[] = $newRoundTime;
169
      }
170
    }
171
172
    $validity = $app['phpdraft.RoundTimeValidator']->AreRoundTimesValid($createModel);
173
174
    if (!$validity->success) {
175
      return $app->json($validity, Response::HTTP_BAD_REQUEST);
176
    }
177
178
    //Save round times
179
    $response = $app['phpdraft.RoundTimeService']->SaveRoundTimes($draft, $createModel);
180
181
    return $app->json($response, $response->responseType(Response::HTTP_CREATED));
182
  }
183
184
  private function _BuildDepthChartPositionModel(Request $request, $draftId = null) {
185
    $createPositionsModel = new \PhpDraft\Domain\Models\DepthChartPositionCreateModel();
186
    $createPositionsModel->depthChartEnabled = true;
187
188
    $depthChartPositionJson = $request->get('depthChartPositions');
189
    $display_order = 0;
190
191
    foreach ($depthChartPositionJson as $depthChartPositionRequest) {
192
      $depthChartPosition = new \PhpDraft\Domain\Entities\DepthChartPosition();
193
      $depthChartPosition->draft_id = $draftId;
194
      $depthChartPosition->position = $depthChartPositionRequest['position'];
195
      $depthChartPosition->slots = (int)$depthChartPositionRequest['slots'];
196
      $depthChartPosition->display_order = $display_order++;
197
198
      $createPositionsModel->positions[] = $depthChartPosition;
199
    }
200
201
    return $createPositionsModel;
202
  }
203
}
204