Passed
Push — master ( d4c951...fa97fb )
by Rafal
03:32
created

LiveGame::updateLiveGames()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 0
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
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 = $this->checkGames($activeGamesGames);
42
43
            if ($changeEntity === true) {
44
                $this->entityManager->flush();
45
            }
46
        }
47
    }
48
49
    /**
50
     * @param array $activeGamesGames
51
     * @return bool
52
     */
53
    private function checkGames(array $activeGamesGames): bool
54
    {
55
        $changeEntity = false;
56
        $games = $this->client->getGames();
57
        foreach ($activeGamesGames as $activeGamesGame) {
58
            foreach ($games as $game) {
59
                if ($this->checkForNewResult($activeGamesGame, $game)) {
60
                    $changeEntity = true;
61
                    $activeGamesGame->setFirstTeamResult($game->getFirstTeamResult());
62
                    $activeGamesGame->setSecondTeamResult($game->getSecondTeamResult());
63
                    $this->entityManager->persist($activeGamesGame);
64
                }
65
            }
66
        }
67
        return $changeEntity;
68
    }
69
70
    /**
71
     * @param Game $activeGamesGame
72
     * @param GameResult $game
73
     * @return bool
74
     */
75
    private function checkForNewResult(Game $activeGamesGame, GameResult $game): bool
76
    {
77
        $findTeamFirst = $activeGamesGame->getTeamFirst()->getName() === $game->getFirstTeamName();
78
        $findTeamSecond = $activeGamesGame->getTeamSecond()->getName() === $game->getSecondTeamName();
79
        $checkTeamFirstResult = (int)$activeGamesGame->getFirstTeamResult() !== $game->getFirstTeamResult();
80
        $checkTeamSecondResult = (int)$activeGamesGame->getSecondTeamResult() !== $game->getSecondTeamResult();
81
        return $findTeamFirst && $findTeamSecond && ($checkTeamFirstResult || $checkTeamSecondResult);
82
    }
83
84
}