Completed
Push — master ( 57abcf...bb1066 )
by Travis
12:58 queued 09:31
created

User   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 162
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 0 Features 1
Metric Value
wmc 16
c 5
b 0
f 1
lcom 1
cbo 3
dl 0
loc 162
ccs 69
cts 69
cp 1
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A ResolveVanityURL() 0 21 3
A GetPlayerSummaries() 0 20 2
A getChunkedPlayerSummaries() 0 15 1
A compressPlayerSummaries() 0 11 2
A GetPlayerBans() 0 20 2
B GetFriendList() 0 30 3
A convertToObjects() 0 10 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 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 1
        }
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 4
        ];
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 4
        }
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 1
        }
117
118
        // Set up the arguments
119
        $arguments = [
120 2
            'steamids' => implode(',', (array)$steamId),
121 2
        ];
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')
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 2
        ];
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 2
        }
154
155 2
        $friends = $this->GetPlayerSummaries(implode(',', $steamIds));
156
157 2
        return $friends;
158
    }
159
160 4
    protected function convertToObjects($players)
161
    {
162 4
        $cleanedPlayers = [];
163
164 4
        foreach ($players as $player) {
165 4
            $cleanedPlayers[] = new PlayerContainer($player);
166 4
        }
167
168 4
        return $cleanedPlayers;
169
    }
170
}
171