Passed
Push — develop ( aa9ac7...096b8e )
by Nikita
05:39
created

GameRepository   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
dl 0
loc 44
ccs 0
cts 5
cp 0
rs 10
c 1
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A upgradeFromRepo() 0 23 5
A getAll() 0 3 1
A __construct() 0 3 1
1
<?php
2
3
namespace Gameap\Repositories;
4
5
use Gameap\Models\Game;
6
use Gameap\Models\GameMod;
7
use Gameap\Services\GlobalApi;
8
use Symfony\Component\Finder\Glob;
9
10
class GameRepository
11
{
12
    protected $model;
13
14
    public function __construct(Game $game)
15
    {
16
        $this->model = $game;
17
    }
18
19
    /**
20
     * @param int $perPage
21
     * @return mixed
22
     */
23
    public function getAll($perPage = 20)
24
    {
25
        return Game::orderBy('name')->paginate($perPage);
26
    }
27
28
    /**
29
     * Upgrade Games and Game Mods using GameAP API
30
     */
31
    public function upgradeFromRepo()
32
    {
33
        $apiGames = GlobalApi::games();
34
35
        foreach ($apiGames as $gameData) {
36
            $game = Game::firstOrCreate(['code' => $gameData['code']]);
37
38
            $game->fill($gameData);
39
            $game->save();
40
41
            if (!empty($gameData['mods'])) {
42
                foreach ($gameData['mods'] as $gameModData) {
43
                    $gameMod = GameMod::firstOrCreate(['name' => $gameModData['name']]);
44
                    $gameMod->fill($gameModData);
45
46
                    if ($gameMod->isValid()) {
47
                        $gameMod->save();
48
                    }
49
                }
50
            }
51
        }
52
53
        return true;
54
    }
55
}