Passed
Push — main ( e7c7b0...8ad6bb )
by Yevhenii
02:45
created

Http::setStrategy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
cc 1
rs 10
ccs 3
cts 3
cp 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SteamMarketProviders\ParserManager\Http;
6
7
use Exception;
8
use stdClass;
9
use SteamMarketProviders\ParserManager\Contract\Http\HttpExceptionInterface;
10
use SteamMarketProviders\ParserManager\Contract\StrategyInterface;
11
use SteamMarketProviders\ParserManager\Exception\Http\URLNotSetException;
0 ignored issues
show
Bug introduced by
The type SteamMarketProviders\Par...Http\URLNotSetException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use SteamMarketProviders\ParserManager\Exception\HttpException;
13
use SteamMarketProviders\ParserManager\Exception\InvalidArgumentException;
14
use SteamMarketProviders\ParserManager\Http\Strategy\GuzzleStrategy;
15
16
final class Http
17
{
18
    private string $url;
19
20
    /**
21
     * @param StrategyInterface|null $strategy
22
     */
23 8
    public function __construct(private null|StrategyInterface $strategy = null)
24
    {
25 8
        if (!$this->strategy) {
26 8
            $this->strategy = new GuzzleStrategy();
27
        }
28
    }
29
30
    /**
31
     * @param string $url
32
     * @return $this
33
     * @throws InvalidArgumentException
34
     */
35 2
    public function setUrl(string $url): Http
36
    {
37 2
        if (!filter_var($url, FILTER_VALIDATE_URL)) {
38 1
            throw new InvalidArgumentException(
39 1
                sprintf('The url: "%s" is not in a valid format', $url)
40
            );
41
        }
42
43 1
        $this->url = $url;
44
45 1
        return $this;
46
    }
47
48
    /**
49
     * @return string
50
     */
51
    public function getUrl(): string
52
    {
53
        return $this->url;
54
    }
55
56
    /**
57
     * @param StrategyInterface $strategy
58
     * @return $this
59
     */
60 1
    public function setStrategy(StrategyInterface $strategy): Http
61
    {
62 1
        $this->strategy = $strategy;
63
64 1
        return $this;
65
    }
66
67
    /**
68
     * @return StrategyInterface
69
     */
70 2
    public function getStrategy(): StrategyInterface
71
    {
72 2
        return $this->strategy;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->strategy could return the type null which is incompatible with the type-hinted return SteamMarketProviders\Par...tract\StrategyInterface. Consider adding an additional type-check to rule them out.
Loading history...
73
    }
74
75
    /**
76
     * @return stdClass
77
     * @throws HttpException
78
     * @throws HttpExceptionInterface
79
     * @throws URLNotSetException
80
     */
81 1
    public function sendRequest(): stdClass
82
    {
83
        try {
84 1
            $response = $this->strategy->sendRequest($this->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

84
            /** @scrutinizer ignore-call */ 
85
            $response = $this->strategy->sendRequest($this->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...
85
86
            $code = $response->getStatus();
87
            if ($code !== 200) {
88
                throw new HttpException("");
89
            }
90
91
            $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

91
            $response = json_decode(/** @scrutinizer ignore-type */ $response->getBody());
Loading history...
92
93
            if (!$response->success) {
94
                throw new HttpException("");
95
            }
96 1
        } catch (HttpExceptionInterface $httpException) {
97
            throw new $httpException();
98 1
        } catch (Exception $e) {
99
            throw new HttpException($e->getMessage());
100
        }
101
102
        return $response;
103
    }
104
}
105