GetFrom   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
c 0
b 0
f 0
lcom 1
cbo 6
dl 0
loc 107
ccs 33
cts 33
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 2
A getHttpClient() 0 4 1
A adById() 0 4 1
A adByUrl() 0 8 1
A ad() 0 12 3
B search() 0 25 2
1
<?php
2
3
namespace Lbc;
4
5
use GuzzleHttp\Client;
6
use Lbc\Crawler\AdCrawler;
7
use Lbc\Crawler\SearchResultCrawler;
8
use Lbc\Parser\SearchResultUrlParser;
9
use Symfony\Component\DomCrawler\Crawler;
10
11
/**
12
 * Class GetFrom
13
 * @package Lbc
14
 */
15
class GetFrom
16
{
17
    /**
18
     * @var Client
19
     */
20
    protected $client;
21
22
    /**
23
     * GetFrom constructor.
24
     * @param Client|null $client
25
     */
26 12
    public function __construct(Client $client = null)
27
    {
28 12
        $this->client = $client ?: new Client();
29 12
    }
30
31
    /**
32
     * Return the http client
33
     * (useful to mock the response for unit testing)
34
     *
35
     * @return Client
36
     */
37 2
    public function getHttpClient()
38
    {
39 2
        return $this->client;
40
    }
41
42
    /**
43
     * retrieve the search result data from the given url
44
     *
45
     * @param $url
46
     * @param bool $detailedAd
47
     *
48
     * @return array
49
     */
50 4
    public function search($url, $detailedAd = false)
51
    {
52 4
        $searchData = new SearchResultCrawler(
53 4
            new Crawler((string) $this->client->get($url)->getBody()),
54 4
            $url
55
        );
56
57 4
        $url = new SearchResultUrlParser($url, $searchData->getNbPages());
58
59 4
        $ads = ($detailedAd) ? $searchData->getAds() : $searchData->getAdsId();
60
61
        $sumarize = [
62 4
            'total_ads'    => $searchData->getNbAds(),
63 4
            'total_page'   => $searchData->getNbPages(),
64 4
            'ads_per_page' => $searchData->getNbAdsPerPage(),
65 4
            'category'     => $searchData->getUrlParser()->getCategory(),
66 4
            'location'     => $searchData->getUrlParser()->getLocation(),
67 4
            'search_area'  => $searchData->getUrlParser()->getSearchArea(),
68 4
            'sort_by'      => $searchData->getUrlParser()->getSortType(),
69 4
            'type'         => $searchData->getUrlParser()->getType(),
70 4
            'ads'          => $ads,
71
        ];
72
73 4
        return array_merge($url->getNav(), $sumarize);
74
    }
75
76
    /**
77
     * Retrieve the ad's data from an ad's ID and its category
78
     *
79
     * @param $id
80
     * @param $category
81
     *
82
     * @return array
83
     */
84 2
    private function adById($id, $category)
85
    {
86 2
        return $this->ad("https://www.leboncoin.fr/{$category}/{$id}.htm");
87
    }
88
89
    /**
90
     * Retrieve the ad's data from the given url
91
     *
92
     * @param $url
93
     * @return array
94
     */
95 4
    private function adByUrl($url)
96
    {
97 4
        $content = $this->client->get($url)->getBody()->getContents();
98
99 4
        $adData = new AdCrawler(new Crawler(utf8_encode($content)), $url);
100
101 4
        return $adData->getAll();
102
    }
103
104
    /**
105
     * Dynamique method to retrive the data by url OR id and category
106
     *
107
     * @return bool|mixed
108
     */
109 6
    public function ad()
110
    {
111 6
        if (func_num_args() === 1) {
112 4
            return call_user_func_array([$this, 'adByUrl'], func_get_args());
113
        }
114
115 4
        if (func_num_args() === 2) {
116 2
            return call_user_func_array([$this, 'adById'], func_get_args());
117
        }
118
119 2
        throw new \InvalidArgumentException('Bad number of argument');
120
    }
121
}
122