Completed
Pull Request — master (#22)
by De Cramer
02:18
created

MapStorage::getMapByIndex()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 3
cp 0
cc 1
eloc 3
nc 1
nop 1
crap 2
1
<?php
2
3
namespace eXpansion\Core\Storage;
4
5
use Maniaplanet\DedicatedServer\Structures\Map;
6
use oliverde8\AssociativeArraySimplified\AssociativeArray;
7
8
/**
9
 * Class MapStorage stores data on the maps to be played & that is being currently played.
10
 *
11
 * @package eXpansion\Core\Storage
12
 * @author Oliver de Cramer
13
 */
14
class MapStorage
15
{
16
    /** @var Map[] List of all current maps on the server. */
17
    protected $maps = [];
18
19
    /** @var Map Current map being played. */
20
    protected $currentMap;
21
22
    /** @var Map Next map to be played. */
23
    protected $nextMap;
24
25
    /**
26
     * Add a map to the current map list.
27
     *
28
     * @param Map $map
29
     *
30
     */
31 4
    public function addMap(Map $map)
32
    {
33 4
        $this->maps[$map->uId] = $map;
34 4
    }
35
36
    /**
37
     * Get current map list.
38
     *
39
     * @return array
40
     */
41 2
    public function getMaps()
42
    {
43 2
        return $this->maps;
44
    }
45
46
    /**
47
     * Get a map.
48
     *
49
     * @param string $uid The unique id of the map to get.
50
     *
51
     * @return mixed
52
     */
53 1
    public function getMap($uid)
54
    {
55 1
        return AssociativeArray::getFromKey($this->maps, $uid, null);
56
    }
57
58
    /**
59
     * Get a map.
60
     *
61
     * @param integer $index the index number of the map to fetch
62
     *
63
     * @return Map|null
64
     */
65
    public function getMapByIndex($index)
66
    {
67
        $map = array_slice($this->maps, (int) $index, 1, false);
68
        return end($map);
69
    }
70
71
72
    /**
73
     * Reset map data.
74
     */
75 1
    public function resetMapData()
76
    {
77 1
        $this->maps = [];
78 1
    }
79
80
    /**
81
     * Get current map being played.
82
     *
83
     * @return Map
84
     */
85 1
    public function getCurrentMap()
86
    {
87 1
        return $this->currentMap;
88
    }
89
90
    /**
91
     * Set the current map when it's changed.
92
     *
93
     * @param Map $currentMap
94
     */
95 1
    public function setCurrentMap(Map $currentMap)
96
    {
97 1
        $this->currentMap = $currentMap;
98 1
    }
99
100
    /**
101
     * Get next map to be played.
102
     *
103
     * @return Map
104
     */
105 1
    public function getNextMap()
106
    {
107 1
        return $this->nextMap;
108
    }
109
110
    /**
111
     * Set the next map that is going to be played.
112
     *
113
     * @param Map $nextMap
114
     */
115 1
    public function setNextMap($nextMap)
116
    {
117 1
        $this->nextMap = $nextMap;
118 1
    }
119
}
120