Completed
Pull Request — Laravel4 (#36)
by
unknown
10:35
created

User::GetPlayerSummaries()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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