Completed
Push — master ( 838849...00c88c )
by Rémi
10s
created

GetFrom::__callStatic()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

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