These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | |||
4 | namespace Gbowo\Adapter\Paystack\Plugin; |
||
5 | |||
6 | use Gbowo\Plugin\AbstractFetchPlan; |
||
7 | use function GuzzleHttp\json_decode; |
||
8 | use Psr\Http\Message\ResponseInterface; |
||
9 | use Gbowo\Exception\InvalidHttpResponseException; |
||
10 | |||
11 | class FetchPlan extends AbstractFetchPlan |
||
12 | { |
||
13 | |||
14 | /** |
||
15 | * @var string The relative link for fetching a certain plan |
||
16 | */ |
||
17 | const FETCH_PLAN_LINK = '/plan/:identifier'; |
||
18 | |||
19 | protected $baseUrl; |
||
20 | |||
21 | public function __construct(string $baseUrl) |
||
22 | { |
||
23 | $this->baseUrl = $baseUrl; |
||
24 | } |
||
25 | |||
26 | /** |
||
27 | * @param array ...$args |
||
28 | * @return mixed |
||
29 | * @throws \Gbowo\Exception\InvalidHttpResponseException if ann http response of 200 isn't returned |
||
30 | */ |
||
31 | View Code Duplication | public function handle(...$args) |
|
0 ignored issues
–
show
|
|||
32 | { |
||
33 | |||
34 | $link = $this->baseUrl . str_replace(":identifier", $args[0], self::FETCH_PLAN_LINK); |
||
35 | |||
36 | /** |
||
37 | * @var ResponseInterface $response |
||
38 | */ |
||
39 | $response = $this->adapter->getHttpClient() |
||
40 | ->get($link); |
||
41 | |||
42 | if (200 !== $response->getStatusCode()) { |
||
43 | throw new InvalidHttpResponseException( |
||
44 | "Expected 200. Got {$response->getStatusCode()} instead" |
||
45 | ); |
||
46 | } |
||
47 | |||
48 | return json_decode($response->getBody(), true)['data']; |
||
49 | } |
||
50 | } |
||
51 |
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.