Completed
Push — master ( 4a002f...a1b70a )
by Ezra
13s queued 10s
created

Player::getLocation()   B

Complexity

Conditions 9
Paths 7

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 0
cts 11
cp 0
rs 8.0555
c 0
b 0
f 0
cc 9
nc 7
nop 0
crap 90
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
    public function __construct($player)
58
    {
59
        $this->steamId                  = $player->steamid;
60
        $this->steamIds                 = (new Id((int)$this->steamId));
61
        $this->communityVisibilityState = $player->communityvisibilitystate;
62
        $this->profileState             = $this->checkIssetField($player, 'profilestate');
63
        $this->personaName              = $player->personaname;
64
        $this->lastLogoff               = date('F jS, Y h:ia', $this->checkIssetField($player, 'lastlogoff'));
65
        $this->profileUrl               = $player->profileurl;
66
        $this->avatar                   = $this->getImageForAvatar($player->avatar);
67
        $this->avatarMedium             = $this->getImageForAvatar($player->avatarmedium);
68
        $this->avatarFull               = $this->getImageForAvatar($player->avatarfull);
69
        $this->avatarUrl                = $player->avatar;
70
        $this->avatarMediumUrl          = $player->avatarmedium;
71
        $this->avatarFullUrl            = $player->avatarfull;
72
        $this->personaState             = $this->convertPersonaState($player->personastate);
73
        $this->personaStateId           = $player->personastate;
74
        $this->realName                 = $this->checkIssetField($player, 'realname');
75
        $this->primaryClanId            = $this->checkIssetField($player, 'primaryclanid');
76
        $this->timecreated              = $this->checkIssetField($player, 'timecreated');
77
        $this->personaStateFlags        = $this->checkIssetField($player, 'personastateflags');
78
        $this->locCountryCode           = $this->checkIssetField($player, 'loccountrycode');
79
        $this->locStateCode             = $this->checkIssetField($player, 'locstatecode');
80
        $this->locCityId                = $this->checkIssetField($player, 'loccityid');
81
        $this->location                 = $this->getLocation();
82
        $this->commentPermission        = $this->checkIssetField($player, 'commentpermission');
83
84
        $gameDetails = [
85
            'gameServerIp'      => $this->checkIssetField($player, 'gameserverip'),
86
            'gameServerSteamId' => $this->checkIssetField($player, 'gameserversteamid'),
87
            'gameExtraInfo'     => $this->checkIssetField($player, 'gameextrainfo'),
88
            'gameId'            => $this->checkIssetField($player, 'gameid'),
89
        ];
90
91
        if (! empty(array_filter($gameDetails)))
92
        {
93
            $this->gameDetails = (new GameDetails($player));
94
        }
95
    }
96
97
    protected function getLocation()
98
    {
99
        $countriesFile = json_decode(\file_get_contents(__DIR__ . '/../Resources/countries.json'));
100
        $result        = new \stdClass;
101
102
        if ($this->locCountryCode != null && isset($countriesFile->{$this->locCountryCode})) {
103
            $result->country = $countriesFile->{$this->locCountryCode}->name;
104
105
            if ($this->locStateCode != null && isset($countriesFile->{$this->locCountryCode}->states->{$this->locStateCode})) {
106
                $result->state = $countriesFile->{$this->locCountryCode}->states->{$this->locStateCode}->name;
107
            }
108
109
            if ($this->locCityId != null && isset($countriesFile->{$this->locCountryCode}->states->{$this->locStateCode}) && ! empty($countriesFile->{$this->locCountryCode}->states->{$this->locStateCode}->cities)) {
110
                if (isset($countriesFile->{$this->locCountryCode}->states->{$this->locStateCode}->cities->{$this->locCityId})) {
111
                    $result->city = $countriesFile->{$this->locCountryCode}->states->{$this->locStateCode}->cities->{$this->locCityId}->name;
112
                }
113
            }
114
        }
115
116
        return $result;
117
    }
118
119
    protected function convertPersonaState($personaState)
120
    {
121
        switch ($personaState) {
122
            case 0:
123
                $state = '<span class="text-error">Offline</span>';
124
                break;
125
            case 1:
126
                $state = '<span class="text-success">Online</span>';
127
                break;
128
            case 2:
129
                $state = '<span class="text-warning">Busy</span>';
130
                break;
131
            case 3:
132
                $state = '<span class="text-warning">Away</span>';
133
                break;
134
            case 4:
135
                $state = '<span class="text-warning">Snooze</span>';
136
                break;
137
            case 5:
138
                $state = 'Looking to Trade';
139
                break;
140
            case 6:
141
                $state = 'Looking to Play';
142
                break;
143
            default:
144
                $state = 'Unknown';
145
        }
146
147
        return $state;
148
    }
149
}
150