Completed
Pull Request — master (#68)
by De Cramer
05:45
created

MapDataProvider::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 2
crap 2
1
<?php
2
3
namespace eXpansion\Framework\Core\DataProviders;
4
5
use eXpansion\Framework\Core\Storage\MapStorage;
6
use Maniaplanet\DedicatedServer\Connection;
7
use Maniaplanet\DedicatedServer\Xmlrpc\IndexOutOfBoundException;
8
9
/**
10
 * Class MapDataProvider provides information to plugins about what is going on with the maps on the server.
11
 *
12
 * @package eXpansion\Framework\Core\DataProviders
13
 */
14
class MapDataProvider extends AbstractDataProvider
15
{
16
    /** Size of batch to get maps from the storage. */
17
    const BATCH_SIZE = 500;
18
19
    /**
20
     * @var MapStorage
21
     */
22
    protected $mapStorage;
23
24
    /**
25
     * @var Connection
26
     */
27
    protected $connection;
28
29
    /**
30
     * PlayerDataProvider constructor.
31
     *
32
     * @param MapStorage $mapStorage
33
     * @param Connection $connection
34
     */
35 3
    public function __construct(MapStorage $mapStorage, Connection $connection)
36
    {
37 3
        $this->mapStorage = $mapStorage;
38 3
        $this->connection = $connection;
39
40 3
        $this->updateMapList();
41
42 3
        $currentMap = $this->connection->getCurrentMapInfo();
43 3
        if ($currentMap) {
44 2
            $this->mapStorage->setCurrentMap($currentMap);
45 2
            $this->mapStorage->setNextMap($this->connection->getNextMapInfo());
46
        }
47 3
    }
48
49
    /**
50
     * Update the list of maps in the storage.
51
     */
52 3
    protected function updateMapList()
53
    {
54 3
        $start = 0;
55
56
        do {
57
            try {
58 3
                $maps = $this->connection->getMapList(self::BATCH_SIZE, $start);
59 1
            } catch (IndexOutOfBoundException $e) {
60
                // This is normal error when we we are trying to find all maps and we are out of bounds.
61 1
                return;
62
            }
63
64 3
            if ($maps) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $maps of type Maniaplanet\DedicatedServer\Structures\Map[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
65 2
                foreach ($maps as $map) {
66 2
                    $this->mapStorage->addMap($map);
67
                }
68
            }
69
70 3
            $start += self::BATCH_SIZE;
71
72 3
        } while (count($maps) == self::BATCH_SIZE);
73 2
    }
74
75
    /**
76
     * Called when map list is modified.
77
     *
78
     * @param $curMapIndex
79
     * @param $nextMapIndex
80
     * @param $isListModified
81
     *
82
     */
83 1
    function onMapListModified($curMapIndex, $nextMapIndex, $isListModified)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
84
    {
85 1
        if ($isListModified) {
86 1
            $oldMaps = $this->mapStorage->getMaps();
87
88 1
            $this->mapStorage->resetMapData();
89 1
            $this->updateMapList();
90
91
            // We will dispatch even only when list is modified. If not we dispatch specific events.
92 1
            $this->dispatch(__FUNCTION__, [$oldMaps, $curMapIndex, $nextMapIndex]);
93
        }
94
95 1
        $currentMap = $this->mapStorage->getMapByIndex($curMapIndex);
96
        // current map can be false if map by index is not found..
97 1
        if ($currentMap) {
98 1
            if ($this->mapStorage->getCurrentMap()->uId != $currentMap->uId) {
99 1
                $previousMap = $this->mapStorage->getCurrentMap();
100 1
                $this->mapStorage->setCurrentMap($currentMap);
101
102 1
                $this->dispatch('onExpansionMapChange', [$currentMap, $previousMap]);
103
            }
104
        }
105
106 1
        $nextMap = $this->mapStorage->getMapByIndex($nextMapIndex);
107
        // next map can be false if map by index is not found..
108 1
        if ($nextMap) {
109 1
            if ($this->mapStorage->getNextMap()->uId != $nextMap->uId) {
110 1
                $previousNextMap = $this->mapStorage->getNextMap();
111 1
                $this->mapStorage->setNextMap($nextMap);
112
113 1
                $this->dispatch('onExpansionNextMapChange', [$nextMap, $previousNextMap]);
114
            }
115
        }
116 1
    }
117
}
118