PickController::AlreadyDrafted()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace PhpDraft\Controllers\Commish;
4
5
use \Silex\Application;
6
use Symfony\Component\HttpFoundation\Request;
7
use Symfony\Component\HttpFoundation\Response;
8
use PhpDraft\Domain\Entities\ProPlayer;
9
use PhpDraft\Domain\Models\PhpDraftResponse;
10
11
class PickController {
12
  public function GetCurrent(Application $app, Request $request) {
13
    $draft_id = $request->get('draft_id');
14
    $draft = $app['phpdraft.DraftRepository']->Load($draft_id);
15
    
16
    $response = $app['phpdraft.PickService']->GetCurrentPick($draft);
17
18
    return $app->json($response, $response->responseType());
19
  }
20
21
  public function Add(Application $app, Request $request) {
22
    $draft_id = $request->get('draft_id');
23
    $draft = $app['phpdraft.DraftRepository']->Load($draft_id);
24
    $pick_id = $request->get('player_id');
25
26
    try {
27
      $pick = $app['phpdraft.PickRepository']->Load($pick_id);
28
29
      $pick->first_name = $request->get('first_name');
30
      $pick->last_name = $request->get('last_name');
31
      $pick->team = $request->get('team');
32
      $pick->position = $request->get('position');
33
    } catch (\Exception $e) {
34
      $response = new PhpDraftResponse(false, array());
35
      $response->errors[] = "Unable to add pick #$pick_id";
36
37
      return $app->json($response, $response->responseType());
38
    }
39
40
    $validity = $app['phpdraft.PickValidator']->IsPickValidForAdd($draft, $pick);
41
42
    if (!$validity->success) {
43
      return $app->json($validity, $validity->responseType());
44
    }
45
46
    $response = $app['phpdraft.PickService']->AddPick($draft, $pick);
47
48
    return $app->json($response, $response->responseType(Response::HTTP_CREATED));
49
  }
50
51
  public function Update(Application $app, Request $request) {
52
    $draft_id = $request->get('draft_id');
53
    $draft = $app['phpdraft.DraftRepository']->Load($draft_id);
54
    $pick_id = $request->get('player_id');
55
56
    try {
57
      $pick = $app['phpdraft.PickRepository']->Load($pick_id);
58
59
      $pick->first_name = $request->get('first_name');
60
      $pick->last_name = $request->get('last_name');
61
      $pick->team = $request->get('team');
62
      $pick->position = $request->get('position');
63
    } catch (\Exception $e) {
64
      $response = new PhpDraftResponse(false, array());
65
      $response->errors[] = "Unable to edit pick #$pick_id";
66
67
      return $app->json($response, $response->responseType());
68
    }
69
70
    $validity = $app['phpdraft.PickValidator']->IsPickValidForUpdate($draft, $pick);
71
72
    if (!$validity->success) {
73
      return $app->json($validity, $validity->responseType());
74
    }
75
76
    $response = $app['phpdraft.PickService']->UpdatePick($draft, $pick);
77
78
    return $app->json($response, $response->responseType());
79
  }
80
81
  public function AlreadyDrafted(Application $app, Request $request) {
82
    $draft_id = $request->get('draft_id');
83
    $first_name = $request->get('first_name');
84
    $last_name = $request->get('last_name');
85
86
    $response = $app['phpdraft.PickService']->AlreadyDrafted($draft_id, $first_name, $last_name);
87
88
    return $app->json($response, $response->responseType());
89
  }
90
91
  //This will also be used as the "Create" method to populate a list of valid rounds for the view to use
92
  public function GetLast5(Application $app, Request $request) {
93
    $draft_id = $request->get('draft_id');
94
    $response = new PhpDraftResponse();
95
96
    try {
97
      $draft = $app['phpdraft.DraftRepository']->Load($draft_id);
98
99
      $response->draft_rounds = $draft->draft_rounds;
0 ignored issues
show
Bug introduced by
The property draft_rounds does not seem to exist on PhpDraft\Domain\Models\PhpDraftResponse.
Loading history...
100
      $response->last_5_picks = $app['phpdraft.PickRepository']->LoadLastPicks($draft_id, 5);
0 ignored issues
show
Bug introduced by
The property last_5_picks does not seem to exist on PhpDraft\Domain\Models\PhpDraftResponse.
Loading history...
101
      $response->success = true;
102
    } catch (\Exception $e) {
103
      $response->success = false;
104
      $response->errors[] = "Unable to load last 5 picks.";
105
    }
106
107
    return $app->json($response, $response->responseType());
108
  }
109
110
  public function GetByRound(Application $app, Request $request) {
111
    $draft_id = $request->get('draft_id');
112
    $draft = $app['phpdraft.DraftRepository']->Load($draft_id);
113
    $round = (int)$request->get('draft_round');
114
    $response = new PhpDraftResponse();
115
116
    try {
117
      $response->round = $round;
0 ignored issues
show
Bug introduced by
The property round does not seem to exist on PhpDraft\Domain\Models\PhpDraftResponse.
Loading history...
118
      $response->round_picks = $app['phpdraft.PickRepository']->LoadRoundPicks($draft, $round, false, true);
0 ignored issues
show
Bug introduced by
The property round_picks does not seem to exist on PhpDraft\Domain\Models\PhpDraftResponse.
Loading history...
119
      $response->success = true;
120
    } catch (\Exception $e) {
121
      $response->success = false;
122
      $response->errors[] = "Unable to load round #$round's picks";
123
    }
124
125
    return $app->json($response, $response->responseType());
126
  }
127
}