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

User::GetPlayerSummaries()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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