Passed
Push — master ( 4d14cc...cd8bbe )
by Rafal
02:57
created

LiveGame::checkForNewResult()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 5
nop 2
dl 0
loc 7
rs 10
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 = 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) {
0 ignored issues
show
introduced by
The condition $changeEntity === true is always false.
Loading history...
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
}