Completed
Push — master ( c0ede7...0a2d54 )
by
unknown
02:00 queued 10s
created

User::GetPlayerBans()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 8
cts 8
cp 1
rs 9.6
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Syntax\SteamApi\Steam;
4
5
use Syntax\SteamApi\Client;
6
use Syntax\SteamApi\Containers\Player as PlayerContainer;
7
use Syntax\SteamApi\Exceptions\UnrecognizedId;
8
9
class User extends Client
10
{
11
    private $friendRelationships = [
12
        'all',
13
        'friend',
14
    ];
15
16 6
    public function __construct($steamId)
17
    {
18 6
        parent::__construct();
19 6
        $this->interface = 'ISteamUser';
20 6
        $this->steamId   = $steamId;
21 6
    }
22
23
    /**
24
     * Get the user_ids for a display name.
25
     *
26
     * @param null $displayName Custom name from steam profile link.
27
     *
28
     * @return mixed
29
     *
30
     * @throws UnrecognizedId
31
     */
32 2
    public function ResolveVanityURL($displayName = null)
33
    {
34
        // This only works with a display name.  Make sure we have one.
35 2
        if ($displayName == null) {
36 1
            throw new UnrecognizedId('You must pass a display name for this call.');
37
        }
38
39
        // Set up the api details
40 1
        $this->method  = __FUNCTION__;
41 1
        $this->version = 'v0001';
42
43 1
        $results = $this->setUpClient(['vanityurl' => $displayName])->response;
44
45
        // The message key is used when something goes wrong.  If it exists, return it.
46 1
        if (isset($results->message)) {
47
            return $results->message;
48
        }
49
50
        // Return the full steam ID object for the display name.
51 1
        return $this->convertId($results->steamid);
52
    }
53
54
    /**
55
     * @param string $steamId
56
     *
57
     * @return array
58
     */
59 1
    public function GetPlayerSummaries($steamId = null)
60
    {
61
        // Set up the api details
62 1
        $this->method  = __FUNCTION__;
63 1
        $this->version = 'v0002';
64
65 1
        if ($steamId == null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $steamId of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
66
            $steamId = $this->steamId;
67
        }
68
69 1
        $steamId = implode(',', (array)$steamId);
70
71 1
        $chunks = array_chunk(explode(',', $steamId), 100);
72
73 1
        $map = array_map([$this, 'getChunkedPlayerSummaries'], $chunks);
74
75 1
        $players = $this->compressPlayerSummaries($map);
76
77 1
        return $players;
78
    }
79
80 1
    private function getChunkedPlayerSummaries($chunk)
81
    {
82
        // Set up the arguments
83
        $arguments = [
84 1
            'steamids' => implode(',', $chunk),
85
        ];
86
87
        // Get the client
88 1
        $client = $this->setUpClient($arguments)->response;
89
90
        // Clean up the games
91 1
        $players = $this->convertToObjects($client->players);
92
93 1
        return $players;
94
    }
95
96 1
    private function compressPlayerSummaries($summaries)
97
    {
98 1
        $result = [];
99 1
        $keys   = array_keys($summaries);
100
101 1
        foreach ($keys as $key) {
102 1
            $result = array_merge($result, $summaries[$key]);
103
        }
104
105 1
        return $result;
106
    }
107
108 1
    public function GetPlayerBans($steamId = null)
109
    {
110
        // Set up the api details
111 1
        $this->method  = __FUNCTION__;
112 1
        $this->version = 'v1';
113
114 1
        if ($steamId == null) {
115 1
            $steamId = $this->steamId;
116
        }
117
118
        // Set up the arguments
119
        $arguments = [
120 1
            'steamids' => implode(',', (array)$steamId),
121
        ];
122
123
        // Get the client
124 1
        $client = $this->setUpClient($arguments);
125
126 1
        return $client->players;
127
    }
128
129 2
    public function GetFriendList($relationship = 'all', $summaries = true)
130
    {
131
        // Set up the api details
132 2
        $this->method  = __FUNCTION__;
133 2
        $this->version = 'v0001';
134
135 2
        if (! in_array($relationship, $this->friendRelationships)) {
136 1
            throw new \InvalidArgumentException('Provided relationship [' . $relationship . '] is not valid.  Please select from: ' . implode(', ', $this->friendRelationships));
137
        }
138
139
        // Set up the arguments
140
        $arguments = [
141 1
            'steamid'      => $this->steamId,
142 1
            'relationship' => $relationship,
143
        ];
144
145
        // Get the client
146 1
        $client = $this->setUpClient($arguments)->friendslist;
147
148
        // Clean up the games
149 1
        $steamIds = [];
150
151 1
        foreach ($client->friends as $friend) {
152 1
            $steamIds[] = $friend->steamid;
153
        }
154
155 1
        if($summaries) {
156 1
            $friends = $this->GetPlayerSummaries(implode(',', $steamIds));
157
        } else {
158
            $friends = $steamIds;
159
        }
160
161 1
        return $friends;
162
    }
163
164 1
    protected function convertToObjects($players)
165
    {
166 1
        $cleanedPlayers = [];
167
168 1
        foreach ($players as $player) {
169 1
            if(property_exists($player, 'steamid')) {
170 1
                $cleanedPlayers[] = new PlayerContainer($player);
171
            }
172
        }
173
174 1
        return $cleanedPlayers;
175
    }
176
}
177