Stats::GetPlayerAchievementsAPI()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 0
cts 12
cp 0
rs 9.488
c 0
b 0
f 0
cc 3
nc 2
nop 1
crap 12
1
<?php
2
3
namespace Syntax\SteamApi\Steam\User;
4
5
use Exception;
6
use Syntax\SteamApi\Client;
7
use Syntax\SteamApi\Containers\Achievement;
8
9
class Stats extends Client
10
{
11 6
    public function __construct($steamId)
12
    {
13 6
        parent::__construct();
14 6
        $this->interface = 'ISteamUserStats';
15 6
        $this->steamId   = $steamId;
16 6
    }
17
18
    /**
19
     * @deprecated
20
     *
21
     * @param $appId
22
     *
23
     * @return array
24
     */
25
    public function GetPlayerAchievementsAPI($appId)
26
    {
27
        // Set up the api details
28
        $this->method  = 'GetPlayerAchievementsAPI';
29
        $this->version = 'v0001';
30
31
        // Set up the arguments
32
        $arguments = [
33
            'steamid' => $this->steamId,
34
            'appid'   => $appId,
35
            'l'       => 'english',
36
        ];
37
38
        // Get the client
39
        $stats = $this->GetSchemaForGame($appId);
40
41
        // Make sure the game has achievements
42
        if ($stats == null || $stats->game->availableGameStats->achievements == null) {
43
            return null;
44
        }
45
46
        $client = $this->setUpClient($arguments)->playerstats;
47
        $stats  = $stats->game->availableGameStats->achievements;
48
49
        // Clean up the games
50
        return $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...
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
        } 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 2
            'xml' => 1,
70
        ];
71
72
        try {
73
            // Get the client
74 2
            $client = $this->setUpXml($arguments);
75
76
            // Clean up the games
77 2
            return $this->convertToObjects($client->achievements->achievement);
78 1
        } catch (Exception $e) {
79
            // In rare cases, games can force the use of a simplified name instead of an app ID
80
            // In these cases, try again by grabbing the redirected url.
81 1
            if (is_int($appId)) {
82 1
                $this->getRedirectUrl();
83
84
                try {
85
                    // Get the client
86 1
                    $client = $this->setUpXml($arguments);
87
88
                    // Clean up the games
89 1
                    return $this->convertToObjects($client->achievements->achievement);
90 1
                } catch (Exception $exception) {
91 1
                    return null;
92
                }
93
            }
94
95
            // If the name and ID fail, return null.
96
            return null;
97
        }
98
    }
99
100 1 View Code Duplication
    public function GetGlobalAchievementPercentagesForApp($gameId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
101
    {
102
        // Set up the api details
103 1
        $this->method  = __FUNCTION__;
104 1
        $this->version = 'v0002';
105
106
        // Set up the arguments
107
        $arguments = [
108 1
            'gameid' => $gameId,
109 1
            'l'      => 'english',
110
        ];
111
112
        // Get the client
113 1
        $client = $this->setUpClient($arguments)->achievementpercentages;
114
115 1
        return $client->achievements;
116
    }
117
118
    /**
119
     * @param $appId int Steam 64 id
120
     * @param $all   bool Return all stats when true and only achievements when false
121
     *
122
     * @return mixed
123
     */
124 2
    public function GetUserStatsForGame($appId, $all = false)
125
    {
126
        // Set up the api details
127 2
        $this->method  = __FUNCTION__;
128 2
        $this->version = 'v0002';
129
130
        // Set up the arguments
131
        $arguments = [
132 2
            'steamid' => $this->steamId,
133 2
            'appid'   => $appId,
134 2
            'l'       => 'english',
135
        ];
136
137
        // Get the client
138 2
        $client = $this->setUpClient($arguments)->playerstats;
139
140
        // Games like DOTA and CS:GO have additional stats here.  Return everything if they are wanted.
141
        if ($all === true) {
142
            return $client;
143
        }
144
145
        return $client->achievements;
146
    }
147
148
    /**
149
     * @param $appId
150
     *
151
     * @link https://wiki.teamfortress.com/wiki/WebAPI/GetSchemaForGame
152
     *
153
     * @return mixed
154
     */
155 1 View Code Duplication
    public function GetSchemaForGame($appId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
156
    {
157
        // Set up the api details
158 1
        $this->method  = __FUNCTION__;
159 1
        $this->version = 'v0002';
160
161
        // Set up the arguments
162
        $arguments = [
163 1
            'appid' => $appId,
164 1
            'l'     => 'english',
165
        ];
166
167
        // Get the client
168 1
        return $this->setUpClient($arguments);
169
    }
170
171 1
    protected function convertToObjects($achievements)
172
    {
173 1
        $cleanedAchievements = [];
174
175 1
        foreach ($achievements as $achievement) {
176 1
            $cleanedAchievements[] = new Achievement($achievement);
177
        }
178
179 1
        return $cleanedAchievements;
180
    }
181
}
182