Completed
Push — master ( 61323d...82f5dc )
by
unknown
17s queued 10s
created

User::getChunkedPlayerSummaries()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 5
cts 5
cp 1
rs 9.7666
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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 11
    public function __construct($steamId)
17
    {
18 11
        parent::__construct();
19 11
        $this->interface = 'ISteamUser';
20 11
        $this->steamId   = $steamId;
21 11
    }
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 3
    public function ResolveVanityURL($displayName = null)
33
    {
34
        // This only works with a display name.  Make sure we have one.
35 3
        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 2
        $this->method  = __FUNCTION__;
41 2
        $this->version = 'v0001';
42
43 2
        $results = $this->setUpClient(['vanityurl' => $displayName])->response;
44
45
        // The message key is used when something goes wrong.  If it exists, return it.
46 2
        if (isset($results->message)) {
47 1
            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 4
    public function GetPlayerSummaries($steamId = null)
60
    {
61
        // Set up the api details
62 4
        $this->method  = __FUNCTION__;
63 4
        $this->version = 'v0002';
64
65 4
        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 1
            $steamId = $this->steamId;
67
        }
68
69 4
        $steamId = implode(',', (array)$steamId);
70
71 4
        $chunks = array_chunk(explode(',', $steamId), 100);
72
73 4
        $map = array_map([$this, 'getChunkedPlayerSummaries'], $chunks);
74
75 4
        $players = $this->compressPlayerSummaries($map);
76
77 4
        return $players;
78
    }
79
80 4
    private function getChunkedPlayerSummaries($chunk)
81
    {
82
        // Set up the arguments
83
        $arguments = [
84 4
            'steamids' => implode(',', $chunk),
85
        ];
86
87
        // Get the client
88 4
        $client = $this->setUpClient($arguments)->response;
89
90
        // Clean up the games
91 4
        $players = $this->convertToObjects($client->players);
92
93 4
        return $players;
94
    }
95
96 4
    private function compressPlayerSummaries($summaries)
97
    {
98 4
        $result = [];
99 4
        $keys   = array_keys($summaries);
100
101 4
        foreach ($keys as $key) {
102 4
            $result = array_merge($result, $summaries[$key]);
103
        }
104
105 4
        return $result;
106
    }
107
108 2
    public function GetPlayerBans($steamId = null)
109
    {
110
        // Set up the api details
111 2
        $this->method  = __FUNCTION__;
112 2
        $this->version = 'v1';
113
114 2
        if ($steamId == null) {
115 1
            $steamId = $this->steamId;
116
        }
117
118
        // Set up the arguments
119
        $arguments = [
120 2
            'steamids' => implode(',', (array)$steamId),
121
        ];
122
123
        // Get the client
124 2
        $client = $this->setUpClient($arguments);
125
126 2
        return $client->players;
127
    }
128
129 3
    public function GetFriendList($relationship = 'all', $summaries = true)
130
    {
131
        // Set up the api details
132 3
        $this->method  = __FUNCTION__;
133 3
        $this->version = 'v0001';
134
135 3
        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 2
            'steamid'      => $this->steamId,
142 2
            'relationship' => $relationship,
143
        ];
144
145
        // Get the client
146 2
        $client = $this->setUpClient($arguments)->friendslist;
147
148
        // Clean up the games
149 2
        $steamIds = [];
150
151 2
        foreach ($client->friends as $friend) {
152 2
            $steamIds[] = $friend->steamid;
153
        }
154
155 2
        if($summaries) {
156 2
            $friends = $this->GetPlayerSummaries(implode(',', $steamIds));
157
        } else {
158
            $friends = $steamIds;
159
        }
160
161 2
        return $friends;
162
    }
163
164 4
    protected function convertToObjects($players)
165
    {
166 4
        $cleanedPlayers = [];
167
168 4
        foreach ($players as $player) {
169 4
            if(property_exists($player, 'steamid')) {
170 4
                $cleanedPlayers[] = new PlayerContainer($player);
171
            }
172
        }
173
174 4
        return $cleanedPlayers;
175
    }
176
}
177