|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
class CoC_League |
|
4
|
|
|
{ |
|
5
|
|
|
protected $api; |
|
6
|
|
|
protected $leagueId; |
|
7
|
|
|
protected $league = NULL; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Constructor of CoC_League |
|
11
|
|
|
* |
|
12
|
|
|
* @param $leagueId |
|
13
|
|
|
*/ |
|
14
|
|
|
public function __construct($league) |
|
15
|
|
|
{ |
|
16
|
|
|
$this->api = new ClashOfClans(); |
|
17
|
|
|
if(is_object($league)) |
|
18
|
|
|
{ |
|
19
|
|
|
$this->league = $league; |
|
20
|
|
|
} |
|
21
|
|
|
else |
|
22
|
|
|
{ |
|
23
|
|
|
$this->leagueId = $league; |
|
24
|
|
|
$this->getLeague(); |
|
25
|
|
|
} |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Gets an stdClass containing league-information |
|
30
|
|
|
* |
|
31
|
|
|
* @return stdClass, league |
|
32
|
|
|
*/ |
|
33
|
|
|
protected function getLeague() |
|
34
|
|
|
{ |
|
35
|
|
|
if($this->league == NULL) |
|
36
|
|
|
{ |
|
37
|
|
View Code Duplication |
foreach ($this->api->getLeagueList()->items as $league) |
|
|
|
|
|
|
38
|
|
|
{ |
|
39
|
|
|
if ($league->id == $this->leagueId) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->league = $league; |
|
42
|
|
|
return $this->league; |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
return 0; |
|
46
|
|
|
} |
|
47
|
|
|
else |
|
48
|
|
|
{ |
|
49
|
|
|
return $this->league; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Sets the league by providing it's exact name |
|
56
|
|
|
* |
|
57
|
|
|
* @param string, league name |
|
58
|
|
|
* @return bool, success or fail |
|
59
|
|
|
*/ |
|
60
|
|
|
public function setLeagueByName($name) |
|
61
|
|
|
{ |
|
62
|
|
View Code Duplication |
foreach ($this->api->getLeagueList()->items as $league) |
|
|
|
|
|
|
63
|
|
|
{ |
|
64
|
|
|
if ($league->name == $name) |
|
65
|
|
|
{ |
|
66
|
|
|
$this->leagueId = $league->id; |
|
67
|
|
|
$this->league = $league; |
|
68
|
|
|
return 1; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
return 0; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Gets the league ID |
|
76
|
|
|
* |
|
77
|
|
|
* @return int |
|
78
|
|
|
*/ |
|
79
|
|
|
public function getLeagueId() |
|
80
|
|
|
{ |
|
81
|
|
|
return $this->getLeague()->id; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Gets the league name |
|
86
|
|
|
* |
|
87
|
|
|
* @return string, name |
|
88
|
|
|
*/ |
|
89
|
|
|
public function getLeagueName() |
|
90
|
|
|
{ |
|
91
|
|
|
return $this->getLeague()->name; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Sets the league by providing it's exact name |
|
96
|
|
|
* |
|
97
|
|
|
* @param (optional) $size ("tiny", "small", "medium") |
|
98
|
|
|
* @return string, URL to the picture |
|
99
|
|
|
*/ |
|
100
|
|
|
public function getLeagueIcon($size = "") //small, tiny, medium |
|
101
|
|
|
{ |
|
102
|
|
|
$league = $this->getLeague(); |
|
103
|
|
|
switch ($size) { |
|
104
|
|
|
case "small": |
|
105
|
|
|
return $league->iconUrls->small; |
|
106
|
|
|
break; |
|
107
|
|
|
case "medium": |
|
108
|
|
|
return $league->iconUrls->medium; |
|
109
|
|
|
break; |
|
110
|
|
|
case "tiny": |
|
111
|
|
|
return $league->iconUrls->tiny; |
|
112
|
|
|
break; |
|
113
|
|
|
default: |
|
114
|
|
|
return $league->iconUrls->small; //medium is not always available, so lets chose small on default |
|
115
|
|
|
break; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
?> |
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.