PlayerProfileTransformer   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 247
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 14

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 247
rs 10
c 1
b 0
f 0
wmc 20
lcom 1
cbo 14

8 Methods

Rating   Name   Duplication   Size   Complexity  
C includeCensus() 0 52 8
A includeOutfit() 0 5 1
B includeVehicles() 0 33 3
B includeWeapons() 0 25 3
A __construct() 0 11 1
A transform() 0 10 1
A includeInvolvement() 0 6 1
B includeMetrics() 0 25 2
1
<?php
2
3
namespace Ps2alerts\Api\Transformer\Profiles;
4
5
use League\Fractal\TransformerAbstract;
6
use Ps2alerts\Api\Contract\HttpClientAwareInterface;
7
use Ps2alerts\Api\Contract\HttpClientAwareTrait;
8
use Ps2alerts\Api\Repository\Metrics\OutfitTotalRepository;
9
use Ps2alerts\Api\Repository\Metrics\PlayerRepository;
10
use Ps2alerts\Api\Repository\Metrics\VehicleRepository;
11
use Ps2alerts\Api\Repository\Metrics\WeaponRepository;
12
use Ps2alerts\Api\Transformer\Profiles\PlayerCensusTransformer;
13
use Ps2alerts\Api\Transformer\Profiles\PlayerInvolvementTransformer;
14
use Ps2alerts\Api\Transformer\Profiles\PlayerMetricsTransformer;
15
use Ps2alerts\Api\Transformer\Profiles\PlayerOutfitTransformer;
16
17
class PlayerProfileTransformer extends TransformerAbstract implements HttpClientAwareInterface
18
{
19
    use HttpClientAwareTrait;
20
21
    /**
22
     * List of available includes to this resource
23
     *
24
     * @var array
25
     */
26
    protected $availableIncludes = [
27
        'census',
28
        'involvement',
29
        'metrics',
30
        'outfit',
31
        //'vehicles',
32
        'weapons'
33
    ];
34
35
    protected $outfitTotalRepo;
36
    protected $playerRepo;
37
    protected $vehicleRepo;
38
    protected $weaponRepo;
39
40
    public function __construct(
41
        OutfitTotalRepository $outfitTotalRepo,
42
        PlayerRepository      $playerRepo,
43
        VehicleRepository     $vehicleRepo,
44
        WeaponRepository      $weaponRepo
45
    ) {
46
        $this->outfitTotalRepo = $outfitTotalRepo;
47
        $this->playerRepo      = $playerRepo;
48
        $this->vehicleRepo     = $vehicleRepo;
49
        $this->weaponRepo      = $weaponRepo;
50
    }
51
52
    /**
53
     * The transform method required by Fractal to parse the data and return proper typing and fields.
54
     *
55
     * @param  array $data Data to transform
56
     *
57
     * @return array
58
     */
59
    public function transform($data)
60
    {
61
        return [
62
            'id'      => (string) $data['playerID'], // Bigint
63
            'name'    => (string) $data['playerName'],
64
            'outfit'  => (string) $data['playerOutfit'], // Bigint
65
            'faction' => (int) $data['playerFaction'],
66
            'server'  => (int) $data['playerServer']
67
        ];
68
    }
69
70
    /**
71
     * Grab Census only info
72
     *
73
     * @param  array $data
74
     *
75
     * @return \League\Fractal\Resource\Collection
76
     */
77
    public function includeCensus($data)
78
    {
79
        $client = $this->getHttpClientDriver();
80
81
        $namespace = 'ps2:v2';
82
83
        if ($data['playerServer'] >= 2000) {
84
            $namespace = 'ps2ps4eu';
85
        } elseif ($data['playerServer'] >= 1000) {
86
            $namespace = 'ps2ps4us';
87
        }
88
89
        $response = $client->get(
90
            "https://census.daybreakgames.com/s:planetside2alertstats/get/{$namespace}/character/{$data['playerID']}?c:resolve=stat,stat_by_faction"
91
        );
92
93
        $json = json_decode($response->getBody()->getContents(), true);
94
95
        $character = $json['character_list'][0];
96
97
        $statCount = count($character['stats']['stat']);
98
        $statFactionCount = count($character['stats']['stat_by_faction']);
99
100
        $character['kills']     = 0;
101
        $character['deaths']    = 0;
102
        $character['headshots'] = 0;
103
104
        for ($i = 0; $i < $statFactionCount; $i++) {
105
            $row = $character['stats']['stat_by_faction'][$i];
106
107
            if ($row['stat_name'] === 'weapon_kills') {
108
                $character['kills'] = ($row['value_forever_nc'] + $row['value_forever_tr']);
109
            }
110
111
            if ($row['stat_name'] === 'weapon_headshots') {
112
                $character['headshots'] = ($row['value_forever_nc'] + $row['value_forever_tr']);
113
            }
114
        }
115
116
        for ($i = 0; $i < $statCount; $i++) {
117
            $row = $character['stats']['stat'][$i];
118
119
            if ($row['stat_name'] === 'weapon_deaths') {
120
                $character['deaths'] = $row['value_forever'];
121
            }
122
        }
123
124
        // Removes daft ".0" from the end of the login string...
125
        $character['times']['last_login_date'] = str_replace('.0', '', $character['times']['last_login_date']);
126
127
        return $this->item($character, new PlayerCensusTransformer);
0 ignored issues
show
Documentation introduced by
new \Ps2alerts\Api\Trans...ayerCensusTransformer() is of type object<Ps2alerts\Api\Tra...layerCensusTransformer>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
128
    }
129
130
    /**
131
     * Get Alert involvement & metrics
132
     *
133
     * @param  array $data
134
     *
135
     * @return \League\Fractal\Resource\Collection
136
     */
137
    public function includeInvolvement($data)
138
    {
139
        $data = $this->playerRepo->readAllByIdWithArchive($data['playerID'], 'playerID');
140
141
        return $this->collection($data, new PlayerInvolvementTransformer);
0 ignored issues
show
Documentation introduced by
new \Ps2alerts\Api\Trans...nvolvementTransformer() is of type object<Ps2alerts\Api\Tra...InvolvementTransformer>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
142
    }
143
144
    /**
145
     * Gets Metrics for a player
146
     *
147
     * @param  array $player
0 ignored issues
show
Bug introduced by
There is no parameter named $player. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
148
     *
149
     * @return array
150
     */
151
    public function includeMetrics($data)
152
    {
153
        $metrics = [
154
            'kills'     => 0,
155
            'deaths'    => 0,
156
            'teamkills' => 0,
157
            'suicides'  => 0,
158
            'headshots' => 0
159
        ];
160
161
        $alerts = $this->playerRepo->readAllByIdWithArchive($data['playerID'], 'playerID');
162
        $count = count($alerts);
163
        $metrics['involvement'] = $count;
164
165
        // Calculate metrics
166
        for ($i = 0; $i < $count; $i++) {
167
            $metrics['kills']     = $metrics['kills'] + $alerts[$i]['playerKills'];
168
            $metrics['deaths']    = $metrics['deaths'] + $alerts[$i]['playerDeaths'];
169
            $metrics['teamkills'] = $metrics['teamkills'] + $alerts[$i]['playerTeamKills'];
170
            $metrics['suicides']  = $metrics['suicides'] + $alerts[$i]['playerSuicides'];
171
            $metrics['headshots'] = $metrics['headshots'] + $alerts[$i]['headshots'];
172
        }
173
174
        return $this->item($metrics, new PlayerMetricsTransformer);
0 ignored issues
show
Documentation introduced by
new \Ps2alerts\Api\Trans...yerMetricsTransformer() is of type object<Ps2alerts\Api\Tra...ayerMetricsTransformer>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
175
    }
176
177
    /**
178
     * Get Outfit info and metrics
179
     *
180
     * @param  array $data
181
     *
182
     * @return \League\Fractal\Resource\Collection
183
     */
184
    public function includeOutfit($data)
185
    {
186
        $data = $this->outfitTotalRepo->readSingleById($data['playerOutfit'], 'primary');
187
        return $this->item($data, new PlayerOutfitTransformer);
0 ignored issues
show
Documentation introduced by
new \Ps2alerts\Api\Trans...ayerOutfitTransformer() is of type object<Ps2alerts\Api\Tra...layerOutfitTransformer>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
188
    }
189
190
    /**
191
     * Get Vehicle info and metrics
192
     *
193
     * @param  array $data
194
     *
195
     * @return \League\Fractal\Resource\Collection
196
     */
197
    public function includeVehicles($data)
198
    {
199
        $metrics = [];
200
        $data = $this->vehicleRepo->readAllByIdWithArchive($data['playerID'], 'playerID');
201
        $count = count($data);
202
203
        // Calculate metrics
204
        for ($i = 0; $i < $count; $i++) {
205
            $vehicleID = $data[$i]['vehicleID'];
206
            if (empty($metrics[$vehicleID])) {
207
                $metrics[$vehicleID] = [
208
                    'id'        => $vehicleID,
209
                    'kills'     => 0,
210
                    'killsInf'  => 0,
211
                    'killsVeh'  => 0,
212
                    'deaths'    => 0,
213
                    'deathsInf' => 0,
214
                    'deathsVeh' => 0,
215
                    'bails'     => 0
216
                ];
217
            }
218
219
            $metrics[$vehicleID]['kills']     = $metrics[$vehicleID]['kills'] + $data[$i]['killCount'];
220
            $metrics[$vehicleID]['killsInf']  = $metrics[$vehicleID]['killsInf'] + $data[$i]['killICount'];
221
            $metrics[$vehicleID]['killsVeh']  = $metrics[$vehicleID]['killsVeh'] + $data[$i]['killVCount'];
222
            $metrics[$vehicleID]['deaths']    = $metrics[$vehicleID]['deaths'] + $data[$i]['deathCount'];
223
            $metrics[$vehicleID]['deathsInf'] = $metrics[$vehicleID]['deathsInf'] + $data[$i]['deathICount'];
224
            $metrics[$vehicleID]['deathsVeh'] = $metrics[$vehicleID]['deathsVeh'] + $data[$i]['deathVCount'];
225
            $metrics[$vehicleID]['bails']     = $metrics[$vehicleID]['bails'] + $data[$i]['bails'];
226
        }
227
228
        return $this->collection($metrics, new PlayerVehicleTransformer);
0 ignored issues
show
Documentation introduced by
new \Ps2alerts\Api\Trans...yerVehicleTransformer() is of type object<Ps2alerts\Api\Tra...ayerVehicleTransformer>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
229
    }
230
231
    /**
232
     * Get Weapon info and metrics
233
     *
234
     * @param  array $data
235
     *
236
     * @return \League\Fractal\Resource\Collection
237
     */
238
    public function includeWeapons($data)
239
    {
240
        $metrics = [];
241
        $data = $this->weaponRepo->readAllByIdWithArchive($data['playerID'], 'playerID');
242
        $count = count($data);
243
244
        // Calculate metrics
245
        for ($i = 0; $i < $count; $i++) {
246
            $weaponID = $data[$i]['weaponID'];
247
            if (empty($metrics[$weaponID])) {
248
                $metrics[$weaponID] = [
249
                    'id'        => $weaponID,
250
                    'kills'     => 0,
251
                    'headshots' => 0,
252
                    'teamkills' => 0
253
                ];
254
            }
255
256
            $metrics[$weaponID]['kills']     = $metrics[$weaponID]['kills'] + $data[$i]['killCount'];
257
            $metrics[$weaponID]['headshots'] = $metrics[$weaponID]['headshots'] + $data[$i]['headshots'];
258
            $metrics[$weaponID]['teamkills'] = $metrics[$weaponID]['teamkills'] + $data[$i]['teamkills'];
259
        }
260
261
        return $this->collection($metrics, new PlayerWeaponTransformer);
0 ignored issues
show
Documentation introduced by
new \Ps2alerts\Api\Trans...ayerWeaponTransformer() is of type object<Ps2alerts\Api\Tra...layerWeaponTransformer>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
262
    }
263
}
264