Completed
Push — master ( 035ed0...4cfde5 )
by Jonas
04:22
created

FetchAllGamesJob   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 4
dl 0
loc 64
ccs 0
cts 34
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B execute() 0 25 3
B newGame() 0 11 7
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of SteamScore.
7
 *
8
 * (c) SteamScore <[email protected]>
9
 *
10
 * This Source Code Form is subject to the terms of the Mozilla Public
11
 * License, v. 2.0. If a copy of the MPL was not distributed with this
12
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
13
 */
14
15
namespace SteamScore\Api\Domain\Jobs;
16
17
use Doctrine\ORM\EntityManagerInterface;
18
use SteamScore\Api\Domain\Entities\Game;
19
use SteamScore\Api\Domain\Interfaces\JobInterface;
20
use SteamScore\Api\Domain\Services\SteamSpyService;
21
22
final class FetchAllGamesJob implements JobInterface
23
{
24
    /**
25
     * @var EntityManagerInterface
26
     */
27
    private $entities;
28
29
    /**
30
     * @var SteamSpyService
31
     */
32
    private $steamSpy;
33
34
    /**
35
     * Constructor.
36
     *
37
     * @param SteamSpyService $steamSpy
38
     */
39
    public function __construct(EntityManagerInterface $entities, SteamSpyService $steamSpy)
40
    {
41
        $this->entities = $entities;
42
        $this->steamSpy = $steamSpy;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function execute(array $args = [])
49
    {
50
        $remoteGames = $this->steamSpy->findAll();
51
        $localGames = $this->entities->getRepository(Game::class)->findAll();
52
        $localGames = array_reduce($localGames, function (array $carry, Game $game) {
53
            $carry[$game->getAppId()] = $game;
54
55
            return $carry;
56
        }, []);
57
58
        foreach ($remoteGames as $remoteGame) {
59
            $game = $localGames[$remoteGame['appid']] ?? $this->newGame($remoteGame);
60
61
            // @todo: Update data on existing games.
62
63
            $this->entities->persist($game);
64
            unset($localGames[$remoteGame['appid']]);
65
        }
66
67
        foreach ($localGames as $localGame) {
68
            $this->entities->remove($localGame);
69
        }
70
71
        $this->entities->flush();
72
    }
73
74
    private function newGame(array $gameData)
75
    {
76
        return new Game(
77
            $gameData['appid'],
78
            $gameData['name'],
79
            ($gameData['developer'] !== '' && $gameData['developer'] !== null) ? $gameData['developer'] : null,
80
            ($gameData['publisher'] !== '' && $gameData['publisher'] !== null) ? $gameData['publisher'] : null,
81
            ($gameData['score_rank'] !== '' && $gameData['score_rank'] !== null) ? $gameData['score_rank'] : null,
82
            $gameData['players_forever']
83
        );
84
    }
85
}
86