Completed
Push — master ( 939cff...94561e )
by Amine
21:29
created

Scraper::getCategoryById()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 29
rs 8.8571
cc 1
eloc 18
nc 1
nop 1
1
<?php
2
3
namespace AmineBenHariz\TunisianetScraper;
4
5
use AmineBenHariz\TunisianetScraper\Entity\Category;
6
use AmineBenHariz\TunisianetScraper\Entity\Product;
7
use Goutte\Client;
8
use Symfony\Component\DomCrawler\Crawler;
9
10
/**
11
 * Class Scraper
12
 * @package AmineBenHariz\TunisianetScraper
13
 */
14
class Scraper
15
{
16
    /**
17
     * @var Client
18
     */
19
    private $client;
20
21
    /**
22
     * Scraper constructor.
23
     */
24
    public function __construct()
25
    {
26
        $this->client = new Client();
27
    }
28
29
    /**
30
     * @param int $productId
31
     * @return Product
32
     */
33
    public function getProductById($productId)
34
    {
35
        $url = 'http://www.tunisianet.com.tn/x/'.$productId.'-x.html';
36
37
        $crawler = $this->client->request('GET', $url);
38
39
        $url = $this->client->getHistory()->current()->getUri();
40
41
        $title = $crawler->filter('h1 > acronym')->text();
42
        $reference = $crawler->filter('#hit_ref')->attr('value');
43
        $priceTag = $crawler->filter('#our_price_display')->text();
44
        $price =  floatval(str_replace(',', '.', $priceTag));
45
        $description = $crawler->filter('#short_description_content')->text();
46
        $available = $crawler->filter('form#buy_block > div')->attr('class') === 'en_stock';
47
48
        $product = new Product($productId, $title, $reference, $description, $price, $available, $url);
49
50
        return $product;
51
    }
52
53
    /**
54
     * @param int $categoryId
55
     * @return Category
56
     */
57
    public function getCategoryById($categoryId)
58
    {
59
        $url = 'http://www.tunisianet.com.tn/'.$categoryId.'-';
60
        $crawler = $this->client->request('GET', $url);
61
62
        $url = $this->client->getHistory()->current()->getUri();
63
64
        $title = $crawler->filter('title')->text();
65
66
67
        $products = $crawler->filter('#produit_liste_main .ajax_block_product')->each(function (Crawler $node) {
68
69
            $title = $node->filter('.center_block > h2 > a')->text();
70
            $id = intval($node->filter("input[id^='hit_id']")->attr('value'));
71
            $reference = $node->filter("input[id^='hit_ref']")->attr('value');
72
            $description = $node->filter('.center_block > .product_desc > a')->text();
73
            $priceTag = $node->filter('.price')->text();
74
            $price =  floatval(str_replace(',', '.', $priceTag));
75
            $available = $node->filter('#produit_liste_prix > div:nth-child(2)')->attr('class') === 'en_stock';
76
            $url = $node->filter('.center_block > h2 > a')->attr('href');
77
78
            $product = new Product($id, $title, $reference, $description, $price, $available, $url);
79
80
            return $product;
81
        });
82
83
        $category = new Category($categoryId, $title, $url, $products);
84
        return $category;
85
    }
86
}
87