Completed
Pull Request — master (#105)
by Sergey
03:14
created

Keywords::parseKeywordsFromRequest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
rs 9.4285
cc 2
eloc 8
nc 2
nop 1
1
<?php
2
3
namespace seregazhuk\PinterestBot\Api\Providers;
4
5
use seregazhuk\PinterestBot\Api\Request;
6
use seregazhuk\PinterestBot\Helpers\UrlHelper;
7
8
class Keywords extends Provider
9
{
10
    public function recommendedFor($query)
11
    {
12
        $data = ['options' => ['scope' => 'pins', 'query' => $query]];
13
        $get = Request::createQuery(
14
            $data, "/search/pins/?q=" . $query
15
        );
16
        $response = $this->getRequest()->exec(UrlHelper::getSearchUrl() . '?' . $get);
17
18
        return $this->parseKeywordsFromRequest($response);
19
    }
20
21
    /**
22
     * @param $response
23
     * @return array|null
24
     */
25
    protected function parseKeywordsFromRequest($response)
26
    {
27
        if (!isset($response['resource_data_cache'][0]['data']['guides'])) {
28
            return null;
29
        }
30
31
        $keywords = $response['resource_data_cache'][0]['data']['guides'];
32
33
        return array_map(
34
            function ($keywordData) {
35
                return $keywordData['term'];
36
            }, $keywords
37
        );
38
    }
39
}