Passed
Pull Request — master (#53)
by Matthew
02:21
created

DraftService::DraftSettingUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 2
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
1
<?php
2
namespace PhpDraft\Domain\Services;
3
4
use Silex\Application;
5
use Symfony\Component\HttpFoundation\Request;
6
use PhpDraft\Domain\Entities\Draft;
7
use PhpDraft\Domain\Models\PhpDraftResponse;
8
use PhpDraft\Domain\Models\DepthChartPositionCreateModel;
9
10
class DraftService {
11
  private $app;
12
13
  public function __construct(Application $app) {
14
    $this->app = $app;
15
  }
16
17
  public function GetCurrentPick($draft_id) {
18
    $draft_id = (int)$draft_id;
19
20
    $draft = $this->app['phpdraft.DraftRepository']->Load($draft_id);
21
22
    return (int)$draft->draft_current_pick;
23
  }
24
25
  public function CreateNewDraft(Draft $draft, DepthChartPositionCreateModel $depthChartModel = null) {
26
    $response = new PhpDraftResponse();
27
28
    try {
29
      $draft = $this->app['phpdraft.DraftRepository']->Create($draft);
30
31
      if ($draft->using_depth_charts) {
32
        $depth_charts = $this->app['phpdraft.DepthChartPositionRepository']->Save($depthChartModel, $draft->draft_id);
0 ignored issues
show
Unused Code introduced by
The assignment to $depth_charts is dead and can be removed.
Loading history...
33
      }
34
35
      $response->success = true;
36
      $response->draft = $draft;
0 ignored issues
show
Bug introduced by
The property draft does not seem to exist on PhpDraft\Domain\Models\PhpDraftResponse.
Loading history...
37
    } catch (\Exception $e) {
38
      $response->success = false;
39
      $response->errors = array($e->getMessage());
40
    }
41
42
    return $response;
43
  }
44
45
  public function UpdateDraft(Draft $draft, DepthChartPositionCreateModel $depthChartModel = null) {
46
    $response = new PhpDraftResponse();
47
48
    try {
49
      $draft = $this->app['phpdraft.DraftRepository']->Update($draft);
50
51
      if ($draft->using_depth_charts == 1) {
52
        $this->app['phpdraft.DepthChartPositionRepository']->DeleteAllDepthChartPositions($draft->draft_id);
53
        $depth_charts = $this->app['phpdraft.DepthChartPositionRepository']->Save($depthChartModel, $draft->draft_id);
0 ignored issues
show
Unused Code introduced by
The assignment to $depth_charts is dead and can be removed.
Loading history...
54
      }
55
56
      $response->success = true;
57
      $response->draft = $draft;
0 ignored issues
show
Bug introduced by
The property draft does not seem to exist on PhpDraft\Domain\Models\PhpDraftResponse.
Loading history...
58
    } catch (\Exception $e) {
59
      $response->success = false;
60
      $response->errors = array($e->getMessage());
61
    }
62
63
    return $response;
64
  }
65
66
  public function UpdateDraftStatus(Draft $draft, $old_status) {
67
    $response = new PhpDraftResponse();
68
69
    try {
70
      $draft = $this->app['phpdraft.DraftRepository']->UpdateStatus($draft);
71
72
      //If we know we're moving from undrafted to in progress, perform the necessary setup steps:
73
      if ($draft->draft_status != $old_status && $draft->draft_status == "in_progress") {
74
        //Delete all trades
75
        $this->app['phpdraft.TradeRepository']->DeleteAllTrades($draft->draft_id);
76
        //Delete all picks
77
        $this->app['phpdraft.PickRepository']->DeleteAllPicks($draft->draft_id);
78
        //Setup new picks
79
        $managers = $this->app['phpdraft.ManagerRepository']->GetManagersByDraftOrder($draft->draft_id);
80
        $descending_managers = $this->app['phpdraft.ManagerRepository']->GetManagersByDraftOrder($draft->draft_id, true);
81
        $this->app['phpdraft.PickRepository']->SetupPicks($draft, $managers, $descending_managers);
82
        //Set draft to in progress
83
        $this->app['phpdraft.DraftRepository']->SetDraftInProgress($draft);
84
      }
85
86
      $response->success = true;
87
      $response->draft = $draft;
0 ignored issues
show
Bug introduced by
The property draft does not seem to exist on PhpDraft\Domain\Models\PhpDraftResponse.
Loading history...
88
    } catch (\Exception $e) {
89
      $response->success = false;
90
      $response->errors = array($e->getMessage());
91
    }
92
93
    return $response;
94
  }
95
96
  public function DeleteDraft(Draft $draft) {
97
    $response = new PhpDraftResponse();
98
99
    try {
100
      //Delete all trades
101
      $this->app['phpdraft.TradeRepository']->DeleteAllTrades($draft->draft_id);
102
      //Delete all depth chart positions
103
      $this->app['phpdraft.DepthChartPositionRepository']->DeleteAllDepthChartPositions($draft->draft_id);
104
      //Delete all picks
105
      $this->app['phpdraft.PickRepository']->DeleteAllPicks($draft->draft_id);
106
      //Delete all managers
107
      $this->app['phpdraft.ManagerRepository']->DeleteAllManagers($draft->draft_id);
108
      //Delete all round timers
109
      $this->app['phpdraft.RoundTimeRepository']->DeleteAll($draft->draft_id);
110
      //Delete the draft
111
      $this->app['phpdraft.DraftRepository']->DeleteDraft($draft->draft_id);
112
113
      $response->success = true;
114
    } catch (\Exception $e) {
115
      $response->success = false;
116
      $response->errors = array($e->getMessage());
117
    }
118
119
    return $response;
120
  }
121
122
  public function GetDraftStats($draft_id) {
123
    $response = new PhpDraftResponse();
124
125
    try {
126
      $response->draft_statistics = $this->app['phpdraft.DraftStatsRepository']->LoadDraftStats($draft_id);
0 ignored issues
show
Bug introduced by
The property draft_statistics does not seem to exist on PhpDraft\Domain\Models\PhpDraftResponse.
Loading history...
127
      $response->success = true;
128
    } catch (\Exception $e) {
129
      $message = $e->getMessage();
130
      $response->success = false;
131
      $response->errors[] = $message;
132
    }
133
134
    return $response;
135
  }
136
137
  public function GetDraftStatusDisplay(Draft $draft) {
138
    $statuses = $this->app['phpdraft.DraftDataRepository']->GetStatuses();
139
140
    return $statuses[$draft->draft_status];
141
  }
142
143
  public function DraftSettingUp(Draft $draft) {
144
    return $draft->draft_status == "undrafted";
145
  }
146
147
  public function DraftInProgress(Draft $draft) {
148
    return $draft->draft_status == "in_progress";
149
  }
150
151
  public function DraftComplete(Draft $draft) {
152
    return $draft->draft_status == "complete";
153
  }
154
}
155