Advertisement
geirby

DDD_in_sportsbook

Aug 10th, 2022 (edited)
591
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.13 KB | None | 0 0
  1. <?
  2. new BetItem(
  3.     new Dto(
  4.         $eventShared->getEventType(),
  5.         $eventShared->getId(),
  6.         $marketShared->getId(),
  7.         $outcomeShared->getId(),
  8.         $outcomeShared->getType()
  9.     ),
  10.     $eventShared,
  11.     1.58
  12. )
  13. <implements BetableInterface>
  14.  
  15. public function getEventType(): App\Sportsbook\EventType
  16. {
  17.     return $this->eventType;
  18. }
  19.  
  20.  
  21. Например, при разделении интерфейса для работы с временем соревнования мы можем прослеживать(валидировать) такие связи как время матча внутри сезона.
  22.  
  23.  
  24. ===================== Competitions =======================
  25. //redundant ?
  26. enum CompetitionType
  27. {
  28.     case Race;
  29.     case League;
  30.     case Season;
  31.     case Stage;
  32.     case Event;
  33. }
  34.  
  35. interface Competition
  36. {
  37.     private CompetitionTime $competitionTime
  38.     private CompetitionType $competitionType
  39.        
  40.     public function getType(): CompetitionType;
  41.     public function getTime(): CompetitionTime;
  42. }
  43.    
  44. class Season implement Competition
  45. {      
  46.     $startDate (Y-m-d H:i:s)
  47.     $endDate (Y-m-d H:i:s)
  48.     ($year as additional)
  49.    
  50.     public function __construct(SeasonCompetitionTime $competitionTime)
  51.     {
  52.         $this->competitionTime = $competitionTime;
  53.     }
  54.    
  55.     public function save()
  56.     {
  57.         $this->startDate = $this->competitionTime->getScheduledStart();
  58.     }
  59. }
  60.    
  61. class Event implement Competition
  62. {
  63.     $time (Y-m-d H:i:s)
  64.     public function __construct(EventCompetitionTime $competitionTime)
  65.     {
  66.         $this->competitionTime = $competitionTime;
  67.     }
  68.    
  69.     public function save()
  70.     {
  71.         $this->time = $this->competitionTime->getActualDate();
  72.     }
  73. }
  74.  
  75. class League implement Competition
  76. {
  77.     $startDate (Y-m-d H:i:s)
  78.     $endDate (Y-m-d H:i:s)
  79.  
  80.     public function getStartDate()
  81.     {
  82.         return $this->competitionTime->getActualStart();
  83.     }
  84.    
  85.     public function getWhatDoYouNeedFromTimeAccordingToCompetitionsBussinesslogic();
  86. }  
  87.  
  88. class Stage implement Competition
  89.     $scheduled (Y-m-d H:i:s)
  90.     $scheduledEnd (Y-m-d H:i:s)
  91.    
  92. class Race implement Competition
  93. {
  94.     $startTime (Y-m-d H:i:s) (accuracy to i:s)
  95.     (?)$jumpTime (Y-m-d H:i:s) (accuracy to i:s)
  96. }
  97. ========================Competitions Time========================
  98. /*
  99. Policies
  100. -- must has start
  101. -- might/must be scheduled
  102. -- might/must has finish time (end of match)
  103. -- might/must be sheduled_end (end in future but bets until finishing)
  104. */
  105.  
  106. //implement Policy about Competition date/time
  107. interface CompetitionTime
  108. {  
  109.     private DateTime $actualStart;
  110.     public function getActualStart();  
  111.     public function validate();
  112. }
  113.  
  114. class AbstractCompetitionTime implement CompetitionTime
  115. {
  116.     public function makeExpiredAt(DateTime $neededPeriod): Datetime
  117.     {
  118.         return new DateTime() + $neededPeriod;
  119.     }
  120. }
  121. class SeasonCompetionTime implement CompetitionTime
  122. {
  123.     private DateTime $scheduledStart;
  124.     private DateTime $scheduledFinish;
  125.    
  126.     public function __construct(
  127.         $scheduledStart,
  128.         $scheduledStart
  129.     ){     
  130.         $this->scheduledStart = $scheduledStart;
  131.         $this->scheduledFinish = $scheduledFinish;
  132.     }
  133.    
  134.     public function makeExpiredAt(DateTime $neededPeriod): DateTime
  135.     {
  136.         return $this->scheduledStart + $neededPeriod;
  137.     }
  138.    
  139.     public function validate($scheduledStart, $scheduledStart)
  140.     {
  141.         if($scheduledStart < $scheduledFinish) {
  142.             throw new SeasonCompetionTimeValidateException();
  143.         }
  144.     }
  145. }
  146.    
  147. class EventCompetionTime implement CompetitionTime
  148. {
  149.     public function __construct(
  150.         $actualStart,
  151.         $seasonId = null,      
  152.     ){
  153.         $this->validate($actualStart, $seasonId);
  154.         $this->actualStart = $actualStart;
  155.     }
  156.    
  157.     public function validate($actualStart, $seasonId = null)
  158.     {
  159.         if ($seasonId) {
  160.             $this->validateBySeasonPeriod($actualStart, $seasonId);
  161.         }
  162.     }
  163.    
  164.     private function validateBySeasonPeriod($actualStart, $seasonId)
  165.     {
  166.         $season = Season::findFirst($seasonId);
  167.         if(
  168.             $actualStart < $season->competitionTime->getActualStart()
  169.             || $actualStart > $season->competitionTime->getScheduledFinish();
  170.         ) {
  171.             throw new EventCompetionTimeValidateException();
  172.         }
  173.     }
  174.    
  175.     public function makeExpiredAt(Date $neededPeriod): DateTime
  176.     {
  177.         return $this->actualStart + $neededPeriod;
  178.     }
  179. }
  180.  
  181. class CompetitionTimeService
  182.     public function doSomethingWithCompetitionTime();
  183.     public function getYear(CompetitionTime $time);
  184.  
  185.  
  186. /*
  187. decomposition by classes has to force developer creates new instance of Competition with time(period) by CompetitionType
  188. */
  189.  
  190. $createLeague = new League();
  191. $seasonCompetitionTime = new SeasonCompetitionTime($startTimeFromImport, $endTimeFromImport);
  192. $createdSeason = new Season($seasonCompetitionTime);
  193. $eventCompetitionTime = new EvenCompetitionTime($timeFromImport, $createdSeason->getId());
  194. $createdEvent = new Event($eventCompetitionTime);
  195.  
  196. $competitions = [
  197.     $createLeague,
  198.     $createdSeason,
  199.     $createdEvent
  200. ];
  201.  
  202. //somewhere in service
  203. foreach($competitions as $competition) {
  204.     echo $competition->competitionTime->getExpiredAt($period2week);
  205. }
  206. //$expiredAt = $createdEvent->competitionTime->getExpiredAt($period2week);
  207. //$expiredAt = $createdSeason->competitionTime->getExpiredAt($period1week);
  208.  
  209.  
  210.  
  211.  
  212. $league = $repository->findFirst($leagueId);
  213. $league->getTime();
  214.  
  215. $league->save();
  216.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement