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

Stats::GetUserStatsForGame()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 0
loc 23
ccs 11
cts 11
cp 1
rs 9.0856
cc 2
eloc 11
nc 2
nop 2
crap 2
1
<?php
2
3
namespace Syntax\SteamApi\Steam\User;
4
5
use Syntax\SteamApi\Client;
6
use Syntax\SteamApi\Containers\Achievement;
7
8
class Stats extends Client
9
{
10 6
    public function __construct($steamId)
11
    {
12 6
        parent::__construct();
13 6
        $this->interface = 'ISteamUserStats';
14 6
        $this->steamId   = $steamId;
15 6
    }
16
17
    /**
18
     * @deprecated
19
     *
20
     * @param $appId
21
     *
22
     * @return array
23
     */
24 1
    public function GetPlayerAchievementsAPI($appId)
25 1
    {
26
        // Set up the api details
27 1
        $this->method  = 'GetPlayerAchievementsAPI';
28 1
        $this->version = 'v0001';
29
30
        // Set up the arguments
31
        $arguments = [
32
            'steamid' => $this->steamId,
33
            'appid'   => $appId,
34
            'l'       => 'english',
35
        ];
36
37
        // Get the client
38
        $stats = $this->GetSchemaForGame($appId);
39
40
        // Make sure the game has achievements
41
        if ($stats == null || $stats->game->availableGameStats->achievements == null) {
42
            return null;
43
        }
44
45
        $client = $this->setUpClient($arguments)->playerstats;
46
        $stats  = $stats->game->availableGameStats->achievements;
47
48
        // Clean up the games
49
        $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...
50
51
        return $achievements;
52
    }
53
54 2
    public function GetPlayerAchievements($appId)
55
    {
56
        // Set up the api details
57 2
        $this->interface = null;
58 2
        $this->method    = 'achievements';
59
60 2
        if (is_numeric($this->steamId)) {
61 2
            $this->url = 'http://steamcommunity.com/profiles/';
62 2
        } else {
63
            $this->url = 'http://steamcommunity.com/id/';
64
        }
65
66 2
        $this->url = $this->url . $this->steamId . '/stats/' . $appId;
67
68
        // Set up the arguments
69
        $arguments = [
70 2
            'xml' => 1,
71 2
        ];
72
73
        try {
74
            // Get the client
75 2
            $client = $this->setUpXml($arguments);
76
77
            // Clean up the games
78 2
            $achievements = $this->convertToObjects($client->achievements->achievement);
79
80 1
            return $achievements;
81 1
        } catch (\Exception $e) {
82
            // In rare cases, games can force the use of a simplified name instead of an app ID
83
            // In these cases, try again with the name.
84 1
            if (is_int($appId)) {
85 1
                $app     = $this->app()->appDetails($appId);
86 1
                $appName = str_replace(' ', '', $app->first()->name);
0 ignored issues
show
Documentation Bug introduced by
The method first does not exist on object<Syntax\SteamApi\Steam\App>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
87
88 1
                return $this->GetPlayerAchievements($appName);
89
            }
90
91
            // If the name and ID fail, return null.
92 1
            return null;
93
        }
94
    }
95
96 1
    public function GetGlobalAchievementPercentagesForApp($gameId)
97
    {
98
        // Set up the api details
99 1
        $this->method  = __FUNCTION__;
100 1
        $this->version = 'v0002';
101
102
        // Set up the arguments
103
        $arguments = [
104 1
            'gameid' => $gameId,
105 1
            'l'      => 'english',
106 1
        ];
107
108
        // Get the client
109 1
        $client = $this->setUpClient($arguments)->achievementpercentages;
110
111 1
        return $client->achievements;
112
    }
113
114
    /**
115
     * @param $appId int Steam 64 id
116
     * @param $all   bool Return all stats when true and only achievements when false
117
     *
118
     * @return mixed
119
     */
120 2
    public function GetUserStatsForGame($appId, $all = false)
121
    {
122
        // Set up the api details
123 2
        $this->method  = __FUNCTION__;
124 2
        $this->version = 'v0002';
125
126
        // Set up the arguments
127
        $arguments = [
128 2
            'steamid' => $this->steamId,
129 2
            'appid'   => $appId,
130 2
            'l'       => 'english',
131 2
        ];
132
133
        // Get the client
134 2
        $client = $this->setUpClient($arguments)->playerstats;
135
136
        // Games like DOTA and CS:GO have additional stats here.  Return everything if they are wanted.
137 2
        if ($all === true) {
138 1
            return $client;
139
        }
140
141 1
        return $client->achievements;
142
    }
143
144
    /**
145
     * @param $appId
146
     *
147
     * @link https://wiki.teamfortress.com/wiki/WebAPI/GetSchemaForGame
148
     *
149
     * @return mixed
150
     */
151 1
    public function GetSchemaForGame($appId)
152
    {
153
        // Set up the api details
154 1
        $this->method  = __FUNCTION__;
155 1
        $this->version = 'v0002';
156
157
        // Set up the arguments
158
        $arguments = [
159 1
            'appid' => $appId,
160 1
            'l'     => 'english',
161 1
        ];
162
163
        // Get the client
164 1
        $client = $this->setUpClient($arguments);
165
166 1
        return $client;
167
    }
168
169 2
    protected function convertToObjects($achievements)
170
    {
171 2
        $cleanedAchievements = [];
172
173 2
        foreach ($achievements as $achievement) {
174 1
            $cleanedAchievements[] = new Achievement($achievement);
175 1
        }
176
177 1
        return $cleanedAchievements;
178
    }
179
}
180