GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 11c0a1...856a6b )
by Jacky
34s
created

PlaceRepository   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 311
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 311
rs 10
c 0
b 0
f 0
wmc 30
lcom 1
cbo 4

14 Methods

Rating   Name   Duplication   Size   Complexity  
B select() 0 34 1
A get() 0 14 3
A getByIds() 0 16 3
A getSystemPlaces() 0 16 3
A getPlayerPlaces() 0 16 3
A getNpcPlaces() 0 16 3
A search() 0 21 3
A insert() 0 23 1
A update() 0 17 1
A updatePlace() 0 15 2
A turnAsSpawnPlace() 0 14 1
A npcQuickfix() 0 4 1
A remove() 0 5 1
B format() 0 53 4
1
<?php
2
3
namespace Asylamba\Modules\Gaia\Repository;
4
5
use Asylamba\Classes\Entity\AbstractRepository;
6
7
use Asylamba\Modules\Gaia\Model\Place;
8
9
class PlaceRepository extends AbstractRepository
10
{
11
	public function select($whereClause = '', $parameters = [])
12
	{
13
		$statement = $this->connection->prepare(
14
			'SELECT p.*,
15
			s.rSector AS rSector,
16
			s.xPosition AS xPosition,
17
			s.yPosition AS yPosition,
18
			s.typeOfSystem AS typeOfSystem,
19
			se.tax AS tax,
20
			se.rColor AS sectorColor,
21
			pl.rColor AS playerColor,
22
			pl.name AS playerName,
23
			pl.avatar AS playerAvatar,
24
			pl.status AS playerStatus,
25
			pl.level AS playerLevel,
26
			ob.rPlace AS obId,
27
			ob.name AS obName,
28
			ob.points AS points,
29
			ob.levelCommercialPlateforme AS levelCommercialPlateforme,
30
			ob.levelSpatioport AS levelSpatioport,
31
			ob.resourcesStorage AS obResources,
32
			ob.antiSpyAverage AS antiSpyAverage,
33
			ob.typeOfBase AS obTypeOfBase
34
			FROM place AS p
35
			LEFT JOIN system s ON p.rSystem = s.id
36
			LEFT JOIN sector se ON s.rSector = se.id
37
			LEFT JOIN player pl ON p.rPlayer = pl.id
38
			LEFT JOIN orbitalBase ob ON p.id = ob.rPlace '
39
			. $whereClause
40
		);
41
		$statement->execute($parameters);
42
		
43
		return $statement;
44
	}
45
	
46
	/**
47
	 * @param int $id
48
	 * @return Place
49
	 */
50
	public function get($id)
51
	{
52
		if (($p = $this->unitOfWork->getObject(Place::class, $id)) !== null) {
53
			return $p;
54
		}
55
		$statement = $this->select('WHERE p.id = :id', ['id' => $id]);
56
		
57
		if (($row = $statement->fetch()) === false) {
58
			return null;
59
		}
60
		$place = $this->format($row);
61
		$this->unitOfWork->addObject($place);
62
		return $place;
63
	}
64
	
65
	/**
66
	 * @param array $ids
67
	 * @return array
68
	 */
69
	public function getByIds($ids)
70
	{
71
		$statement = $this->select('WHERE p.id IN (' . implode(',', $ids) . ')');
72
		
73
		$data = [];
74
		while ($row = $statement->fetch()) {
75
			if (($p = $this->unitOfWork->getObject(Place::class, $row['id'])) !== null) {
76
				$data[] = $p;
77
				continue;
78
			}
79
			$place = $this->format($row);
80
			$this->unitOfWork->addObject($place);
81
			$data[] = $place;
82
		}
83
		return $data;
84
	}
85
	
86
	/**
87
	 * @param array $systemId
88
	 * @return array
89
	 */
90
	public function getSystemPlaces($systemId)
91
	{
92
		$statement = $this->select('WHERE p.rSystem = :system_id ' . $this->getOrderByClause(['position' => 'ASC']), ['system_id' => $systemId]);
93
		
94
		$data = [];
95
		while ($row = $statement->fetch()) {
96
			if (($p = $this->unitOfWork->getObject(Place::class, $row['id'])) !== null) {
97
				$data[] = $p;
98
				continue;
99
			}
100
			$place = $this->format($row);
101
			$this->unitOfWork->addObject($place);
102
			$data[] = $place;
103
		}
104
		return $data;
105
	}
106
	
107
	/**
108
	 * @return array
109
	 */
110
	public function getPlayerPlaces()
111
	{
112
		$statement = $this->select('WHERE p.rPlayer IS NOT NULL AND p.typeOfPlace = ' . Place::TERRESTRIAL);
113
		
114
		$data = [];
115
		while ($row = $statement->fetch()) {
116
			if (($p = $this->unitOfWork->getObject(Place::class, $row['id'])) !== null) {
117
				$data[] = $p;
118
				continue;
119
			}
120
			$place = $this->format($row);
121
			$this->unitOfWork->addObject($place);
122
			$data[] = $place;
123
		}
124
		return $data;
125
	}
126
	
127
	/**
128
	 * @return array
129
	 */
130
	public function getNpcPlaces()
131
	{
132
		$statement = $this->select('WHERE p.rPlayer IS NULL AND typeOfPlace = ' . Place::TERRESTRIAL);
133
		
134
		$data = [];
135
		while ($row = $statement->fetch()) {
136
			if (($p = $this->unitOfWork->getObject(Place::class, $row['id'])) !== null) {
137
				$data[] = $p;
138
				continue;
139
			}
140
			$place = $this->format($row);
141
			$this->unitOfWork->addObject($place);
142
			$data[] = $place;
143
		}
144
		return $data;
145
	}
146
	
147
	/**
148
	 * @param string $search
149
	 * @return array
150
	 */
151
	public function search($search)
152
	{
153
		$statement = $this->select(
154
			'WHERE (pl.statement = 1 OR pl.statement = 2 OR pl.statement = 3)
155
			AND (LOWER(pl.name) LIKE LOWER(:place_name)
156
			OR LOWER(ob.name) LIKE LOWER(:base_name)) ' . $this->getOrderByClause(['pl.id' => 'DESC']) . ' LIMIT 20',
157
			['place_name' => "%$search%", 'base_name' => "%$search%"]
158
		);
159
		
160
		$data = [];
161
		while ($row = $statement->fetch()) {
162
			if (($p = $this->unitOfWork->getObject(Place::class, $row['id'])) !== null) {
163
				$data[] = $p;
164
				continue;
165
			}
166
			$place = $this->format($row);
167
			$this->unitOfWork->addObject($place);
168
			$data[] = $place;
169
		}
170
		return $data;
171
	}
172
	
173
	public function insert($place)
174
	{
175
		$qr = $this->connection->prepare(
176
			'INSERT INTO place(rPlayer, rSystem, typeOfPlace, position, population,
177
			coefResources, coefHistory, resources, danger, maxDanger, uPlace)
178
			VALUES(:player_id, :system_id, :place_type, :position, :population,
179
			:resources_coeff, :history_coeff, :resources, :danger, :maxDanger, :uPlace)'
180
		);
181
		$qr->execute(array(
182
			'player_id' => $place->getRPlayer(),
183
			'system_id' => $place->getRSystem(),
184
			'place_type' => $place->getTypeOfPlace(),
185
			'position' => $place->getPosition(),
186
			'population' => $place->getPopulation(),
187
			'resources_coeff' => $place->getCoefResources(),
188
			'history_coeff' => $place->getCoefHistory(),
189
			'resources' => $place->getResources(),
190
			'danger' => $place->danger,
191
			'maxDanger' => $place->maxDanger,
192
			'uPlace' => $place->uPlace
193
		));
194
		$place->setId($this->connection->lastInsertId());
195
	}
196
	
197
	public function update($place)
198
	{
199
		$statement = $this->connection->prepare(
200
			'UPDATE place SET
201
				rPlayer = :player_id,
202
				resources = :resources,
203
				danger = :danger,
204
				uPlace = :u_place
205
			WHERE id = :id');
206
		$statement->execute(array(
207
			'player_id' => $place->getRPlayer(),
208
			'resources' => $place->getResources(),
209
			'danger' => $place->danger,
210
			'u_place' => $place->uPlace,
211
			'id' => $place->getId()
212
		));
213
	}
214
	
215
	/**
216
	 * @param Place $place
217
	 * @param boolean $updateDanger
218
	 */
219
	public function updatePlace($place, $updateDanger = false)
220
	{
221
		$statement = $this->connection->prepare(
222
			'UPDATE place SET
223
				resources = resources + :resources,
224
				danger = danger + :danger,
225
				uPlace = :updated_at
226
			WHERE id = :id');
227
		$statement->execute([
228
			'resources' => $place->resources,
229
			'danger' => ($updateDanger === true) ? $place->danger : 0,
230
			'updated_at' => $place->uPlace, 
231
			'id' => $place->getId()
232
		]);
233
	}
234
	
235
	/**
236
	 * @param int $placeId
237
	 * @param int $playerId
238
	 * @return bool
239
	 */
240
	public function turnAsSpawnPlace($placeId, $playerId)
241
	{
242
		$statement = $this->connection->prepare(
243
			'UPDATE place SET rPlayer = :player_id, coefResources = :resources_coeff,
244
			coefHistory = :history_coeff, population = :population WHERE id = :id'
245
		);
246
		return $statement->execute([
247
			'player_id' => $playerId,
248
			'resources_coeff' => 60,
249
			'history_coeff' => 20,
250
			'population' => 50,
251
			'id' => $placeId
252
		]);
253
	}
254
	
255
	public function npcQuickfix()
256
	{
257
		$this->connection->exec('UPDATE place SET danger = maxDanger WHERE danger > maxDanger');
258
	}
259
	
260
	public function remove($place)
261
	{
262
		$statement = $this->connection->prepare('DELETE FROM place FROM id = :id');
263
		$statement->execute(['id' => $place->getId()]);
264
	}
265
	
266
	public function format($data)
267
	{
268
		$place = new Place();
269
270
		$place->setId((int) $data['id']);
271
		$place->setRSystem((int) $data['rSystem']);
272
		$place->setTypeOfPlace((int) $data['typeOfPlace']);
273
		$place->setPosition($data['position']);
274
		$place->setPopulation((float) $data['population']);
275
		$place->setCoefResources($data['coefResources']);
276
		$place->setCoefHistory($data['coefHistory']);
277
		$place->setResources((int) $data['resources']);
278
		$place->danger = (int) $data['danger'];
279
		$place->maxDanger = (int) $data['maxDanger'];
280
		$place->uPlace = $data['uPlace'];
281
282
		$place->setRSector($data['rSector']);
283
		$place->setXSystem($data['xPosition']);
284
		$place->setYSystem($data['yPosition']);
285
		$place->setTypeOfSystem($data['typeOfSystem']);
286
		$place->setTax($data['tax']);
287
		$place->setSectorColor((int) $data['sectorColor']);
288
289
		if ($data['rPlayer'] != 0) {
290
			$place->setRPlayer((int) $data['rPlayer']);
291
			$place->setPlayerColor((int) $data['playerColor']);
292
			$place->setPlayerName($data['playerName']);
293
			$place->setPlayerAvatar($data['playerAvatar']);
294
			$place->setPlayerStatus((int) $data['playerStatus']);
295
			$place->playerLevel = (int) $data['playerLevel'];
296
			if (isset($data['msId'])) {
297
				$place->setTypeOfBase((int) $data['msType']);
298
				$place->setBaseName($data['msName']);
299
				$place->setResources((int) $data['msResources']);
300
			} elseif (isset($data['obId'])) {
301
				$place->setTypeOfBase(Place::TYP_ORBITALBASE);
302
				$place->typeOfOrbitalBase = (int) $data['obTypeOfBase'];
303
				$place->setBaseName($data['obName']);
304
				$place->setLevelCommercialPlateforme((int) $data['levelCommercialPlateforme']);
305
				$place->setLevelSpatioport((int) $data['levelSpatioport']);
306
				$place->setResources((int) $data['obResources']);
307
				$place->setAntiSpyInvest((int) $data['antiSpyAverage']);
308
				$place->setPoints((int) $data['points']);
309
			} else {
310
				throw new ErrorException('Problèmes d\'appartenance du lieu !');
311
			}
312
		} else {
313
			$place->setTypeOfBase(Place::TYP_EMPTY);
314
			$place->setBaseName('Planète rebelle');
315
			$place->setPoints(0);
316
		}
317
		return $place;
318
	}
319
}