Completed
Push — master ( a1b70a...dd69ef )
by Ezra
02:32
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 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 11
    public function __construct($steamId)
18
    {
19 11
        parent::__construct();
20 11
        $this->interface = 'ISteamUser';
21 11
        $this->steamId   = $steamId;
22 11
    }
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 3
    public function ResolveVanityURL($displayName = null)
34
    {
35
        // This only works with a display name.  Make sure we have one.
36 3
        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 2
        $this->method  = __FUNCTION__;
42 2
        $this->version = 'v0001';
43
44 2
        $results = $this->setUpClient(['vanityurl' => $displayName])->response;
45
46
        // The message key is used when something goes wrong.  If it exists, return it.
47 2
        if (isset($results->message)) {
48 1
            return $results->message;
49
        }
50
51
        // Return the full steam ID object for the display name.
52 1
        return $this->convertId($results->steamid);
53
    }
54
55
    /**
56
     * @param string $steamId
57
     *
58
     * @return array
59
     */
60 4
    public function GetPlayerSummaries($steamId = null)
61
    {
62
        // Set up the api details
63 4
        $this->method  = __FUNCTION__;
64 4
        $this->version = 'v0002';
65
66 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...
67 1
            $steamId = $this->steamId;
68
        }
69
70 4
        $steamId = implode(',', (array)$steamId);
71
72 4
        $chunks = array_chunk(explode(',', $steamId), 100);
73
74 4
        $map = array_map([$this, 'getChunkedPlayerSummaries'], $chunks);
75
76 4
        return $this->compressPlayerSummaries($map);
77
    }
78
79 4
    private function getChunkedPlayerSummaries($chunk)
80
    {
81
        // Set up the arguments
82
        $arguments = [
83 4
            'steamids' => implode(',', $chunk),
84
        ];
85
86
        // Get the client
87 4
        $client = $this->setUpClient($arguments)->response;
88
89
        // Clean up the games
90 4
        return $this->convertToObjects($client->players);
91
    }
92
93 4
    private function compressPlayerSummaries($summaries)
94
    {
95 4
        $result = [];
96 4
        $keys   = array_keys($summaries);
97
98 4
        foreach ($keys as $key) {
99 4
            $result = array_merge($result, $summaries[$key]);
100
        }
101
102 4
        return $result;
103
    }
104
105 2
    public function GetPlayerBans($steamId = null)
106
    {
107
        // Set up the api details
108 2
        $this->method  = __FUNCTION__;
109 2
        $this->version = 'v1';
110
111 2
        if ($steamId == null) {
112 1
            $steamId = $this->steamId;
113
        }
114
115
        // Set up the arguments
116
        $arguments = [
117 2
            'steamids' => implode(',', (array)$steamId),
118
        ];
119
120
        // Get the client
121 2
        $client = $this->setUpClient($arguments);
122
123 2
        return $client->players;
124
    }
125
126 3
    public function GetFriendList($relationship = 'all', $summaries = true)
127
    {
128
        // Set up the api details
129 3
        $this->method  = __FUNCTION__;
130 3
        $this->version = 'v0001';
131
132 3
        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 2
            'steamid'      => $this->steamId,
139 2
            'relationship' => $relationship,
140
        ];
141
142
        // Get the client
143 2
        $client = $this->setUpClient($arguments)->friendslist;
144
145
        // Clean up the games
146 2
        $steamIds = [];
147
148 2
        foreach ($client->friends as $friend) {
149 2
            $steamIds[] = $friend->steamid;
150
        }
151
152 2
        if($summaries) {
153 2
            $friends = $this->GetPlayerSummaries(implode(',', $steamIds));
154
        } else {
155
            $friends = $steamIds;
156
        }
157
158 2
        return $friends;
159
    }
160
161 4
    protected function convertToObjects($players)
162
    {
163 4
        $cleanedPlayers = [];
164
165 4
        foreach ($players as $player) {
166 4
            if(property_exists($player, 'steamid')) {
167 4
                $cleanedPlayers[] = new PlayerContainer($player);
168
            }
169
        }
170
171 4
        return $cleanedPlayers;
172
    }
173
}
174