Game   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 42
dl 0
loc 102
rs 10
c 0
b 0
f 0
wmc 14

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getAwayGoals() 0 13 3
A import() 0 27 5
A getTeamsEntities() 0 9 2
A getHomeGoals() 0 13 3
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 EntityManagerInterface
22
     */
23
    private $entityManager;
24
25
    /**
26
     * @param ClientInterface $client
27
     * @param EntityManagerInterface $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(): void
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($this->getHomeGoals($game['result']));
58
                $gameEntity->setSecondTeamResult($this->getAwayGoals($game['result']));
59
                $this->entityManager->persist($gameEntity);
60
            }
61
        }
62
        $this->entityManager->flush();
63
    }
64
65
    /**
66
     * @param array $result
67
     * @return int|null
68
     */
69
    private function getHomeGoals(array $result): ?int
70
    {
71
        $goals = $result['goalsHomeTeam'];
72
73
        if (isset($result['extraTime'])) {
74
            $goals = $result['extraTime']['goalsHomeTeam'];
75
        }
76
77
        if (isset($result['penaltyShootout'])) {
78
            $goals += $result['penaltyShootout']['goalsHomeTeam'];
79
        }
80
81
        return $goals;
82
    }
83
84
    /**
85
     * @param array $result
86
     * @return int|null
87
     */
88
    private function getAwayGoals(array $result): ?int
89
    {
90
        $goals = $result['goalsAwayTeam'];
91
92
        if (isset($result['extraTime'])) {
93
            $goals = $result['extraTime']['goalsAwayTeam'];
94
        }
95
96
        if (isset($result['penaltyShootout'])) {
97
            $goals += $result['penaltyShootout']['goalsAwayTeam'];
98
        }
99
100
        return $goals;
101
    }
102
103
    /**
104
     * @return TeamEntity[]
105
     */
106
    private function getTeamsEntities(): array
107
    {
108
        $teamRepository = $this->entityManager->getRepository(TeamEntity::class);
109
        $teamEntitys = $teamRepository->findAll();
110
        $teamName2Entity = [];
111
        foreach ($teamEntitys as $entity) {
112
            $teamName2Entity[$entity->getName()] = $entity;
113
        }
114
        return $teamName2Entity;
115
    }
116
117
}
118