Completed
Pull Request — master (#37)
by Mahmoud
08:04
created

Stats::GetPlayerAchievementsAPI()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 29
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 6.5488

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 29
ccs 4
cts 15
cp 0.2667
rs 8.8571
cc 3
eloc 14
nc 2
nop 1
crap 6.5488
1
<?php namespace Syntax\SteamApi\Steam\User;
2
3
use Syntax\SteamApi\Client;
4
use Syntax\SteamApi\Containers\Achievement;
5
6
class Stats extends Client
7
{
8
9 6
    public function __construct($steamId)
10
    {
11 6
        parent::__construct();
12 6
        $this->interface = 'ISteamUserStats';
13 6
        $this->steamId   = $steamId;
14 6
    }
15
16
    /**
17
     * @deprecated
18
     *
19
     * @param $appId
20
     *
21
     * @return array
22
     */
23 1
    public function GetPlayerAchievementsAPI($appId)
24 1
    {
25
        // Set up the api details
26 1
        $this->method  = 'GetPlayerAchievementsAPI';
27 1
        $this->version = 'v0001';
28
29
        // Set up the arguments
30
        $arguments = [
31
            'steamid' => $this->steamId,
32
            'appid'   => $appId,
33
            'l'       => 'english',
34
        ];
35
36
        // Get the client
37
        $stats = $this->GetSchemaForGame($appId);
38
39
        // Make sure the game has achievements
40
        if ($stats == null || $stats->game->availableGameStats->achievements == null) {
41
            return null;
42
        }
43
44
        $client = $this->setUpClient($arguments)->playerstats;
45
        $stats  = $stats->game->availableGameStats->achievements;
46
47
        // Clean up the games
48
        $achievements = $this->convertToObjects($client->achievements, $stats);
0 ignored issues
show
Unused Code introduced by
The call to Stats::convertToObjects() has too many arguments starting with $stats.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
49
50
        return $achievements;
51
    }
52
53 2
    public function GetPlayerAchievements($appId)
54
    {
55
        // Set up the api details
56 2
        $this->interface = null;
57 2
        $this->method    = 'achievements';
58
59 2
        if (is_numeric($this->steamId)) {
60 2
            $this->url = 'http://steamcommunity.com/profiles/';
61 2
        } else {
62
            $this->url = 'http://steamcommunity.com/id/';
63
        }
64
65 2
        $this->url = $this->url . $this->steamId . '/stats/' . $appId;
66
67
        // Set up the arguments
68
        $arguments = [
69
            'xml' => 1
70 2
        ];
71
72
        try {
73
            // Get the client
74 2
            $client = $this->setUpXml($arguments);
75
76
            // Clean up the games
77 1
            $achievements = $this->convertToObjects($client->achievements->achievement);
78
79 1
            return $achievements;
80 1
        } catch (\Exception $e) {
81
            // In rare cases, games can force the use of a simplified name instead of an app ID
82
            // In these cases, try again with the name.
83 1
            if (is_int($appId)) {
84 1
                $app     = $this->app()->appDetails($appId);
85
                $appName = str_replace(' ', '', $app->first()->name);
86
87
                return $this->GetPlayerAchievements($appName);
88
            }
89
90
            // If the name and ID fail, return null.
91
            return null;
92
        }
93
    }
94
95 1
    public function GetGlobalAchievementPercentagesForApp($gameId)
96
    {
97
        // Set up the api details
98 1
        $this->method  = __FUNCTION__;
99 1
        $this->version = 'v0002';
100
101
        // Set up the arguments
102
        $arguments = [
103 1
            'gameid' => $gameId,
104 1
            'l'      => 'english',
105 1
        ];
106
107
        // Get the client
108 1
        $client = $this->setUpClient($arguments)->achievementpercentages;
109
110
        return $client->achievements;
111
    }
112
113
    /**
114
     * @param $appId int Steam 64 id
115
     * @param $all   bool Return all stats when true and only achievements when false
116
     *
117
     * @return mixed
118
     */
119 2
    public function GetUserStatsForGame($appId, $all = false)
120
    {
121
        // Set up the api details
122 2
        $this->method  = __FUNCTION__;
123 2
        $this->version = 'v0002';
124
125
        // Set up the arguments
126
        $arguments = [
127 2
            'steamid' => $this->steamId,
128 2
            'appid'   => $appId,
129 2
            'l'       => 'english',
130 2
        ];
131
132
        // Get the client
133 2
        $client = $this->setUpClient($arguments)->playerstats;
134
135
        // Games like DOTA and CS:GO have additional stats here.  Return everything if they are wanted.
136
        if ($all === true) {
137
            return $client;
138
        }
139
140
        return $client->achievements;
141
    }
142
143
    /**
144
     * @param $appId
145
     *
146
     * @link https://wiki.teamfortress.com/wiki/WebAPI/GetSchemaForGame
147
     *
148
     * @return mixed
149
     */
150 1
    public function GetSchemaForGame($appId)
151
    {
152
        // Set up the api details
153 1
        $this->method  = __FUNCTION__;
154 1
        $this->version = 'v0002';
155
156
        // Set up the arguments
157
        $arguments = [
158 1
            'appid' => $appId,
159 1
            'l'     => 'english',
160 1
        ];
161
162
        // Get the client
163 1
        $client = $this->setUpClient($arguments);
164
165
        return $client;
166
    }
167
168 1
    protected function convertToObjects($achievements)
169
    {
170 1
        $cleanedAchievements = [];
171
172 1
        foreach ($achievements as $achievement) {
173 1
            $cleanedAchievements[] = new Achievement($achievement);
174 1
        }
175
176 1
        return $cleanedAchievements;
177
    }
178
}