|
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\Entities\Trade; |
|
8
|
|
|
use PhpDraft\Domain\Entities\TradeAsset; |
|
9
|
|
|
use PhpDraft\Domain\Models\PhpDraftResponse; |
|
10
|
|
|
|
|
11
|
|
|
class TradeService { |
|
12
|
|
|
private $app; |
|
13
|
|
|
|
|
14
|
|
|
public function __construct(Application $app) { |
|
15
|
|
|
$this->app = $app; |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
public function GetManagerAssets(Draft $draft, $manager_id) { |
|
19
|
|
|
$response = new PhpDraftResponse(); |
|
20
|
|
|
|
|
21
|
|
|
try { |
|
22
|
|
|
$assets = $this->app['phpdraft.PickRepository']->LoadManagerPicks($manager_id, $draft, false); |
|
23
|
|
|
|
|
24
|
|
|
$response->success = true; |
|
25
|
|
|
$response->manager_id = $manager_id; |
|
|
|
|
|
|
26
|
|
|
$response->assets = $assets; |
|
|
|
|
|
|
27
|
|
|
} catch (\Exception $e) { |
|
28
|
|
|
$response->success = false; |
|
29
|
|
|
$response->errors = array($e->getMessage()); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
return $response; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function EnterTrade(Draft $draft, Trade $trade) { |
|
36
|
|
|
$response = new PhpDraftResponse(); |
|
37
|
|
|
|
|
38
|
|
|
try { |
|
39
|
|
|
//Exchange the picks in the database |
|
40
|
|
|
$trade = $this->_ExchangeAssets($draft, $trade); |
|
41
|
|
|
//Save the trade itself |
|
42
|
|
|
$trade = $this->app['phpdraft.TradeRepository']->SaveTrade($trade); |
|
43
|
|
|
//Save all assets |
|
44
|
|
|
$trade = $this->app['phpdraft.TradeRepository']->SaveAssets($trade); |
|
45
|
|
|
//Update the draft's counter |
|
46
|
|
|
$draft->draft_counter = $this->app['phpdraft.DraftRepository']->IncrementDraftCounter($draft); |
|
47
|
|
|
|
|
48
|
|
|
$response->success = true; |
|
49
|
|
|
$response->trade = $trade; |
|
|
|
|
|
|
50
|
|
|
} catch (\Exception $e) { |
|
51
|
|
|
$response->success = false; |
|
52
|
|
|
$response->errors = array($e->getMessage()); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
return $response; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Perform the core swap of ownership of each pick (player) in the database |
|
60
|
|
|
* @param Trade $trade |
|
61
|
|
|
* @return Trade |
|
62
|
|
|
* @throws Exception |
|
63
|
|
|
*/ |
|
64
|
|
|
private function _ExchangeAssets(Draft $draft, Trade $trade) { |
|
65
|
|
|
//Update the draft counter - can be the same for all assets, doesnt matter. Trade = 1 action |
|
66
|
|
|
$new_counter_value = $draft->draft_counter + 1; |
|
67
|
|
|
|
|
68
|
|
|
foreach ($trade->trade_assets as $asset) { |
|
69
|
|
|
$asset->player->manager_id = $asset->newmanager->manager_id; |
|
70
|
|
|
$asset->player->player_counter = $new_counter_value; |
|
71
|
|
|
|
|
72
|
|
|
$this->app['phpdraft.PickRepository']->UpdatePick($asset->player); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return $trade; |
|
76
|
|
|
} |
|
77
|
|
|
} |