1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace App\GameApi\Business\WorldCupSfgIo\Import; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use App\GameApi\Business\WorldCupSfgIo\Client\ClientInterface; |
8
|
|
|
use App\GameApi\Persistence\DataProvider\GameResult; |
9
|
|
|
use App\GameCore\Persistence\Entity\Game; |
10
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
11
|
|
|
|
12
|
|
|
class LiveGame implements LiveGameInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var EntityManagerInterface |
16
|
|
|
*/ |
17
|
|
|
private $entityManager; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var ClientInterface |
21
|
|
|
*/ |
22
|
|
|
private $client; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param EntityManagerInterface $entityManager |
26
|
|
|
* @param ClientInterface $client |
27
|
|
|
*/ |
28
|
|
|
public function __construct(EntityManagerInterface $entityManager, ClientInterface $client) |
29
|
|
|
{ |
30
|
|
|
$this->entityManager = $entityManager; |
31
|
|
|
$this->client = $client; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function updateLiveGames(): void |
35
|
|
|
{ |
36
|
|
|
$activeGamesGames = $this->entityManager |
37
|
|
|
->getRepository(Game::class) |
38
|
|
|
->findActiveGames(); |
39
|
|
|
|
40
|
|
|
if (!empty($activeGamesGames)) { |
41
|
|
|
$changeEntity = false; |
42
|
|
|
$games = $this->client->getGames(); |
43
|
|
|
foreach ($activeGamesGames as $activeGamesGame) { |
44
|
|
|
foreach ($games as $game) { |
45
|
|
|
if ($this->checkForNewResult($activeGamesGame, $game)) { |
46
|
|
|
$changeEntity = true; |
47
|
|
|
$activeGamesGame->setFirstTeamResult($game->getFirstTeamResult()); |
48
|
|
|
$activeGamesGame->setSecondTeamResult($game->getSecondTeamResult()); |
49
|
|
|
$this->entityManager->persist($activeGamesGame); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if ($changeEntity === true) { |
|
|
|
|
55
|
|
|
$this->entityManager->flush(); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param Game $activeGamesGame |
62
|
|
|
* @param GameResult $game |
63
|
|
|
* @return bool |
64
|
|
|
*/ |
65
|
|
|
private function checkForNewResult(Game $activeGamesGame, GameResult $game): bool |
66
|
|
|
{ |
67
|
|
|
$findTeamFirst = $activeGamesGame->getTeamFirst()->getName() === $game->getFirstTeamName(); |
68
|
|
|
$findTeamSecond = $activeGamesGame->getTeamSecond()->getName() === $game->getSecondTeamName(); |
69
|
|
|
$checkTeamFirstResult = (int)$activeGamesGame->getFirstTeamResult() !== $game->getFirstTeamResult(); |
70
|
|
|
$checkTeamSecondResult = (int)$activeGamesGame->getSecondTeamResult() !== $game->getSecondTeamResult(); |
71
|
|
|
return $findTeamFirst && $findTeamSecond && ($checkTeamFirstResult || $checkTeamSecondResult); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
} |