1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace App\GameApi\Business\ApiFootballData\Import; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use App\GameApi\Business\ApiFootballData\ClientInterface; |
8
|
|
|
use App\GameCore\Persistence\Entity\Game as GameEntity; |
9
|
|
|
use App\GameCore\Persistence\Entity\Team as TeamEntity; |
10
|
|
|
use Doctrine\ORM\EntityManager; |
11
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
12
|
|
|
|
13
|
|
|
class Game implements GameInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var ClientInterface |
17
|
|
|
*/ |
18
|
|
|
private $client; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var EntityManager |
22
|
|
|
*/ |
23
|
|
|
private $entityManager; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param ClientInterface $client |
27
|
|
|
* @param EntityManager $entityManager |
28
|
|
|
*/ |
29
|
|
|
public function __construct(ClientInterface $client, EntityManagerInterface $entityManager) |
30
|
|
|
{ |
31
|
|
|
$this->client = $client; |
32
|
|
|
$this->entityManager = $entityManager; |
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
public function import() |
37
|
|
|
{ |
38
|
|
|
$games = $this->client->getGames(); |
|
|
|
|
39
|
|
|
|
40
|
|
|
$teamName2Entity = $this->getTeamsEntities(); |
41
|
|
|
$gameRepository = $this->entityManager->getRepository(GameEntity::class); |
42
|
|
|
foreach ($games['fixtures'] as $game) { |
43
|
|
|
if (!empty($game['homeTeamName']) && !empty($game['awayTeamName'])) { |
44
|
|
|
$dateTime = new \DateTime($game['date'], new \DateTimeZone('UTC')); |
45
|
|
|
$dateTime->setTimezone(new \DateTimeZone('Europe/Berlin')); |
46
|
|
|
$gameEntity = $gameRepository->findOneBy([ |
47
|
|
|
'teamFirst' => $teamName2Entity[$game['homeTeamName']], |
48
|
|
|
'teamSecond' => $teamName2Entity[$game['awayTeamName']] |
49
|
|
|
]); |
50
|
|
|
|
51
|
|
|
if (!$gameEntity instanceof GameEntity) { |
52
|
|
|
$gameEntity = new GameEntity(); |
53
|
|
|
} |
54
|
|
|
$gameEntity->setDate($dateTime); |
55
|
|
|
$gameEntity->setTeamFirst($teamName2Entity[$game['homeTeamName']]); |
56
|
|
|
$gameEntity->setTeamSecond($teamName2Entity[$game['awayTeamName']]); |
57
|
|
|
$gameEntity->setFirstTeamResult($game['result']['goalsHomeTeam']); |
58
|
|
|
$gameEntity->setSecondTeamResult($game['result']['goalsAwayTeam']); |
59
|
|
|
$this->entityManager->persist($gameEntity); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
$this->entityManager->flush(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return TeamEntity[] |
67
|
|
|
*/ |
68
|
|
|
private function getTeamsEntities(): array |
69
|
|
|
{ |
70
|
|
|
$teamRepository = $this->entityManager->getRepository(TeamEntity::class); |
71
|
|
|
$teamEntitys = $teamRepository->findAll(); |
72
|
|
|
$teamName2Entity = []; |
73
|
|
|
foreach ($teamEntitys as $entity) { |
74
|
|
|
$teamName2Entity[$entity->getName()] = $entity; |
75
|
|
|
} |
76
|
|
|
return $teamName2Entity; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
} |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.