Created
October 4, 2018 10:01
-
-
Save mishazapl/af05712c67ddcea757e61ec7bc0386a8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Created by PhpStorm. | |
* User: mihail | |
* Date: 08.07.18 | |
* Time: 18:19 | |
*/ | |
namespace App\Services\Matches; | |
use App\Match; | |
use Illuminate\Support\Facades\DB; | |
class ChangeStatus extends StatusMatches | |
{ | |
protected $score; | |
public function __construct(Match $match, $status = null, array $score = null) | |
{ | |
parent::__construct($match, $status); | |
$this->score = $score; | |
} | |
protected function actionMatch(): void | |
{ | |
$this->updateMatchStatus(); | |
if ($this->isNullScore()) return; | |
$this->updateMatchesScore(); | |
} | |
private function updateMatchStatus(): void | |
{ | |
$this->match->fill(['status' => $this->status])->update(); | |
} | |
private function isNullScore(): bool | |
{ | |
return is_null($this->score); | |
} | |
private function updateMatchesScore() | |
{ | |
$score = implode(',', $this->score); | |
DB::update("UPDATE matches SET score = ARRAY[$score] where id = ?", [$this->match->id]); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Created by PhpStorm. | |
* User: mihail | |
* Date: 08.07.18 | |
* Time: 18:22 | |
*/ | |
namespace App\Services\Matches; | |
class DeleteMatch extends StatusMatches | |
{ | |
protected function actionMatch(): void | |
{ | |
$this->match->delete(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Created by PhpStorm. | |
* User: mihail | |
* Date: 22.07.18 | |
* Time: 21:48 | |
*/ | |
namespace App\Services\Matches\Site; | |
use App\Bet; | |
use App\Match; | |
class MatchesSimpleFactory | |
{ | |
/** | |
* @return mixed | |
*/ | |
public function expects() | |
{ | |
return Match::with(['teamOne', 'teamTwo', 'tourney', 'playersBets' => function ($query) { | |
$query->where('user_id', (auth()->guest()) ? null : auth()->user()->id) | |
->where('status', Bet::DONE); | |
}]) | |
->expects() | |
->sumSelect() | |
->sumOneTeam() | |
->sumTwoTeam() | |
->latest('start'); | |
} | |
/** | |
* @return mixed | |
*/ | |
public function currents() | |
{ | |
return Match::with(['teamOne', 'teamTwo', 'tourney', 'playersBets' => function ($query) { | |
$query->where('user_id', (auth()->guest()) ? null : auth()->user()->id) | |
->where('status', Bet::DONE); | |
}]) | |
->latest('start') | |
->sumSelect() | |
->sumOneTeam() | |
->sumTwoTeam() | |
->currents(); | |
} | |
/** | |
* @return mixed | |
*/ | |
public function afters() | |
{ | |
return Match::with('teamOne', 'teamTwo', 'tourney') | |
->latest('start') | |
->afters(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Created by PhpStorm. | |
* User: mihail | |
* Date: 08.07.18 | |
* Time: 17:39 | |
*/ | |
namespace App\Services\Matches; | |
use App\Bet; | |
use App\BetLog; | |
use App\Events\CreateLog; | |
use App\Match; | |
use App\Notifications\BetNotify; | |
use App\Services\Bets\Notification; | |
use Illuminate\Support\Facades\DB; | |
use Illuminate\Support\Facades\Log; | |
abstract class StatusMatches | |
{ | |
public $match; | |
protected $status; | |
/** | |
* StatusMatches constructor. | |
* @param Match $match | |
* @param null $status | |
*/ | |
public function __construct(Match $match, $status = null) | |
{ | |
$this->match = $match; | |
$this->status = $status; | |
} | |
final public function cancelBets() | |
{ | |
$this->match->update(['blocked' => true]); | |
DB::beginTransaction(); | |
$this->eachPlayersBet($this->getBets()); | |
$this->actionMatch(); | |
DB::commit(); | |
$this->match->update(['blocked' => false]); | |
} | |
abstract protected function actionMatch(): void; | |
/** | |
* @return Bet[]|\Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection | |
*/ | |
protected function getBets() | |
{ | |
return Bet::with('user') | |
->where('match_id', $this->match->id) | |
->where('status', Bet::DONE) | |
->get(); | |
} | |
/** | |
* @param $playersBets | |
*/ | |
protected function eachPlayersBet($playersBets) | |
{ | |
foreach ($playersBets as $playersBet) { | |
$playersBet = $this->updateUserBet($playersBet); | |
$this->sendNotifyPlayerBet($playersBet); | |
$this->updateTransactionBet($playersBet); | |
event(new CreateLog($playersBet->getAttributes())); | |
} | |
} | |
/** | |
* @param $playersBet | |
* @return mixed | |
*/ | |
protected function updateUserBet($playersBet) | |
{ | |
$playersBet->user->fill(['balance' => $playersBet->user->balance + $playersBet->sum]) | |
->update(); | |
return $playersBet; | |
} | |
/** | |
* @param $playersBet | |
*/ | |
protected function sendNotifyPlayerBet($playersBet) | |
{ | |
Notification::sendNotify($playersBet->user, " | |
Ставка в матче | |
{$this->match->teamOne->title} vs {$this->match->teamTwo->title} была отменена."); | |
} | |
/** | |
* @param $playersBet | |
*/ | |
protected function updateTransactionBet($playersBet) | |
{ | |
$playersBet->fill([ | |
'status' => Bet::CANCELLED, | |
'result' => 0 | |
])->update(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Created by PhpStorm. | |
* User: mihail | |
* Date: 13.07.18 | |
* Time: 14:33 | |
*/ | |
namespace App\Services\Matches\Strategy; | |
use App\Match; | |
use Illuminate\Http\Request; | |
class Context | |
{ | |
/** | |
* @param Match $match | |
* @param Request $request | |
* @param int $status | |
* @param int $changeStatus | |
* @return null | |
*/ | |
public function executeStrategy(Match $match, Request $request, int $status, int $changeStatus) | |
{ | |
if ($status == $changeStatus) { | |
$match->update(); | |
return null; | |
} | |
return $this->createStrategy($status)->statusCombination($match, $request, $status, $changeStatus); | |
} | |
/** | |
* @return array | |
*/ | |
private function getStrategy(): array | |
{ | |
return [ | |
Match::CURRENT => Current::class, | |
Match::SHIFT => Shift::class, | |
Match::UPCOMING => Upcoming::class | |
]; | |
} | |
/** | |
* @param $status | |
* @return StatusStrategy | |
*/ | |
private function createStrategy($status): StatusStrategy | |
{ | |
$strategy = $this->getStrategy(); | |
return new $strategy[$status](); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Created by PhpStorm. | |
* User: mihail | |
* Date: 13.07.18 | |
* Time: 14:38 | |
*/ | |
namespace App\Services\Matches\Strategy; | |
use App\Bet; | |
use App\Jobs\ActionMatchJob; | |
use App\Jobs\WinningMatchJob; | |
use App\Match; | |
use App\Services\Matches\ChangeStatus; | |
use Illuminate\Http\Request; | |
use Illuminate\Support\Facades\Validator; | |
class Current implements StatusStrategy | |
{ | |
/** | |
* @param Match $match | |
* @param Request $request | |
* @param int $status | |
* @param int $changeStatus | |
* @return \Illuminate\Contracts\Validation\Validator | |
*/ | |
public function statusCombination(Match $match, Request $request, int $status, int $changeStatus) | |
{ | |
if ($this->isCurrentAndChangePast($status, $changeStatus)) { | |
$validator = $this->validator($request); | |
if ($validator->fails()) | |
return $validator; | |
$this->executeDispatch($match, $request, $changeStatus); | |
} | |
} | |
/** | |
* @param $status | |
* @param $changeStatus | |
* @return bool | |
*/ | |
private function isCurrentAndChangePast($status, $changeStatus): bool | |
{ | |
return $status == Match::CURRENT && $changeStatus == Match::PAST; | |
} | |
/** | |
* @param Request $request | |
* @return \Illuminate\Contracts\Validation\Validator | |
*/ | |
private function validator(Request $request) | |
{ | |
$validator = Validator::make($request->all(), [ | |
'score.*' => 'required|integer' | |
]); | |
return $validator; | |
} | |
/** | |
* @param $match | |
* @return array | |
*/ | |
protected function getBets($match): array | |
{ | |
return [ | |
Bet::where('match_id', $match->id) | |
->where('team_id', $match->team_id_1) | |
->where('status', Bet::DONE) | |
->first(), | |
Bet::where('match_id', $match->id) | |
->where('team_id', $match->team_id_2) | |
->where('status', Bet::DONE) | |
->first() | |
]; | |
} | |
/** | |
* @param $match | |
* @return bool | |
*/ | |
private function isBetsEmptyAll($match): bool | |
{ | |
$bets = $this->getBets($match); | |
return empty($bets[0]) || empty($bets[1]); | |
} | |
/** | |
* @param Match $match | |
* @param Request $request | |
* @param int $changeStatus | |
*/ | |
private function executeDispatch(Match $match, Request $request, int $changeStatus) | |
{ | |
if ($this->isBetsEmptyAll($match)) | |
dispatch(new ActionMatchJob(new ChangeStatus($match, $changeStatus, $request->input('score')))); | |
else | |
dispatch(new WinningMatchJob($match, $request->input('score'))); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Created by PhpStorm. | |
* User: mihail | |
* Date: 13.07.18 | |
* Time: 14:38 | |
*/ | |
namespace App\Services\Matches\Strategy; | |
use App\Match; | |
use Illuminate\Http\Request; | |
class Shift implements StatusStrategy | |
{ | |
/** | |
* @param Match $match | |
* @param Request $request | |
* @param int $status | |
* @param int $changeStatus | |
*/ | |
public function statusCombination(Match $match, Request $request, int $status, int $changeStatus) | |
{ | |
if ($this->isShiftAndChangeUpcoming($status, $changeStatus)) | |
$match->fill(['status' => Match::UPCOMING,])->update(); | |
elseif ($this->isShiftAndChangeCancel($status, $changeStatus)) | |
$match->fill(['status' => Match::CANCEL])->update(); | |
} | |
/** | |
* @param $status | |
* @param $changeStatus | |
* @return bool | |
*/ | |
private function isShiftAndChangeUpcoming($status, $changeStatus): bool | |
{ | |
return $status == Match::SHIFT && $changeStatus == Match::UPCOMING; | |
} | |
/** | |
* @param $status | |
* @param $changeStatus | |
* @return bool | |
*/ | |
private function isShiftAndChangeCancel($status, $changeStatus): bool | |
{ | |
return $status == Match::SHIFT && $changeStatus == Match::CANCEL; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Created by PhpStorm. | |
* User: mihail | |
* Date: 13.07.18 | |
* Time: 14:33 | |
*/ | |
namespace App\Services\Matches\Strategy; | |
use App\Match; | |
use Illuminate\Http\Request; | |
interface StatusStrategy | |
{ | |
/** | |
* @param Match $match | |
* @param Request $request | |
* @param int $status | |
* @param int $changeStatus | |
* @return mixed | |
*/ | |
public function statusCombination(Match $match, Request $request, int $status, int $changeStatus); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Created by PhpStorm. | |
* User: mihail | |
* Date: 13.07.18 | |
* Time: 14:36 | |
*/ | |
namespace App\Services\Matches\Strategy; | |
use App\Jobs\ActionMatchJob; | |
use App\Jobs\CurrentChangeJob; | |
use App\Match; | |
use App\Services\Matches\ChangeStatus; | |
use Illuminate\Http\Request; | |
class Upcoming implements StatusStrategy | |
{ | |
/** | |
* @param Match $match | |
* @param Request $request | |
* @param int $status | |
* @param int $changeStatus | |
*/ | |
public function statusCombination(Match $match, Request $request, int $status, int $changeStatus) | |
{ | |
if ($this->isUpcomingAndChangeShift($status, $changeStatus) || $this->isChangeStatusCancel($changeStatus)) | |
dispatch(new ActionMatchJob(new ChangeStatus($match, $changeStatus))); | |
elseif ($this->isUpcomingAndChangeCurrent($status, $changeStatus)) | |
dispatch(new CurrentChangeJob($match->id)); | |
} | |
/** | |
* @param $status | |
* @param $changeStatus | |
* @return bool | |
*/ | |
private function isUpcomingAndChangeShift($status, $changeStatus): bool | |
{ | |
return $status == Match::UPCOMING && $changeStatus == Match::SHIFT; | |
} | |
/** | |
* @param $changeStatus | |
* @return bool | |
*/ | |
private function isChangeStatusCancel($changeStatus): bool | |
{ | |
return $changeStatus == Match::CANCEL; | |
} | |
/** | |
* @param $status | |
* @param $changeStatus | |
* @return bool | |
*/ | |
private function isUpcomingAndChangeCurrent($status, $changeStatus): bool | |
{ | |
return $status == Match::UPCOMING && $changeStatus == Match::CURRENT; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment