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

AbstractProvider::parseSingleItem()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 16
ccs 0
cts 10
cp 0
rs 9.9666
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SteamMarketProviders\ParserManager\Parser\Provider;
6
7
use Fiber;
0 ignored issues
show
Bug introduced by
The type Fiber 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...
8
use PHPHtmlParser\Dom;
9
use PHPHtmlParser\Exceptions\ChildNotFoundException;
10
use PHPHtmlParser\Exceptions\CircularException;
11
use PHPHtmlParser\Exceptions\ContentLengthException;
12
use PHPHtmlParser\Exceptions\LogicalException;
13
use PHPHtmlParser\Exceptions\NotLoadedException;
14
use PHPHtmlParser\Exceptions\StrictException;
15
use stdClass;
16
use SteamMarketProviders\ParserManager\Builder\ParseRulesBuilder;
17
use SteamMarketProviders\ParserManager\Contract\StrategyInterface;
18
use SteamMarketProviders\ParserManager\Contract\UrlBuilderInterface;
19
use SteamMarketProviders\ParserManager\Exception\HttpException;
20
use SteamMarketProviders\ParserManager\Http\Http;
21
22
abstract class AbstractProvider
23
{
24
    /**
25
     * @var ParseRulesBuilder|null
26
     */
27
    private null|ParseRulesBuilder $parseRulesBuilder = null;
28
29
    /**
30
     * @var UrlBuilderInterface|null
31
     */
32
    private null|UrlBuilderInterface $urlBuilder = null;
33
34
    /**
35
     * @var Dom
36
     */
37
    private readonly Dom $dom;
38
39
    /**
40
     * @var Http
41
     */
42
    private readonly Http $http;
43
44
    public function __construct()
45
    {
46
        $this->http = new Http();
0 ignored issues
show
Bug introduced by
The property http is declared read-only in SteamMarketProviders\Par...ovider\AbstractProvider.
Loading history...
47
        $this->dom = new Dom();
0 ignored issues
show
Bug introduced by
The property dom is declared read-only in SteamMarketProviders\Par...ovider\AbstractProvider.
Loading history...
48
    }
49
50
    /**
51
     * @param int $page
52
     * @return UrlBuilderInterface
53
     */
54
    abstract protected function createUrl(int $page): UrlBuilderInterface;
55
56
    /**
57
     * @return ParseRulesBuilder
58
     */
59
    abstract protected function createParseRules(): ParseRulesBuilder;
60
61
    /**
62
     * @param UrlBuilderInterface $urlBuilder
63
     * @return void
64
     */
65
    public function setUrl(UrlBuilderInterface $urlBuilder): void
66
    {
67
        $this->urlBuilder = $urlBuilder;
68
    }
69
70
    /**
71
     * @param ParseRulesBuilder $parseRulesBuilder
72
     * @return void
73
     */
74
    public function setParseRules(ParseRulesBuilder $parseRulesBuilder): void
75
    {
76
        $this->parseRulesBuilder = $parseRulesBuilder;
77
    }
78
79
    /**
80
     * @param StrategyInterface $strategy
81
     * @return $this
82
     */
83
    public function setHttpStrategy(StrategyInterface $strategy): AbstractProvider
84
    {
85
        $this->http->setStrategy($strategy);
86
87
        return $this;
88
    }
89
90
    /**
91
     * @throws HttpException
92
     * @throws ChildNotFoundException
93
     * @throws CircularException
94
     * @throws StrictException
95
     * @throws NotLoadedException
96
     * @throws ContentLengthException
97
     * @throws LogicalException
98
     */
99
    final public function start(int $page): array
100
    {
101
        if (!$this->urlBuilder) {
102
            $this->urlBuilder = $this->createUrl($page);
103
        }
104
105
        if (!$this->parseRulesBuilder) {
106
            $this->parseRulesBuilder = $this->createParseRules();
107
        }
108
109
        $response = $this->http->sendRequest($this->urlBuilder->build());
110
        return $this->parseHtml($response);
111
    }
112
113
    /**
114
     * @param string $html
115
     * @return array
116
     * @throws ChildNotFoundException
117
     * @throws CircularException
118
     * @throws ContentLengthException
119
     * @throws LogicalException
120
     * @throws NotLoadedException
121
     * @throws StrictException
122
     */
123
    private function parseHtml(stdClass $html): array
124
    {
125
        $this->dom->loadStr($html->results_html);
126
127
        $contents = $this->dom->find('.market_listing_row_link');
128
129
        $data = [];
130
131
        foreach ($contents as $row) {
132
            $link = $row->getAttribute('href');
133
            $name = $row->find('.market_listing_item_name_block > span');
134
            $prices = $row->find('.market_listing_their_price .normal_price span');
135
136
            $data[] = [
137
                'link'=> $link,
138
                'name' => $name->innerHtml,
139
                'price' => $prices->innerHtml
140
            ];
141
        }
142
143
        $this->parseSingleItem($data);
144
145
        return $data;
146
    }
147
148
    private function parseSingleItem(array &$data): void
149
    {
150
        foreach ($data as $name => $info) {
151
            $response = $this->http->sendRequest($info['link']);
152
            $response = $response->results_html;
153
154
            $this->dom->loadStr($response);
155
156
            $data[$name]['info'] = [];
157
            $wrapper = $this->dom->find('.market_listing_iteminfo');
158
159
            $data[$name]['info']['large_image'] = $wrapper->find('.market_listing_largeimage > img')->getAttribute('src');
160
        }
161
162
        var_dump($data);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($data) looks like debug code. Are you sure you do not want to remove it?
Loading history...
163
        die();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
164
    }
165
}
166