Completed
Push — master ( 99fa2d...6291ae )
by Ezra
02:24
created

Player::convertPersonaState()   B

Complexity

Conditions 8
Paths 8

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 10.8127

Importance

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