Test Failed
Push — master ( 3b06f4...e1fadc )
by Nils
02:12
created

ArticleCrawler::extractStock()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 7
ccs 0
cts 0
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Sesame\Crawler;
4
5
use Sesame\Model\Article;
6
use \Symfony\Component\DomCrawler\Crawler as DomCrawler;
7
8
/**
9
 * Class ArticleCrawler
10
 *
11
 * @package Sesame\Crawler
12
 */
13
class ArticleCrawler extends Crawler
14
{
15
16
    /**
17
     * ArticleCrawler constructor.
18
     */
19
    public function __construct()
20
    {
21
        parent::__construct();
22
    }
23
24
    /**
25
     * crawlArticle
26
     *
27
     * @param string $responseBody
28
     * @param bool $crawlVariations
29
     * @return Article
30
     */
31
    public function crawlArticle(string $responseBody, bool $crawlVariations): Article
0 ignored issues
show
Unused Code introduced by
The parameter $crawlVariations is not used and could be removed. ( Ignorable by Annotation )

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

31
    public function crawlArticle(string $responseBody, /** @scrutinizer ignore-unused */ bool $crawlVariations): Article

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
32
    {
33
        $articleCrawler = new DomCrawler($responseBody);
34
        $article = new Article();
35
36
        // extract name of the article
37
        $nameHtml = $articleCrawler->filter('h1.product-name');
38
        $article->setName($nameHtml->text());
39
40
        // extract ammount of total orders
41
        $article->setOrders($this->extractOrders($articleCrawler));
42
43
        // extract amount of articles in stock
44
        $article->setStock($this->extractStock($articleCrawler));
45
46
        return $article;
47
    }
48
49
    /**
50
     * @param DomCrawler $articleCrawler
51
     * @return int
52
     */
53
    protected function extractOrders(DomCrawler $articleCrawler): int
54
    {
55
        $ordersHtml = $articleCrawler->filter('#j-order-num');
56
        $ordersString = $ordersHtml->text();
57
        $ordersStringParts = explode(' ', $ordersString);
58
59
        return (int) $ordersStringParts[0];
60
    }
61
62
    /**
63
     * @param DomCrawler $articleCrawler
64
     * @return int
65
     */
66
    protected function extractStock(DomCrawler $articleCrawler): int
67
    {
68
        $stockAmountHtml = $articleCrawler->filter('#j-sell-stock-num');
69
        $stockAmountString = $stockAmountHtml->text();
70
        $stockAmountStringParts = explode(' ', $stockAmountString);
71
72
        return (int) $stockAmountStringParts[0];
73
    }
74
}
75