Completed
Push — master ( fa81e5...c1047a )
by De Cramer
04:54 queued 01:25
created

GameTitleFetchPass::fetchDataFromRemote()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 2
1
<?php
2
3
4
namespace eXpansion\Framework\Core\DependencyInjection\Compiler;
5
6
use League\Flysystem\Adapter\Local;
7
use League\Flysystem\Filesystem;
8
use oliverde8\AsynchronousJobs\Job\Curl;
9
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
10
use Symfony\Component\DependencyInjection\ContainerBuilder;
11
12
13
/**
14
 * Class GameTitleFetchPass
15
 *
16
 * @package eXpansion\Framework\Core\DependencyInjection\Compiler;
17
 * @author  oliver de Cramer <[email protected]>
18
 */
19
class GameTitleFetchPass implements CompilerPassInterface
20
{
21
    const MAPPINGS_FILE = "title_game_mappings.json";
22
23
    /**
24
     * You can modify the container here before it is dumped to PHP code.
25
     *
26
     * @param ContainerBuilder $container
27
     */
28
    public function process(ContainerBuilder $container)
29
    {
30
        $fileSytem = new Filesystem(new Local(realpath($container->getParameter('kernel.root_dir') . "/../var")));
31
        $fileSytem->createDir('expansion');
32
        $fileSytem = new Filesystem(new Local(realpath($container->getParameter('kernel.root_dir') . "/../var/expansion")));
33
34
        $this->fetchDataFromRemote($fileSytem, $container);
35
36
        if ($fileSytem->has(self::MAPPINGS_FILE)) {
37
            $data = json_decode($fileSytem->read(self::MAPPINGS_FILE), true);
38
            if ($data) {
39
                $values = [];
40
                foreach ($data as $titleId => $titleInformation) {
41
                    $values[$titleId] = $titleInformation['game'];
42
                }
43
44
                $container->setParameter("expansion.storage.gamedata.titles", $values);
45
            }
46
        }
47
    }
48
49
    /**
50
     * Fetch title information from eXp api.
51
     *
52
     * @param Filesystem       $fileSytem
53
     * @param ContainerBuilder $container
54
     *
55
     * @throws \Exception
56
     */
57
    protected function fetchDataFromRemote(Filesystem $fileSytem, ContainerBuilder $container)
58
    {
59
        $curl = new Curl();
60
        $curl->setUrl("https://mp-expansion.com/api/maniaplanet/games");
61
        $curl->run();
62
        if ($curl->getCurlInfo()['http_code'] == 200) {
63
            $fileSytem->put(self::MAPPINGS_FILE, $curl->getResponse());
64
        } else {
65
            $container->get("logger")->warning("Can't fetch title mappings from expansion website.");
66
        }
67
    }
68
}