Passed
Push — main ( 255c0f...66f102 )
by Yevhenii
02:14
created

Http   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 17
c 1
b 0
f 0
dl 0
loc 48
ccs 0
cts 19
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A handlerRequest() 0 20 4
A __construct() 0 4 2
A sendRequest() 0 3 1
A setStrategy() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SteamMarketProviders\ParserManager\Http;
6
7
use stdClass;
8
use SteamMarketProviders\ParserManager\Contract\StrategyInterface;
9
use SteamMarketProviders\ParserManager\Exception\HttpException;
10
use SteamMarketProviders\ParserManager\Http\Strategy\GuzzleStrategy;
11
use Throwable;
12
13
final class Http
14
{
15
    /**
16
     * @param StrategyInterface|null $strategy
17
     */
18
    public function __construct(private null|StrategyInterface $strategy = null)
19
    {
20
        if (!$this->strategy) {
21
            $this->strategy = new GuzzleStrategy();
22
        }
23
    }
24
25
    /**
26
     * @param StrategyInterface $strategy
27
     * @return $this
28
     */
29
    public function setStrategy(StrategyInterface $strategy): Http
30
    {
31
        $this->strategy = $strategy;
32
33
        return $this;
34
    }
35
36
    public function sendRequest(string $url): stdClass
37
    {
38
        return $this->handlerRequest($url);
39
    }
40
41
    private function handlerRequest(string $url): stdClass
42
    {
43
        try {
44
            $response = $this->strategy->sendRequest($url);
0 ignored issues
show
Bug introduced by
The method sendRequest() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

44
            /** @scrutinizer ignore-call */ 
45
            $response = $this->strategy->sendRequest($url);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
45
46
            $code = $response->getStatus();
47
            if ($code !== 200) {
48
                throw new HttpException("");
49
            }
50
51
            $response = json_decode($response->getBody());
0 ignored issues
show
Bug introduced by
It seems like $response->getBody() can also be of type callable and resource; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

51
            $response = json_decode(/** @scrutinizer ignore-type */ $response->getBody());
Loading history...
52
53
            if (!$response->success) {
54
                throw new HttpException("");
55
            }
56
        } catch (Throwable $throwable) {
57
            throw new HttpException($throwable->getMessage());
58
        }
59
60
        return $response;
61
    }
62
}
63