Completed
Push — master ( c7652d...e90b5e )
by
unknown
02:21 queued 10s
created

Player::getLocation()   B

Complexity

Conditions 9
Paths 7

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 9

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 11
cts 11
cp 1
rs 8.0555
c 0
b 0
f 0
cc 9
nc 7
nop 0
crap 9
1
<?php
2
3
namespace Syntax\SteamApi\Containers;
4
5
class Player extends BaseContainer
6
{
7
    public $steamId;
8
9
    public $steamIds;
10
11
    public $communityVisibilityState;
12
13
    public $profileState;
14
15
    public $personaName;
16
17
    public $lastLogoff;
18
19
    public $profileUrl;
20
21
    public $avatar;
22
23
    public $avatarMedium;
24
25
    public $avatarFull;
26
27
    public $avatarUrl;
28
29
    public $avatarMediumUrl;
30
31
    public $avatarFullUrl;
32
33
    public $personaState;
34
35
    public $personaStateId;
36
37
    public $realName;
38
39
    public $primaryClanId;
40
41
    public $timecreated;
42
43
    public $personaStateFlags;
44
45
    public $locCountryCode;
46
47
    public $locStateCode;
48
49
    public $locCityId;
50
51
    public $location;
52
53
    public $commentPermission;
54
55
    public $gameDetails = null;
56
57 2
    public function __construct($player)
58
    {
59 2
        $this->steamId                  = $player->steamid;
60 2
        $this->steamIds                 = (new Id((int)$this->steamId));
61 2
        $this->communityVisibilityState = $player->communityvisibilitystate;
62 2
        $this->profileState             = $this->checkIssetField($player, 'profilestate');
63 2
        $this->personaName              = $player->personaname;
64 2
        $this->lastLogoff               = date('F jS, Y h:ia', $this->checkIssetField($player, 'lastlogoff'));
65 2
        $this->profileUrl               = $player->profileurl;
66 2
        $this->avatar                   = $this->getImageForAvatar($player->avatar);
67 2
        $this->avatarMedium             = $this->getImageForAvatar($player->avatarmedium);
68 2
        $this->avatarFull               = $this->getImageForAvatar($player->avatarfull);
69 2
        $this->avatarUrl                = $player->avatar;
70 2
        $this->avatarMediumUrl          = $player->avatarmedium;
71 2
        $this->avatarFullUrl            = $player->avatarfull;
72 2
        $this->personaState             = $this->convertPersonaState($player->personastate);
73 2
        $this->personaStateId           = $player->personastate;
74 2
        $this->realName                 = $this->checkIssetField($player, 'realname');
75 2
        $this->primaryClanId            = $this->checkIssetField($player, 'primaryclanid');
76 2
        $this->timecreated              = $this->checkIssetField($player, 'timecreated');
77 2
        $this->personaStateFlags        = $this->checkIssetField($player, 'personastateflags');
78 2
        $this->locCountryCode           = $this->checkIssetField($player, 'loccountrycode');
79 2
        $this->locStateCode             = $this->checkIssetField($player, 'locstatecode');
80 2
        $this->locCityId                = $this->checkIssetField($player, 'loccityid');
81 2
        $this->location                 = $this->getLocation();
82 2
        $this->commentPermission        = $this->checkIssetField($player, 'commentpermission');
83
84
        $gameDetails = [
85 2
            'gameServerIp'      => $this->checkIssetField($player, 'gameserverip'),
86 2
            'gameServerSteamId' => $this->checkIssetField($player, 'gameserversteamid'),
87 2
            'gameExtraInfo'     => $this->checkIssetField($player, 'gameextrainfo'),
88 2
            'gameId'            => $this->checkIssetField($player, 'gameid'),
89
        ];
90
91 2
        if (! empty(array_filter($gameDetails)))
92
        {
93 1
            $this->gameDetails = (new GameDetails($player));
94
        }
95 2
    }
96
97 2
    protected function getLocation()
98
    {
99 2
        $countriesFile = json_decode(\file_get_contents(__DIR__ . '/../Resources/countries.json'));
100 2
        $result        = new \stdClass;
101
102 2
        if ($this->locCountryCode != null && isset($countriesFile->{$this->locCountryCode})) {
103 2
            $result->country = $countriesFile->{$this->locCountryCode}->name;
104
105 2
            if ($this->locStateCode != null && isset($countriesFile->{$this->locCountryCode}->states->{$this->locStateCode})) {
106 2
                $result->state = $countriesFile->{$this->locCountryCode}->states->{$this->locStateCode}->name;
107
            }
108
109 2
            if ($this->locCityId != null && isset($countriesFile->{$this->locCountryCode}->states->{$this->locStateCode}) && ! empty($countriesFile->{$this->locCountryCode}->states->{$this->locStateCode}->cities)) {
110 2
                if (isset($countriesFile->{$this->locCountryCode}->states->{$this->locStateCode}->cities->{$this->locCityId})) {
111 2
                    $result->city = $countriesFile->{$this->locCountryCode}->states->{$this->locStateCode}->cities->{$this->locCityId}->name;
112
                }
113
            }
114
        }
115
116 2
        return $result;
117
    }
118
119 2
    protected function convertPersonaState($personaState)
120
    {
121 2
        switch ($personaState) {
122 2
            case 0:
123 2
                return '<span class="text-error">Offline</span>';
124 1
            case 1:
125 1
                return '<span class="text-success">Online</span>';
126 1
            case 2:
127
                return '<span class="text-warning">Busy</span>';
128 1
            case 3:
129
                return '<span class="text-warning">Away</span>';
130 1
            case 4:
131 1
                return '<span class="text-warning">Snooze</span>';
132
            case 5:
133
                return 'Looking to Trade';
134
            case 6:
135
                return 'Looking to Play';
136
        }
137
    }
138
}
139