SearchEndpointController::parsePlayerID()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 4
nop 1
dl 0
loc 16
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace Ps2alerts\Api\Controller\Endpoint\Search;
4
5
use Ps2alerts\Api\Controller\Endpoint\AbstractEndpointController;
6
use Ps2alerts\Api\Repository\Metrics\OutfitTotalRepository;
7
use Ps2alerts\Api\Repository\Metrics\PlayerTotalRepository;
8
use Ps2alerts\Api\Transformer\Search\OutfitSearchTransformer;
9
use Ps2alerts\Api\Transformer\Search\PlayerSearchTransformer;
10
use Psr\Http\Message\ServerRequestInterface;
11
use Psr\Http\Message\ResponseInterface;
12
13
class SearchEndpointController extends AbstractEndpointController
14
{
15
    /**
16
     * Construct
17
     *
18
     * @param OutfitSearchTransformer $outfitSearchTransformer
19
     * @param OutfitTotalRepository   $outfitTotalRepo
20
     * @param PlayerSearchTransformer $playerSearchTransformer
21
     * @param PlayerTotalRepository   $playerTotalRepo
22
     */
23
    public function __construct(
24
        OutfitTotalRepository   $outfitTotalRepo,
25
        PlayerTotalRepository   $playerTotalRepo,
26
        OutfitSearchTransformer $outfitSearchTransformer,
27
        PlayerSearchTransformer $playerSearchTransformer
28
    ) {
29
        $this->playerRepository        = $playerTotalRepo;
0 ignored issues
show
Bug introduced by
The property playerRepository does not seem to exist. Did you mean repository?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
30
        $this->outfitRepository        = $outfitTotalRepo;
0 ignored issues
show
Bug introduced by
The property outfitRepository does not seem to exist. Did you mean repository?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
31
        $this->playerSearchTransformer = $playerSearchTransformer;
0 ignored issues
show
Bug introduced by
The property playerSearchTransformer does not seem to exist. Did you mean transformer?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
32
        $this->outfitSearchTransformer = $outfitSearchTransformer;
0 ignored issues
show
Bug introduced by
The property outfitSearchTransformer does not seem to exist. Did you mean transformer?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
33
    }
34
35
    /**
36
     * Endpoint to return potential players based on search term
37
     *
38
     * @param  ServerRequestInterface $request
39
     * @param  ResponseInterface      $response
40
     * @param  array                  $args
41
     *
42
     * @return ResponseInterface
43
     */
44
    public function getPlayersByTerm(ServerRequestInterface $request, ResponseInterface $response, array $args)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $response is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
45
    {
46
        // If a valid player name we're searching on
47
        $this->parsePlayerName($args['term']);
48
        $players = $this->searchForPlayer($args['term']);
49
50
        if (! empty($players)) {
51
            return $this->respond(
52
                'collection',
53
                $players,
54
                $this->playerSearchTransformer
0 ignored issues
show
Bug introduced by
The property playerSearchTransformer does not seem to exist. Did you mean transformer?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
55
            );
56
        }
57
58
        return $this->respondWithError('Player could not be found', self::CODE_EMPTY);
59
    }
60
61
    /**
62
     * Endpoint to return potential players based on search term
63
     *
64
     * @param  ServerRequestInterface $request
65
     * @param  ResponseInterface      $response
66
     * @param  array                  $args
67
     *
68
     * @return ResponseInterface
69
     */
70
    public function getOutfitsByTerm(ServerRequestInterface $request, ResponseInterface $response, array $args)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $response is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
71
    {
72
        $name = urldecode($args['term']); // Spaces will have to URL encoded
73
74
        $this->parseOutfitName($name);
75
76
        if (! empty($outfits)) {
0 ignored issues
show
Bug introduced by
The variable $outfits seems to never exist, and therefore empty should always return true. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
77
            return $this->respond(
78
                'collection',
79
                $outfits,
80
                $this->outfitSearchTransformer
0 ignored issues
show
Bug introduced by
The property outfitSearchTransformer does not seem to exist. Did you mean transformer?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
81
            );
82
        }
83
84
        return $this->respondWithError('Outfit could not be found', self::CODE_EMPTY);
85
    }
86
87
    /**
88
     * Takes a player name and searches for it
89
     *
90
     * @param  string $term
91
     *
92
     * @return array
93
     */
94
    public function searchForPlayer($term)
95
    {
96
        $query = $this->playerRepository->newQuery();
0 ignored issues
show
Bug introduced by
The property playerRepository does not seem to exist. Did you mean repository?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
97
        $query->cols(['*']);
98
        $query->where('playerName LIKE :term');
99
        $query->bindValue('term', "%{$term}%");
100
101
        return $this->playerRepository->fireStatementAndReturn($query);
0 ignored issues
show
Bug introduced by
The property playerRepository does not seem to exist. Did you mean repository?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
102
    }
103
104
    /**
105
     * Takes a outfit name and searches for it
106
     *
107
     * @param  string $term
108
     *
109
     * @return array
110
     */
111
    public function searchForOutfit($term)
112
    {
113
        $query = $this->outfitRepository->newQuery();
0 ignored issues
show
Bug introduced by
The property outfitRepository does not seem to exist. Did you mean repository?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
114
        $query->cols(['*']);
115
        $query->where("outfitTag LIKE :term");
116
        $query->bindValue('term', "%{$term}%");
117
118
        $data = $this->outfitRepository->fireStatementAndReturn($query);
0 ignored issues
show
Bug introduced by
The property outfitRepository does not seem to exist. Did you mean repository?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
119
120
        if (empty($data)) {
121
            $query = $this->outfitRepository->newQuery();
0 ignored issues
show
Bug introduced by
The property outfitRepository does not seem to exist. Did you mean repository?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
122
            $query->cols(['*']);
123
            $query->where("outfitName LIKE :term");
124
            $query->bindValue('term', "%{$term}%");
125
126
            $data = $this->outfitRepository->fireStatementAndReturn($query);
0 ignored issues
show
Bug introduced by
The property outfitRepository does not seem to exist. Did you mean repository?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
127
        }
128
129
        return $data;
130
    }
131
132
    /**
133
     * Parses a player name and makes sure it's valid
134
     *
135
     * @param  String $name
136
     *
137
     * @return ResponseInterface|boolean
138
     */
139 View Code Duplication
    public function parsePlayerName($name)
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...
140
    {
141
        if (empty($name)) {
142
            return $this->respondWithError('Player name needs to be present.', self::CODE_WRONG_ARGS);
143
        }
144
145
        if (strlen($name) > 24) {
146
            return $this->respondWithError('Player names cannot be longer than 24 characters.', self::CODE_WRONG_ARGS);
147
        }
148
149
        return true;
150
    }
151
152
    /**
153
     * Parses a outfit name and makes sure it's valid
154
     *
155
     * @param  String $name
156
     *
157
     * @return ResponseInterface|boolean
158
     */
159 View Code Duplication
    public function parseOutfitName($name)
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...
160
    {
161
        if (empty($name)) {
162
            return $this->respondWithError('Outfit name needs to be present.', self::CODE_WRONG_ARGS);
163
        }
164
165
        if (strlen($name) > 32) {
166
            return $this->respondWithError('Outfit names cannot be longer than 32 characters.', self::CODE_WRONG_ARGS);
167
        }
168
169
        return true;
170
    }
171
172
    /**
173
     * Runs checks on the player ID
174
     *
175
     * @param  string $id
176
     *
177
     * @return ResponseInterface|boolean
178
     */
179
    public function parsePlayerID($id)
180
    {
181
        if (empty($id)) {
182
            return $this->respondWithError('Player ID needs to be present.', self::CODE_WRONG_ARGS);
183
        }
184
185
        if (strlen($id > 19)) {
186
            return $this->respondWithError('Player ID cannot be longer than 19 characters.', self::CODE_WRONG_ARGS);
187
        }
188
189
        if (! is_numeric($id)) {
190
            return $this->respondWithError('Player ID must be numeric.', self::CODE_WRONG_ARGS);
191
        }
192
193
        return true;
194
    }
195
}
196