AveragePriceScrapper::getAverageData()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 1
dl 0
loc 13
rs 10
c 0
b 0
f 0
ccs 9
cts 9
cp 1
crap 2
1
<?php
2
/**
3
 * File: AveragePriceScrapper.php
4
 *
5
 * @author      Maciej Sławik <[email protected]>
6
 * Github:      https://github.com/maciejslawik
7
 */
8
9
namespace MSlwk\Otomoto\App\Stats\Scrapper\Data;
10
11
use Symfony\Component\DomCrawler\Crawler;
12
13
/**
14
 * Class AveragePriceScrapper
15
 * @package MSlwk\Otomoto\App\Stats\Scrapper\Data
16
 */
17
class AveragePriceScrapper implements AverageDataScrapperInterface
18
{
19
    const HTML_PRICE_SELECTOR = 'span.offer-price__number';
20
    const HTML_PRICE_DETAILS_SELECTOR = 'span.offer-price__details';
21
    const HTML_PRICE_NET_SYMBOL = 'Netto';
22
    const CAR_PRICE_TAX = 1.23;
23
24
    /**
25
     * @param Crawler $htmlCrawler
26
     * @return float
27
     */
28 1
    public function getAverageData(Crawler $htmlCrawler): float
29
    {
30 1
        $priceValueNode = $htmlCrawler->filter(self::HTML_PRICE_SELECTOR)->first()->getNode(0);
31 1
        $priceValueNodeText = $priceValueNode->textContent;
32 1
        $price = (float)str_replace(' ', '', trim($priceValueNodeText));
33
34 1
        $priceDetailsNode = $htmlCrawler->filter(self::HTML_PRICE_DETAILS_SELECTOR)->first()->getNode(0);
35 1
        $priceValueNodeText = $priceDetailsNode->textContent;
36 1
        if (strpos($priceValueNodeText, self::HTML_PRICE_NET_SYMBOL) !== false) {
37 1
            $price = $price * self::CAR_PRICE_TAX;
38
        }
39
40 1
        return $price;
41
    }
42
}
43