Completed
Push — master ( 93d505...55670f )
by
unknown
03:20
created

Game::import()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 4
nop 0
dl 0
loc 27
rs 9.1768
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 EntityManager
22
     */
23
    private $entityManager;
24
25
    /**
26
     * @param ClientInterface $client
27
     * @param EntityManager $entityManager
28
     */
29
    public function __construct(ClientInterface $client, EntityManagerInterface $entityManager)
30
    {
31
        $this->client = $client;
32
        $this->entityManager = $entityManager;
0 ignored issues
show
Documentation Bug introduced by
$entityManager is of type Doctrine\ORM\EntityManagerInterface, but the property $entityManager was declared to be of type Doctrine\ORM\EntityManager. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
33
    }
34
35
36
    public function import()
37
    {
38
        $games = $this->client->getGames();
0 ignored issues
show
Bug introduced by
The method getGames() does not exist on App\GameApi\Business\Api...allData\ClientInterface. Did you maybe mean getTeams()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

38
        /** @scrutinizer ignore-call */ 
39
        $games = $this->client->getGames();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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($game['result']['goalsHomeTeam']);
58
                $gameEntity->setSecondTeamResult($game['result']['goalsAwayTeam']);
59
                $this->entityManager->persist($gameEntity);
60
            }
61
        }
62
        $this->entityManager->flush();
63
    }
64
65
    /**
66
     * @return TeamEntity[]
67
     */
68
    private function getTeamsEntities(): array
69
    {
70
        $teamRepository = $this->entityManager->getRepository(TeamEntity::class);
71
        $teamEntitys = $teamRepository->findAll();
72
        $teamName2Entity = [];
73
        foreach ($teamEntitys as $entity) {
74
            $teamName2Entity[$entity->getName()] = $entity;
75
        }
76
        return $teamName2Entity;
77
    }
78
79
}