Completed
Pull Request — master (#9)
by
unknown
05:48 queued 03:53
created

GetFrom   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 96.08%

Importance

Changes 0
Metric Value
wmc 13
c 0
b 0
f 0
lcom 1
cbo 6
dl 0
loc 133
ccs 49
cts 51
cp 0.9608
rs 10

8 Methods

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