AveragePriceScrapper   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 24
rs 10
c 0
b 0
f 0
ccs 9
cts 9
cp 1
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A getAverageData() 0 13 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