Passed
Push — master ( cd8bbe...d4c951 )
by Rafal
03:21
created

Game::getAwayGoals()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
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
        if (isset($result['penaltyShootout'])) {
73
            $goals += $result['penaltyShootout']['goalsHomeTeam'];
74
        }
75
76
        return $goals;
77
    }
78
79
    /**
80
     * @param array $result
81
     * @return int|null
82
     */
83
    private function getAwayGoals(array $result) : ?int
84
    {
85
        $goals = $result['goalsAwayTeam'];
86
        if (isset($result['penaltyShootout'])) {
87
            $goals += $result['penaltyShootout']['goalsAwayTeam'];
88
        }
89
90
        return $goals;
91
    }
92
93
    /**
94
     * @return TeamEntity[]
95
     */
96
    private function getTeamsEntities(): array
97
    {
98
        $teamRepository = $this->entityManager->getRepository(TeamEntity::class);
99
        $teamEntitys = $teamRepository->findAll();
100
        $teamName2Entity = [];
101
        foreach ($teamEntitys as $entity) {
102
            $teamName2Entity[$entity->getName()] = $entity;
103
        }
104
        return $teamName2Entity;
105
    }
106
107
}
108