Completed
Push — master ( a9d592...d707cc )
by Jelle
09:44
created

TheSportsDb   B

Complexity

Total Complexity 42

Size/Duplication

Total Lines 462
Duplicated Lines 12.99 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 95.51%

Importance

Changes 9
Bugs 2 Features 0
Metric Value
wmc 42
lcom 1
cbo 3
dl 60
loc 462
ccs 85
cts 89
cp 0.9551
rs 8.295
c 9
b 2
f 0

31 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 2
A getSports() 0 3 1
A getSport() 12 12 3
A getLeagues() 0 3 1
A getLeague() 12 12 3
A getLeaguesByCountry() 0 3 1
A getLeaguesBySport() 0 3 1
A getLeaguesByCountryAndSport() 0 3 1
A getTeam() 12 12 3
A getTeamsByName() 0 3 1
A getTeamsByLeague() 0 3 1
A getTeamsByLeagueName() 0 3 1
A getTeamsBySportAndCountry() 0 3 1
A getPlayer() 12 12 3
A getPlayersByTeam() 0 3 1
A getPlayersByTeamName() 0 3 1
A getPlayersByName() 0 3 1
A getPlayersByTeamNameAndName() 0 3 1
A getEvent() 12 12 3
A getEventsByName() 0 3 1
A getEventsByFileName() 0 3 1
A getEventsByNameAndSeason() 0 3 1
A getNextFiveEventsByTeam() 0 3 1
A getNextFifteenEventsByLeague() 0 3 1
A getNextFifteenEventsByLeagueAndRound() 0 3 1
A getLastFiveEventsByTeam() 0 3 1
A getLastFifteenEventsByLeague() 0 3 1
A getEventsByDay() 0 3 1
A getEventsByLeagueRoundAndSeason() 0 3 1
A getEventsByLeagueAndSeason() 0 3 1
A getSeasonsByLeague() 0 3 1

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like TheSportsDb often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use TheSportsDb, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * @file
4
 * The main sportsdb class.
5
 */
6
7
namespace TheSportsDb;
8
9
use TheSportsDb\Entity\EntityManagerConsumerInterface;
10
use TheSportsDb\Entity\EntityManagerConsumerTrait;
11
use TheSportsDb\Entity\EntityManagerInterface;
12
use TheSportsDb\Entity\Proxy\ProxyInterface;
13
14
/**
15
 * Main API class for TheSportsDb.
16
 *
17
 * @author Jelle Sebreghts
18
 */
19
class TheSportsDb implements EntityManagerConsumerInterface {
20
21
  use EntityManagerConsumerTrait;
22
23
  /**
24
   * Creates a new TheSportsDb instance
25
   */
26
  public function __construct(EntityManagerInterface $entityManager = NULL) {
27
    if ($entityManager instanceof EntityManagerInterface) {
28
      $this->entityManager = $entityManager;
29
    }
30
  }
31
32
  /**
33
   * Gets all sports.
34
   *
35
   * @return \TheSportsDb\Entity\SportInterface[]
36
   *   The sports.
37
   */
38 1
  public function getSports() {
39 1
    return $this->entityManager->repository('sport')->all();
40
  }
41
42
  /**
43
   * Get a sport by name.
44
   *
45
   * @param string $name
46
   *   The sport name.
47
   *
48
   * @return \TheSportsDb\Entity\SportInterface
49
   *   The sport.
50
   */
51 1 View Code Duplication
  public function getSport($name) {
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52 1
    $entity = $this->entityManager->repository('sport')->byId($name);
53 1
    if ($entity instanceof ProxyInterface) {
54
      try {
55 1
        $entity->load();
56
      }
57 1
      catch (\Exception $e) {
58 1
        return FALSE;
59
      }
60
    }
61 1
    return $entity;
62
  }
63
64
  /**
65
   * Gets all leagues.
66
   *
67
   * @return \TheSportsDb\Entity\LeagueInterface[]
68
   *   The leagues.
69
   */
70 1
  public function getLeagues() {
71 1
    return $this->entityManager->repository('league')->all();
72
  }
73
74
  /**
75
   * Get a league by id.
76
   *
77
   * @param int $leagueId
78
   *   The league id.
79
   *
80
   * @return \TheSportsDb\Entity\LeagueInterface
81
   *   The league.
82
   */
83 1 View Code Duplication
  public function getLeague($leagueId) {
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
84 1
    $entity = $this->entityManager->repository('league')->byId($leagueId);
85 1
    if ($entity instanceof ProxyInterface) {
86
      try {
87 1
        $entity->load();
88
      }
89 1
      catch (\Exception $e) {
90 1
        return FALSE;
91
      }
92
    }
93 1
    return $entity;
94
  }
95
96
  /**
97
   * Gets all leagues by country.
98
   *
99
   * @param string $country
100
   *   The country of which to get the leagues.
101
   *
102
   * @return \TheSportsDb\Entity\LeagueInterface[]
103
   *   The leagues.
104
   */
105 1
  public function getLeaguesByCountry($country) {
106 1
    return $this->entityManager->repository('league')->byCountry($country);
107
  }
108
109
  /**
110
   * Gets all leagues by sport.
111
   *
112
   * @param mixed $sport
113
   *   The sport of which to get the leagues.
114
   *
115
   * @return \TheSportsDb\Entity\LeagueInterface[]
116
   *   The leagues.
117
   */
118 1
  public function getLeaguesBySport($sport) {
119 1
    return $this->entityManager->repository('league')->bySport($sport);
120
  }
121
122
  /**
123
   * Gets all leagues by country and sport.
124
   *
125
   * @param string $country
126
   *   The country of which to get the leagues.
127
   * @param mixed $sport
128
   *   The sport of which to get the leagues.
129
   *
130
   * @return \TheSportsDb\Entity\LeagueInterface[]
131
   *   The leagues.
132
   */
133 1
  public function getLeaguesByCountryAndSport($country, $sport) {
134 1
    return $this->entityManager->repository('league')->byCountryAndSport($country, $sport);
135
  }
136
137
  /**
138
   * Gets a team by id.
139
   *
140
   * @param mixed $teamId
141
   *   The team id.
142
   *
143
   * @return \TheSportsDb\Entity\TeamInterface
144
   *   The team.
145
   */
146 1 View Code Duplication
  public function getTeam($teamId) {
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
147 1
    $entity = $this->entityManager->repository('team')->byId($teamId);
148 1
    if ($entity instanceof ProxyInterface) {
149
      try {
150 1
        $entity->load();
151
      }
152 1
      catch (\Exception $e) {
153 1
        return FALSE;
154
      }
155
    }
156 1
    return $entity;
157
  }
158
159
  /**
160
   * Gets a team by name.
161
   *
162
   * @param string $teamName
163
   *   The team name.
164
   *
165
   * @return \TheSportsDb\Entity\TeamInterface[]
166
   *   The team.
167
   */
168 1
  public function getTeamsByName($teamName) {
169 1
    return $this->entityManager->repository('team')->byName($teamName);
170
  }
171
172
  /**
173
   * Gets a teams by league id.
174
   *
175
   * @param mixed $leagueId
176
   *   The league id.
177
   *
178
   * @return \TheSportsDb\Entity\TeamInterface[]
179
   *   The teams.
180
   */
181 1
  public function getTeamsByLeague($leagueId) {
182 1
    return $this->entityManager->repository('team')->byLeague($leagueId);
183
  }
184
185
  /**
186
   * Gets a teams by league name.
187
   *
188
   * @param string $leagueName
189
   *   The league name.
190
   *
191
   * @return \TheSportsDb\Entity\TeamInterface[]
192
   *   The teams.
193
   */
194 1
  public function getTeamsByLeagueName($leagueName) {
195 1
    return $this->entityManager->repository('team')->byLeagueName($leagueName);
196
  }
197
198
  /**
199
   * Gets a teams by sport and country.
200
   *
201
   * @param mixed $sport
202
   *   The sport.
203
   * @param string $country
204
   *   The country.
205
   *
206
   * @return \TheSportsDb\Entity\TeamInterface[]
207
   *   The teams.
208
   */
209 1
  public function getTeamsBySportAndCountry($sport, $country) {
210 1
    return $this->entityManager->repository('team')->bySportAndCountry($sport, $country);
211
  }
212
213
  /**
214
   * Get a player by id.
215
   *
216
   * @param mixed $playerId
217
   *   The player id.
218
   *
219
   * @return \TheSportsDb\Entity\PlayerInterface
220
   *   The player.
221
   */
222 1 View Code Duplication
  public function getPlayer($playerId) {
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
223 1
    $entity = $this->entityManager->repository('player')->byId($playerId);
224 1
    if ($entity instanceof ProxyInterface) {
225
      try {
226 1
        $entity->load();
227
      }
228 1
      catch (\Exception $e) {
229 1
        return FALSE;
230
      }
231
    }
232 1
    return $entity;
233
  }
234
235
  /**
236
   * Get a players by team id.
237
   *
238
   * @param mixed $teamId
239
   *   The team id.
240
   *
241
   * @return \TheSportsDb\Entity\PlayerInterface[]
242
   *   The players.
243
   */
244 1
  public function getPlayersByTeam($teamId) {
245 1
    return $this->entityManager->repository('player')->byTeam($teamId);
246
  }
247
248
  /**
249
   * Get a players by team name.
250
   *
251
   * @param mixed $teamName
252
   *   The team name.
253
   *
254
   * @return \TheSportsDb\Entity\PlayerInterface[]
255
   *   The players.
256
   */
257 1
  public function getPlayersByTeamName($teamName) {
258 1
    return $this->entityManager->repository('player')->byTeamName($teamName);
259
  }
260
261
  /**
262
   * Get a players by name.
263
   *
264
   * @param mixed $name
265
   *   The name.
266
   *
267
   * @return \TheSportsDb\Entity\PlayerInterface[]
268
   *   The players.
269
   */
270 1
  public function getPlayersByName($name) {
271 1
    return $this->entityManager->repository('player')->byName($name);
272
  }
273
274
  /**
275
   * Get a players by team and name.
276
   *
277
   * @param string $teamName
278
   *   The team name.
279
   * @param string $name
280
   *   The name.
281
   *
282
   * @return \TheSportsDb\Entity\PlayerInterface[]
283
   *   The players.
284
   */
285 1
  public function getPlayersByTeamNameAndName($teamName, $name) {
286 1
    return $this->entityManager->repository('player')->byTeamNameAndName($teamName, $name);
287
  }
288
289
  /**
290
   * Get an event by id.
291
   *
292
   * @param mixed $eventId
293
   *   The event id.
294
   *
295
   * @return \TheSportsDb\Entity\EventInterface
296
   *   The event.
297
   */
298 1 View Code Duplication
  public function getEvent($eventId) {
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
299 1
    $entity = $this->entityManager->repository('event')->byId($eventId);
300 1
    if ($entity instanceof ProxyInterface) {
301
      try {
302 1
        $entity->load();
303
      }
304 1
      catch (\Exception $e) {
305 1
        return FALSE;
306
      }
307
    }
308 1
    return $entity;
309
  }
310
311
  /**
312
   * Get events by name.
313
   *
314
   * @param string $name
315
   *   The event name.
316
   *
317
   * @return \TheSportsDb\Entity\EventInterface[]
318
   *   The events.
319
   */
320 1
  public function getEventsByName($name) {
321 1
    return $this->entityManager->repository('event')->byName($name);
322
  }
323
324
  /**
325
   * Get events by file name.
326
   *
327
   * @param string $name
328
   *   The file name.
329
   *
330
   * @return \TheSportsDb\Entity\EventInterface[]
331
   *   The events.
332
   */
333 1
  public function getEventsByFileName($name) {
334 1
    return $this->entityManager->repository('event')->byFileName($name);
335
  }
336
337
  /**
338
   * Get events by name and season.
339
   *
340
   * @param string $name
341
   *   The event name.
342
   * @param mixed $season
343
   *   The season.
344
   *
345
   * @return \TheSportsDb\Entity\EventInterface[]
346
   *   The events.
347
   */
348 1
  public function getEventsByNameAndSeason($name, $season) {
349 1
    return $this->entityManager->repository('event')->byNameAndSeason($name, $season);
350
  }
351
352
  /**
353
   * Get next five events by team.
354
   *
355
   * @param mixed $teamId
356
   *   The team id.
357
   *
358
   * @return \TheSportsDb\Entity\EventInterface[]
359
   *   The events.
360
   */
361 1
  public function getNextFiveEventsByTeam($teamId) {
362 1
    return $this->entityManager->repository('event')->nextFiveByTeam($teamId);
363
  }
364
365
  /**
366
   * Get next fifteen events by league.
367
   *
368
   * @param mixed $leagueId
369
   *   The league id.
370
   *
371
   * @return \TheSportsDb\Entity\EventInterface[]
372
   *   The events.
373
   */
374 1
  public function getNextFifteenEventsByLeague($leagueId) {
375 1
    return $this->entityManager->repository('event')->nextFifteenByLeague($leagueId);
376
  }
377
378
  /**
379
   * Get next fifteen events by league and round.
380
   *
381
   * @param mixed $leagueId
382
   *   The league id.
383
   * @param int $round
384
   *   The round.
385
   *
386
   * @return \TheSportsDb\Entity\EventInterface[]
387
   *   The events.
388
   */
389 1
  public function getNextFifteenEventsByLeagueAndRound($leagueId, $round) {
390 1
    return $this->entityManager->repository('event')->nextFifteenByLeagueAndRound($leagueId, $round);
391
  }
392
393
  /**
394
   * Get last five events by team.
395
   *
396
   * @param mixed $teamId
397
   *   The team id.
398
   *
399
   * @return \TheSportsDb\Entity\EventInterface[]
400
   *   The events.
401
   */
402 1
  public function getLastFiveEventsByTeam($teamId) {
403 1
    return $this->entityManager->repository('event')->lastFiveByTeam($teamId);
404
  }
405
406
  /**
407
   * Get last fifteen events by league.
408
   *
409
   * @param mixed $leagueId
410
   *   The league id.
411
   *
412
   * @return \TheSportsDb\Entity\EventInterface[]
413
   *   The events.
414
   */
415 1
  public function getLastFifteenEventsByLeague($leagueId) {
416 1
    return $this->entityManager->repository('event')->lastFifteenByLeague($leagueId);
417
  }
418
419
  /**
420
   * Get events by day.
421
   *
422
   * @param \DateTime $date
423
   *   The day.
424
   * @param string|null $sport
425
   *   The sport.
426
   * @param string|null $leagueName
427
   *   The league name.
428
   *
429
   * @return \TheSportsDb\Entity\EventInterface[]
430
   *   The events.
431
   */
432 1
  public function getEventsByDay(\DateTime $date, $sport = NULL, $leagueName = NULL) {
433 1
    return $this->entityManager->repository('event')->byDay($date, $sport, $leagueName);
434
  }
435
436
  /**
437
   * Get events by league, round and season.
438
   *
439
   * @param mixed $leagueId
440
   *   The league id.
441
   * @param int $round
442
   *   The round.
443
   * @param mixed $season
444
   *   The season.
445
   *
446
   * @return \TheSportsDb\Entity\EventInterface[]
447
   *   The events.
448
   */
449 1
  public function getEventsByLeagueRoundAndSeason($leagueId, $round, $season) {
450 1
    return $this->entityManager->repository('event')->byLeagueRoundAndSeason($leagueId, $round, $season);
451
  }
452
453
  /**
454
   * Get events by league and season.
455
   *
456
   * @param mixed $leagueId
457
   *   The league id.
458
   * @param mixed $season
459
   *   The season.
460
   *
461
   * @return \TheSportsDb\Entity\EventInterface[]
462
   *   The events.
463
   */
464 1
  public function getEventsByLeagueAndSeason($leagueId, $season) {
465 1
    return $this->entityManager->repository('event')->byLeagueAndSeason($leagueId, $season);
466
  }
467
468
  /**
469
   * Get seasons by league.
470
   *
471
   * @param mixed $leagueId
472
   *   The league id.
473
   *
474
   * @return \TheSportsDb\Entity\SeasonInterface[]
475
   *   The seasons.
476
   */
477 1
  public function getSeasonsByLeague($leagueId) {
478 1
    return $this->entityManager->repository('season')->byLeague($leagueId);
479
  }
480
}
481