Passed
Push — master ( 39dd2f...8ee4e9 )
by Rafal
03:50
created

LiveGame   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 48
rs 10
c 0
b 0
f 0
wmc 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B updateLiveGames() 0 26 9
A __construct() 0 4 1
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\GameCore\Persistence\Entity\Game;
9
use Doctrine\ORM\EntityManagerInterface;
10
11
class LiveGame implements LiveGameInterface
12
{
13
    /**
14
     * @var EntityManagerInterface
15
     */
16
    private $entityManager;
17
18
    /**
19
     * @var ClientInterface
20
     */
21
    private $client;
22
23
    /**
24
     * @param EntityManagerInterface $entityManager
25
     * @param ClientInterface $client
26
     */
27
    public function __construct(EntityManagerInterface $entityManager, ClientInterface $client)
28
    {
29
        $this->entityManager = $entityManager;
30
        $this->client = $client;
31
    }
32
33
    public function updateLiveGames() : void
34
    {
35
        $activeGamesGames = $this->entityManager
36
            ->getRepository(Game::class)
37
            ->findActiveGames();
38
39
        if (!empty($activeGamesGames)) {
40
            $changeEntity = false;
41
            $games = $this->client->getGames();
42
            foreach ($activeGamesGames as $activeGamesGame) {
43
                foreach ($games as $game) {
44
                    $findTeamFirst = $activeGamesGame->getTeamFirst()->getName() === $game->getFirstTeamName();
45
                    $findTeamSecond = $activeGamesGame->getTeamSecond()->getName() === $game->getSecondTeamName();
46
                    $checkTeamFirstResult = (int)$activeGamesGame->getFirstTeamResult() !== $game->getFirstTeamResult();
47
                    $checkTeamSecondResult = (int)$activeGamesGame->getSecondTeamResult() !== $game->getSecondTeamResult();
48
                    if ($findTeamFirst && $findTeamSecond && $checkTeamFirstResult && $checkTeamSecondResult) {
49
                        $changeEntity = true;
50
                        $activeGamesGame->setFirstTeamResult($game->getFirstTeamResult());
51
                        $activeGamesGame->setSecondTeamResult($game->getSecondTeamResult());
52
                        $this->entityManager->persist($activeGamesGame);
53
                    }
54
                }
55
            }
56
57
            if ($changeEntity === true) {
0 ignored issues
show
introduced by
The condition $changeEntity === true is always false.
Loading history...
58
                $this->entityManager->flush($activeGamesGame);
59
            }
60
        }
61
    }
62
63
64
}