Completed
Pull Request — master (#3)
by Tristan
07:03 queued 02:41
created

Search::doRequest()   C

Complexity

Conditions 8
Paths 17

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 26
rs 5.3846
cc 8
eloc 17
nc 17
nop 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A Search::getTownCode() 0 10 1
1
<?php
2
3
/**
4
 * MyPoseo API Bundle
5
 *
6
 * @author Tristan Bessoussa <[email protected]>
7
 */
8
9
namespace Tristanbes\MyPoseoBundle\Api;
10
11
use Http\Client\Common\PluginClient;
12
use Http\Client\HttpClient;
13
use Doctrine\Common\Cache\Cache;
14
use Http\Discovery\MessageFactoryDiscovery;
15
use Http\Message\Authentication\QueryParam;
16
use Http\Client\Common\Plugin\AuthenticationPlugin;
17
18
use Psr\Http\Message\RequestInterface;
19
use Psr\Http\Message\ResponseInterface;
20
use Tristanbes\MyPoseoBundle\Connection\RestClient;
21
use Tristanbes\MyPoseoBundle\Exception\NotEnoughCreditsException;
22
23
/**
24
 * @see http://fr.myposeo.com/nos-api/api-search/
25
 */
26
class Search implements SearchInterface
27
{
28
    /**
29
     * @var RestClient
30
     */
31
    private $client;
32
33
    /**
34
     * @param RestClient $client The http client
35
     */
36
    public function __construct(RestClient $client)
37
    {
38
        $this->client = $client;
39
    }
40
41
    /**
42
     * Returns the identifiers of the search engine's extension
43
     *
44
     * @param string  $searchEngine The search engine
45
     * @param integer $ttl          The time to live for the cache
46
     *
47
     * @return array [['id' => 1, 'name => '.fr'] [...]]
48
     */
49
    public function getSearchEngineExtensions($searchEngine, $ttl = null)
50
    {
51
        $cacheKey = sprintf('%s_locations', $searchEngine);
52
53
        $data = $this->client->get('tool/json', [
54
            'method'       => 'getLocations',
55
            'searchEngine' => $searchEngine,
56
        ], $cacheKey, $ttl);
0 ignored issues
show
Bug introduced by
It seems like $ttl defined by parameter $ttl on line 49 can also be of type integer; however, Tristanbes\MyPoseoBundle...ction\RestClient::get() does only seem to accept null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
57
58
        return $data;
59
    }
60
61
    /**
62
     * Get the town's code
63
     *
64
     * @param string $name    The town name
65
     * @param string $country The country ISO
66
     *
67
     * @return array [['id' => 1, 'city_code => '1234', 'city_name' => 'dunkerque', 'code_dep' : '59']]
68
     */
69
    public function getTownCode($name, $country = 'FR')
70
    {
71
        $data = $this->client->get('tool/json', [
72
            'method'  => 'getGeoloc',
73
            'country' => $country,
74
            'city'    => $name,
75
        ]);
76
77
        return $data;
78
    }
79
80
    /**
81
     * Retrieves the url position given a keyword
82
     *
83
     * @param string  $keyword
84
     * @param string  $url
85
     * @param string  $searchEngine
86
     * @param string  $callback
87
     * @param integer $geolocId
88
     * @param integer $location
89
     * @param integer $maxPage
90
     *
91
     * @return array
92
     *
93
     *   {
94
     *     "url_positioned": "",
95
     *     "position": "+100",
96
     *     "page": "-",
97
     *     "type": "seo_natural",
98
     *     "serp": "",
99
     *     "nbr_results": 250000000,
100
     *     "top": "https://urltestdefault.com/path/to/image",
101
     *     "keyword": "keyword",
102
     *     "url_search": "lemonde.fr",
103
     *     "searchEngine": "google",
104
     *     "location": "13"
105
     *   }
106
     */
107
    public function getUrlRankByKeyword($keyword, $url, $searchEngine = 'google', $callback = null, $geolocId = null, $location = 13, $maxPage = null)
108
    {
109
        $options = [];
110
111
        if ($callback) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $callback of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
112
            $options['callback'] = $callback;
113
        }
114
115
        if ($geolocId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $geolocId of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
116
            $options['geolocId'] = $geolocId;
117
        }
118
119
        if ($maxPage) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $maxPage of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
120
            $options['maxPage'] = $maxPage;
121
        }
122
123
        $data = $this->client->get('tool/json', array_merge([
124
            'method'       => 'getPosition',
125
            'keyword'      => $keyword,
126
            'url'          => $url,
127
            'searchEngine' => $searchEngine,
128
            'location'     => $location,
129
        ], $options));
130
131
        return $data;
132
    }
133
134
    public function getNaturalSeoResult()
135
    {
136
        // @todo, feel free to send a PR
137
    }
138
139
    public function getSemResult()
140
    {
141
        // @todo, feel free to send a PR
142
    }
143
}
144