Completed
Pull Request — master (#305)
by
unknown
03:43
created

MapListDataProvider::onMapListModified()   C

Complexity

Conditions 8
Paths 72

Size

Total Lines 46
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 46
ccs 0
cts 25
cp 0
rs 5.5555
cc 8
eloc 26
nc 72
nop 3
crap 72
1
<?php
2
3
namespace eXpansion\Framework\GameManiaplanet\DataProviders;
4
5
use eXpansion\Framework\Core\DataProviders\AbstractDataProvider;
6
use eXpansion\Framework\Core\Plugins\StatusAwarePluginInterface;
7
use eXpansion\Framework\Core\Services\DedicatedConnection\Factory;
8
use eXpansion\Framework\Core\Storage\MapStorage;
9
use Maniaplanet\DedicatedServer\Connection;
10
use Maniaplanet\DedicatedServer\Xmlrpc\IndexOutOfBoundException;
11
use Maniaplanet\DedicatedServer\Xmlrpc\NextMapException;
12
13
/**
14
 * Class MapDataProvider provides information to plugins about what is going on with the maps on the server.
15
 *
16
 * @package eXpansion\Framework\Core\DataProviders
17
 */
18
class MapListDataProvider extends AbstractDataProvider implements StatusAwarePluginInterface
19
{
20
    /** Size of batch to get maps from the storage. */
21
    const BATCH_SIZE = 500;
22
23
    /**
24
     * @var MapStorage
25
     */
26
    protected $mapStorage;
27
28
    /**
29
     * @var Factory
30
     */
31
    protected $connection;
32
33
    /**
34
     * MapListDataProvider constructor.
35
     *
36
     * @param MapStorage $mapStorage
37
     * @param Factory $factory
38
     */
39
    public function __construct(MapStorage $mapStorage, Factory $factory)
40
    {
41
        $this->mapStorage = $mapStorage;
42
        $this->connection = $factory->getConnection();
0 ignored issues
show
Documentation Bug introduced by
It seems like $factory->getConnection() can also be of type object<Maniaplanet\DedicatedServer\Connection>. However, the property $connection is declared as type object<eXpansion\Framewo...atedConnection\Factory>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
43
    }
44
45
    /**
46
     * @inheritdoc
47
     */
48
    public function setStatus($status)
49
    {
50
        if ($status) {
51
            $this->updateMapList();
52
            $currentMap = $this->connection->getCurrentMapInfo();
0 ignored issues
show
Bug introduced by
The method getCurrentMapInfo() does not seem to exist on object<eXpansion\Framewo...atedConnection\Factory>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
53
            if ($currentMap) {
54
                $this->mapStorage->setCurrentMap($currentMap);
55
                try {
56
                    $this->mapStorage->setNextMap($this->connection->getNextMapInfo());
0 ignored issues
show
Bug introduced by
The method getNextMapInfo() does not seem to exist on object<eXpansion\Framewo...atedConnection\Factory>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
57
                } catch (NextMapException $ex) {
58
                    $this->mapStorage->setNextMap($currentMap);
59
                }
60
            }
61
        }
62
    }
63
64
65
    /**
66
     * Update the list of maps in the storage.
67
     */
68
    protected function updateMapList()
69
    {
70
        $start = 0;
71
72
        do {
73
            try {
74
                $maps = $this->connection->getMapList(self::BATCH_SIZE, $start);
0 ignored issues
show
Bug introduced by
The method getMapList() does not seem to exist on object<eXpansion\Framewo...atedConnection\Factory>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
75
            } catch (IndexOutOfBoundException $e) {
76
                // This is normal error when we we are trying to find all maps and we are out of bounds.
77
                return;
78
            } catch (NextMapException $ex) {
79
                // this is if no maps defined
80
                return;
81
            }
82
83
            if (!empty($maps)) {
84
                foreach ($maps as $map) {
85
                    $this->mapStorage->addMap($map);
86
                }
87
            }
88
89
            $start += self::BATCH_SIZE;
90
91
        } while (count($maps) == self::BATCH_SIZE);
92
    }
93
94
    /**
95
     * Called when map list is modified.
96
     *
97
     * @param $curMapIndex
98
     * @param $nextMapIndex
99
     * @param $isListModified
100
     *
101
     */
102
    public function onMapListModified($curMapIndex, $nextMapIndex, $isListModified)
103
    {
104
        if ($isListModified) {
105
            $oldMaps = $this->mapStorage->getMaps();
106
107
            $this->mapStorage->resetMapData();
108
            $this->updateMapList();
109
110
            // We will dispatch even only when list is modified. If not we dispatch specific events.
111
            $this->dispatch(__FUNCTION__, [$oldMaps, $curMapIndex, $nextMapIndex, $isListModified]);
112
        }
113
114
        try {
115
            $currentMap = $this->connection->getCurrentMapInfo();  // sync better
0 ignored issues
show
Bug introduced by
The method getCurrentMapInfo() does not seem to exist on object<eXpansion\Framewo...atedConnection\Factory>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
116
        } catch (\Exception $e) {
117
            echo $e->getMessage(). "\n";
118
119
            $currentMap = null;
120
        }
121
122
        // current map can be false if map by index is not found..
123
        if ($currentMap) {
124
            if ($this->mapStorage->getCurrentMap()->uId != $currentMap->uId) {
125
                $previousMap = $this->mapStorage->getCurrentMap();
126
                $this->mapStorage->setCurrentMap($currentMap);
127
128
                $this->dispatch('onExpansionMapChange', [$currentMap, $previousMap]);
129
            }
130
        }
131
132
        try {
133
            $nextMap = $this->connection->getNextMapInfo();  // sync better
0 ignored issues
show
Bug introduced by
The method getNextMapInfo() does not seem to exist on object<eXpansion\Framewo...atedConnection\Factory>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
134
        } catch (\Exception $e) {
135
            echo $e->getMessage(). "\n";
136
            $nextMap = null;
137
        }
138
        // next map can be false if map by index is not found..
139
        if ($nextMap) {
140
            if ($this->mapStorage->getNextMap()->uId != $nextMap->uId) {
141
                $previousNextMap = $this->mapStorage->getNextMap();
142
                $this->mapStorage->setNextMap($nextMap);
143
144
                $this->dispatch('onExpansionNextMapChange', [$nextMap, $previousNextMap]);
145
            }
146
        }
147
    }
148
}
149