Completed
Push — master ( ae4a40...cd3ab6 )
by Tristan
13:24 queued 09:52
created

Search::processResponse()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 1 Features 1
Metric Value
c 6
b 1
f 1
dl 0
loc 10
rs 9.4285
cc 3
eloc 5
nc 2
nop 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 Tristanbes\MyPoseoBundle\Connection\RestClient;
12
13
/**
14
 * @see http://fr.myposeo.com/nos-api/api-search/
15
 */
16
class Search implements SearchInterface
17
{
18
    /**
19
     * @var RestClient
20
     */
21
    private $client;
22
23
    /**
24
     * @param RestClient $client The http client
25
     */
26
    public function __construct(RestClient $client)
27
    {
28
        $this->client = $client;
29
    }
30
31
    /**
32
     * Returns the identifiers of the search engine's extension
33
     *
34
     * @param string  $searchEngine The search engine
35
     * @param integer $ttl          The time to live for the cache
36
     *
37
     * @return array [['id' => 1, 'name => '.fr'] [...]]
38
     */
39
    public function getSearchEngineExtensions($searchEngine, $ttl = null)
40
    {
41
        $cacheKey = sprintf('%s_locations', $searchEngine);
42
43
        $data = $this->client->get('tool/json', [
44
            'method'       => 'getLocations',
45
            'searchEngine' => $searchEngine,
46
        ], $cacheKey, $ttl);
47
48
        return $data;
49
    }
50
51
    /**
52
     * Get the town's code
53
     *
54
     * @param string $name    The town name
55
     * @param string $country The country ISO
56
     *
57
     * @return array [['id' => 1, 'city_code => '1234', 'city_name' => 'dunkerque', 'code_dep' : '59']]
58
     */
59
    public function getTownCode($name, $country = 'FR')
60
    {
61
        $data = $this->client->get('tool/json', [
62
            'method'  => 'getGeoloc',
63
            'country' => $country,
64
            'city'    => $name,
65
        ]);
66
67
        return $data;
68
    }
69
70
    /**
71
     * Retrieves the url position given a keyword
72
     *
73
     * @param string  $keyword
74
     * @param string  $url
75
     * @param string  $searchEngine
76
     * @param string  $callback
77
     * @param integer $geolocId
78
     * @param integer $location
79
     * @param integer $maxPage
80
     *
81
     * @return array
82
     *
83
     *   {
84
     *     "url_positioned": "",
85
     *     "position": "+100",
86
     *     "page": "-",
87
     *     "type": "seo_natural",
88
     *     "serp": "",
89
     *     "nbr_results": 250000000,
90
     *     "top": "https://urltestdefault.com/path/to/image",
91
     *     "keyword": "keyword",
92
     *     "url_search": "lemonde.fr",
93
     *     "searchEngine": "google",
94
     *     "location": "13"
95
     *   }
96
     */
97
    public function getUrlRankByKeyword($keyword, $url, $searchEngine = 'google', $callback = null, $geolocId = null, $location = 13, $maxPage = null)
98
    {
99
        $options = [];
100
101
        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...
102
            $options['callback'] = $callback;
103
        }
104
105
        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...
106
            $options['geolocId'] = $geolocId;
107
        }
108
109
        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...
110
            $options['maxPage'] = $maxPage;
111
        }
112
113
        $data = $this->client->get('tool/json', array_merge([
114
            'method'       => 'getPosition',
115
            'keyword'      => $keyword,
116
            'url'          => $url,
117
            'searchEngine' => $searchEngine,
118
            'location'     => $location,
119
        ], $options));
120
121
        return $data;
122
    }
123
124
    public function getNaturalSeoResult()
125
    {
126
        // @todo, feel free to send a PR
127
    }
128
129
    public function getSemResult()
130
    {
131
        // @todo, feel free to send a PR
132
    }
133
}
134