Completed
Push — master ( cac559...b1b643 )
by De Cramer
12s
created

MapDataProvider   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 4
dl 0
loc 97
ccs 38
cts 38
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A onRun() 0 5 1
A updateMapList() 0 20 4
B onMapListModified() 0 28 4
1
<?php
2
3
namespace eXpansion\Core\DataProviders;
4
5
use eXpansion\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\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 3
    }
40
41
    /**
42
     * Called when eXpansion is started.
43
     */
44 2
    public function onRun()
45
    {
46 2
        $this->updateMapList();
47
48 2
    }
49
50
    /**
51
     * Update the list of maps in the storage.
52
     */
53 2
    protected function updateMapList()
54
    {
55 2
        $start = 0;
56
57
        do {
58
            try {
59 2
                $maps = $this->connection->getMapList(self::BATCH_SIZE, $start);
60 2
            } catch (IndexOutOfBoundException $e) {
61
                // This is normal error when we we are trying to find all maps and we are out of bounds.
62 1
                return;
63
            }
64
65 2
            foreach ($maps as $map) {
66 2
                $this->mapStorage->addMap($map);
67 2
            }
68
69 2
            $start += self::BATCH_SIZE;
70
71 2
        } while(count($maps) == self::BATCH_SIZE);
72 1
    }
73
74
    /**
75
     * Called when map list is modified.
76
     *
77
     * @param $curMapIndex
78
     * @param $nextMapIndex
79
     * @param $isListModified
80
     *
81
     */
82 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...
83
    {
84 1
        if ($isListModified) {
85 1
            $oldMaps = $this->mapStorage->getMaps();
86
87 1
            $this->mapStorage->resetMapData();
88 1
            $this->updateMapList();
89
90
            // We will dispatch even only when list is modified. If not we dispatch specific events.
91 1
            $this->dispatch(__FUNCTION__, [$oldMaps, $curMapIndex, $nextMapIndex]);
92 1
        }
93
94 1
        $currentMap = $this->mapStorage->getMap($curMapIndex);
95 1
        if ($this->mapStorage->getCurrentMap()->uId != $curMapIndex) {
96 1
            $previousMap = $this->mapStorage->getCurrentMap();
97 1
            $this->mapStorage->setCurrentMap($currentMap);
98
99 1
            $this->dispatch('onExpansionMapChange', [$currentMap, $previousMap]);
100 1
        }
101
102 1
        $nextMap = $this->mapStorage->getMap($nextMapIndex);
103 1
        if ($this->mapStorage->getNextMap()->uId != $nextMapIndex) {
104 1
            $previousNextMap = $this->mapStorage->getNextMap();
105 1
            $this->mapStorage->setNextMap($nextMap);
106
107 1
            $this->dispatch('onExpansionNextMapChange', [$nextMap, $previousNextMap]);
108 1
        }
109 1
    }
110
}
111