Keywords::recommendedFor()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace seregazhuk\PinterestBot\Api\Providers;
4
5
use seregazhuk\PinterestBot\Helpers\UrlBuilder;
6
use seregazhuk\PinterestBot\Api\Providers\Core\Provider;
7
8
class Keywords extends Provider
9
{
10
    /**
11
     * Get recommendations for query word.
12
     *
13
     * @param string $query
14
     * @return array|bool
15
     */
16
    public function recommendedFor($query)
17
    {
18
        $requestOptions = [
19
            'scope' => 'pins',
20
            'query' => $query,
21
        ];
22
23
        $result = $this->get(UrlBuilder::RESOURCE_SEARCH, $requestOptions);
24
25
        return empty($result) ? [] : $this->getKeywordsFromRequest($result);
26
    }
27
28
    /**
29
     * @param bool|array $response
30
     * @return bool|array
31
     */
32
    protected function getKeywordsFromRequest($response)
33
    {
34
        $keywords = $response['guides'];
35
36
        if (empty($keywords)) {
37
            return [];
38
        }
39
40
        return array_map(function ($keywordData) {
41
            return [
42
                'term'     => $keywordData['term'],
43
                'display'  => $keywordData['display'],
44
                'position' => $keywordData['position'],
45
            ];
46
        }, $keywords);
47
    }
48
}
49