PickController::GetSelectedRoundPicks()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace PhpDraft\Controllers;
4
5
use Silex\Application;
6
use Symfony\Component\HttpFoundation\Request;
7
use PhpDraft\Domain\Models\PhpDraftResponse;
8
use PhpDraft\Domain\Models\PickSearchModel;
9
10
class PickController {
11
  public function GetAll(Application $app, Request $request) {
12
    $draftId = (int)$request->get('draft_id');
13
    $draft = $app['phpdraft.DraftRepository']->Load($draftId);
14
15
    $response = $app['phpdraft.PickService']->GetAll($draft);
16
17
    return $app->json($response, $response->responseType());
18
  }
19
20
  public function GetUpdated(Application $app, Request $request) {
21
    $draftId = (int)$request->get('draft_id');
22
    $pickCounter = (int)$request->get('pick_counter');
23
24
    $response = $app['phpdraft.PickService']->LoadUpdatedData($draftId, $pickCounter);
25
26
    return $app->json($response, $response->responseType());
27
  }
28
29
  public function GetLast(Application $app, Request $request) {
30
    $draftId = (int)$request->get('draft_id');
31
    $amount = (int)$request->get('amount');
32
33
    if ($amount == 0) {
34
      $amount = 10;
35
    }
36
37
    return $app->json($app['phpdraft.PickRepository']->LoadLastPicks($draftId, $amount));
38
  }
39
40
  public function GetNext(Application $app, Request $request) {
41
    $draftId = (int)$request->get('draft_id');
42
    $amount = (int)$request->get('amount');
43
44
    if ($amount == 0) {
45
      $amount = 10;
46
    }
47
48
    $draft = $app['phpdraft.DraftRepository']->Load($draftId);
49
50
    return $app->json($app['phpdraft.PickRepository']->LoadNextPicks($draftId, $draft->draft_current_pick, $amount));
51
  }
52
53
  public function GetAllManagerPicks(Application $app, Request $request) {
54
    $draftId = (int)$request->get('draft_id');
55
    $managerId = (int)$request->get('manager_id');
56
57
    $draft = $app['phpdraft.DraftRepository']->Load($draftId);
58
59
    return $app->json($app['phpdraft.PickRepository']->LoadManagerPicks($managerId, $draft, false));
60
  }
61
62
  public function GetSelectedManagerPicks(Application $app, Request $request) {
63
    $draftId = (int)$request->get('draft_id');
0 ignored issues
show
Unused Code introduced by
The assignment to $draftId is dead and can be removed.
Loading history...
64
    $managerId = (int)$request->get('manager_id');
65
66
    return $app->json($app['phpdraft.PickRepository']->LoadManagerPicks($managerId));
67
  }
68
69
  public function GetAllRoundPicks(Application $app, Request $request) {
70
    $draftId = (int)$request->get('draft_id');
71
    $draft = $app['phpdraft.DraftRepository']->Load($draftId);
72
    $draftRound = (int)$request->get('draft_round');
73
    $sortAscending = (bool)$request->get('sort_ascending');
74
75
    return $app->json($app['phpdraft.PickRepository']->LoadRoundPicks($draft, $draftRound, $sortAscending, false));
76
  }
77
78
  public function GetSelectedRoundPicks(Application $app, Request $request) {
79
    $draftId = (int)$request->get('draft_id');
80
    $draft = $app['phpdraft.DraftRepository']->Load($draftId);
81
    $draftRound = (int)$request->get('draft_round');
82
    $sortAscending = (bool)$request->get('sort_ascending');
83
84
    return $app->json($app['phpdraft.PickRepository']->LoadRoundPicks($draft, $draftRound, $sortAscending));
85
  }
86
87
  public function SearchPicks(Application $app, Request $request) {
88
    $draftId = (int)$request->get('draft_id');
89
    $keywords = $request->get('keywords');
90
    $team = $request->get('team');
91
    $position = $request->get('position');
92
    $sort = $request->get('sort');
93
94
    $team = isset($team) ? $team : null;
95
    $position = isset($position) ? $position : null;
96
    $sort = isset($sort) ? $sort : 'DESC';
97
98
    $pickSearchModel = new PickSearchModel($draftId, $keywords, $team, $position, $sort);
99
100
    $pickSearchModel = $app['phpdraft.PickSearchRepository']->SearchStrict($pickSearchModel);
101
    $pickSearchModel = $app['phpdraft.PickSearchRepository']->SearchLoose($pickSearchModel);
102
103
    #If there's a space and no matches so far, create another searches where we manually split them firstname/lastname by sace automatically
104
    $splitNameAutomatically = count($pickSearchModel->player_results) == 0 && strpos($keywords, " ") != false;
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing strpos($keywords, ' ') of type integer to the boolean false. If you are specifically checking for non-zero, consider using something more explicit like > 0 or !== 0 instead.
Loading history...
105
106
    if ($splitNameAutomatically) {
107
      $pickSearchModel = new PickSearchModel($draftId, $keywords, $team, $position, $sort);
108
      $names = explode(" ", $keywords, 2);
109
      $pickSearchModel = $app['phpdraft.PickSearchRepository']->SearchSplit($pickSearchModel, $names[0], $names[1]);
110
    }
111
112
    return $app->json($pickSearchModel);
113
  }
114
115
  public function UpdateDepthChart(Application $app, Request $request) {
116
    $draft_id = (int)$request->get('draft_id');
117
    $pick_id = (int)$request->get('pick_id');
118
    $draft = $app['phpdraft.DraftRepository']->Load($draft_id);
119
120
    try {
121
      $pick = $app['phpdraft.PickRepository']->Load($pick_id);
122
123
      $pick->depth_chart_position_id = (int)$request->get('position_id');
124
    } catch (\Exception $e) {
125
      $response = new PhpDraftResponse(false, array());
126
      $response->errors[] = "Unable to edit pick #$pick_id";
127
128
      return $app->json($response, $response->responseType());
129
    }
130
131
    $validity = $app['phpdraft.PickValidator']->IsPickValidForDepthChartUpdate($draft, $pick);
132
133
    if (!$validity->success) {
134
      return $app->json($validity, $validity->responseType());
135
    }
136
137
    $response = $app['phpdraft.PickService']->UpdatePickDepthChart($draft, $pick);
138
139
    return $app->json($response, $response->responseType());
140
  }
141
}
142