Completed
Pull Request — master (#16)
by Rémi
04:19
created

GetFrom::__call()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
ccs 3
cts 4
cp 0.75
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
crap 2.0625
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
use Symfony\Component\DomCrawler\Crawler;
11
12
/**
13
 * Class GetFrom
14
 * @package Lbc
15
 */
16
class GetFrom
17
{
18
    /**
19
     * @var Client
20
     */
21
    protected $client;
22
23
    /**
24
     * GetFrom constructor.
25
     * @param Client|null $client
26
     */
27 12
    public function __construct(Client $client = null)
28
    {
29 12
        $this->client = $client ?: new Client();
30 12
    }
31
32
    /**
33
     * Return the http client
34
     * (useful to mock the response for unit testing)
35
     *
36
     * @return Client
37
     */
38 2
    public function getHttpClient()
39
    {
40 2
        return $this->client;
41
    }
42
43
    /**
44
     * retrieve the search result data from the given url
45
     *
46
     * @param $url
47
     * @param bool $detailedAd
48
     *
49
     * @return array
50
     */
51 4
    public function search($url, $detailedAd = false)
52
    {
53 4
        $searchData = new SearchResultCrawler(
54 4
            new Crawler((string) $this->client->get($url)->getBody()),
55
            $url
56 4
        );
57
58 4
        $url = new SearchResultUrlParser($url, $searchData->getNbPages());
59
60 4
        $ads = ($detailedAd) ? $searchData->getAds() : $searchData->getAdsId();
61
62
        $sumarize = [
63 4
            'total_ads'    => $searchData->getNbAds(),
64 4
            'total_page'   => $searchData->getNbPages(),
65 4
            'ads_per_page' => $searchData->getNbAdsPerPage(),
66 4
            'category'     => $searchData->getUrlParser()->getCategory(),
67 4
            'location'     => $searchData->getUrlParser()->getLocation(),
0 ignored issues
show
Bug introduced by
The method getLocation does only exist in Lbc\Parser\SearchResultUrlParser, but not in Lbc\Parser\AdUrlParser.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
68 4
            'search_area'  => $searchData->getUrlParser()->getSearchArea(),
0 ignored issues
show
Bug introduced by
The method getSearchArea does only exist in Lbc\Parser\SearchResultUrlParser, but not in Lbc\Parser\AdUrlParser.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
69 4
            'sort_by'      => $searchData->getUrlParser()->getSortType(),
0 ignored issues
show
Bug introduced by
The method getSortType does only exist in Lbc\Parser\SearchResultUrlParser, but not in Lbc\Parser\AdUrlParser.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
70 4
            'type'         => $searchData->getUrlParser()->getType(),
0 ignored issues
show
Bug introduced by
The method getType does only exist in Lbc\Parser\SearchResultUrlParser, but not in Lbc\Parser\AdUrlParser.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
71 4
            'ads'          => $ads,
72 4
        ];
73
74 4
        return array_merge($url->getNav(), $sumarize);
75
    }
76
77
    /**
78
     * Retrieve the ad's data from an ad's ID and its category
79
     *
80
     * @param $id
81
     * @param $category
82
     *
83
     * @return array
84
     */
85 2
    private function adById($id, $category)
86
    {
87 2
        return $this->ad("https://www.leboncoin.fr/{$category}/{$id}.htm");
88
    }
89
90
    /**
91
     * Retrieve the ad's data from the given url
92
     *
93
     * @param $url
94
     * @return array
95
     */
96 4
    private function adByUrl($url)
97
    {
98 4
        $content = $this->client->get($url)->getBody()->getContents();
99
100 4
        $adData = new AdCrawler(new Crawler(utf8_encode($content)), $url);
101
102 4
        return $adData->getAll();
103
    }
104
105
    /**
106
     * Dynamique method to retrive the data by url OR id and category
107
     *
108
     * @return bool|mixed
109
     */
110 6
    public function ad()
111
    {
112 6
        if (func_num_args() === 1) {
113 4
            return call_user_func_array([$this, 'adByUrl'], func_get_args());
114
        }
115
116 4
        if (func_num_args() === 2) {
117 2
            return call_user_func_array([$this, 'adById'], func_get_args());
118
        }
119
120 6
        throw new \InvalidArgumentException('Bad number of argument');
121
    }
122
}
123