Completed
Push — master ( a73180...86ff1d )
by Jelle
09:42
created

SportProxy::loadLeagues()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 0
crap 2
1
<?php
2
/**
3
 * @file
4
 * Contains \TheSportsDb\Entity\Proxy\SportProxy.
5
 */
6
7
namespace TheSportsDb\Entity\Proxy;
8
9
use TheSportsDb\Entity\LeagueInterface;
10
use TheSportsDb\Entity\SportInterface;
11
12
/**
13
 * A sport object that is not yet fully loaded.
14
 *
15
 * @author Jelle Sebreghts
16
 */
17
class SportProxy extends Proxy implements SportInterface {
18
19
  /**
20
   * {@inheritdoc}
21
   */
22 1
  public function load() {
23 1
    $this->loadLeagues();
24 1
  }
25
26
  /**
27
   * Lazy loads the leagues for this sport.
28
   *
29
   * @throws \Exception
30
   *   When the leagues cannot be loaded.
31
   *
32
   * @return void
33
   */
34 1
  protected function loadLeagues() {
35 1
    $leagueData = $this->sportsDbClient->doRequest('search_all_leagues.php', array('s' => $this->properties->id));
36 1
    if (isset($leagueData->countrys)) {
37 1
      $this->update($this->entityManager->mapProperties((object) array('leagues' => $leagueData->countrys), $this->getEntityType()));
38 1
      return;
39
    }
40 1
    throw new \Exception('Could not fully load sport with id ' . $this->properties->id . '.');
41
  }
42
43
  /**
44
   * {@inheritdoc}
45
   */
46 1
  public function getId() {
47 1
    return $this->get('id');
48
  }
49
50
  /**
51
   * {@inheritdoc}
52
   */
53 1
  public function getLeagues() {
54 1
    return $this->get('leagues');
55
  }
56
57
  /**
58
   * {@inheritdoc}
59
   */
60 1
  public function getName() {
61 1
    return $this->get('name');
62
  }
63
64
  /**
65
   * {@inheritdoc}
66
   */
67 1
  public function addLeague(LeagueInterface $league) {
68 1
    if ($this->entity) {
69
      $this->entity->addLeague($league);
70
      return;
71
    }
72 1
    if (!isset($this->properties->leagues)) {
73
      $this->properties->leagues = array();
74
    }
75 1
    if (!isset($this->properties->leagues[$league->getId()])) {
76 1
      $this->properties->leagues[$league->getId()] = $league;
77
    }
78 1
  }
79
80
}
81