Completed
Push — master ( d31412...ff3c6b )
by Sergey
05:01 queued 01:33
created

Searchable   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
c 1
b 0
f 0
lcom 2
cbo 6
dl 0
loc 128
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getSearchScope() 0 4 2
A searchCall() 0 19 2
A createSearchQuery() 0 7 1
A search() 0 9 1
A appendBookMarks() 0 20 2
A parseSearchResult() 0 14 3
1
<?php
2
3
namespace seregazhuk\PinterestBot\Api\Traits;
4
5
use seregazhuk\PinterestBot\Api\Request;
6
use seregazhuk\PinterestBot\Helpers\UrlHelper;
7
use seregazhuk\PinterestBot\Helpers\Pagination;
8
9
/**
10
 * Class Searchable
11
 * @package seregazhuk\PinterestBot\Api\Traits
12
 *
13
 * @property string $searchScope
14
 */
15
trait Searchable
16
{
17
    use HandlesRequestAndResponse;
18
    
19
    private $moduleSearchPage = 'SearchPage';
20
21
    /**
22
     * @return string
23
     */
24
    protected function getSearchScope()
25
    {
26
        return property_exists($this, 'searchScope') ? $this->searchScope : '';
27
    }
28
29
    /**
30
     * Executes search to API. Query - search string.
31
     *
32
     * @param string $query
33
     * @param string $scope
34
     * @param array  $bookmarks
35
     *
36
     * @return array
37
     */
38
    public function searchCall($query, $scope, $bookmarks = [])
39
    {
40
        $url = UrlHelper::getSearchUrl($bookmarks);
41
        $get = $this->createSearchQuery($query, $scope, $bookmarks);
42
        $result = $this->getRequest()->exec($url . '?' . $get);
43
44
        /*
45
         * It was a first time search, we grab data and bookmarks for pagination.
46
         */
47
        if (empty($bookmarks)) {
48
            return $this->parseSearchResult($result);
49
        }
50
51
        /*
52
         * Process a response with bookmarks
53
         */
54
55
        return $this->getResponse()->getPaginationData($result);
56
    }
57
58
    /**
59
     * Creates Pinterest API search request.
60
     *
61
     * @param string $query
62
     * @param string $scope
63
     * @param array $bookmarks
64
     *
65
     * @return array
66
     */
67
    protected function createSearchQuery($query, $scope, $bookmarks = [])
68
    {
69
        $options = ['scope' => $scope, 'query' => $query];
70
        $dataJson = $this->appendBookMarks($bookmarks, $options);
71
72
        return Request::createQuery($dataJson);
73
    }
74
75
    /**
76
     * Search entities by search query.
77
     *
78
     * @param string $query
79
     * @param int $limit
80
     *
81
     * @return \Iterator
82
     */
83
    public function search($query, $limit = 0)
84
    {
85
        return (new Pagination($this))->paginateOver(
86
            'searchCall', [
87
            'query' => $query,
88
            'scope' => $this->getSearchScope(),
89
        ], $limit
90
        );
91
    }
92
93
    /**
94
     * @param array $bookmarks
95
     * @param array $options
96
     *
97
     * @return array
98
     */
99
    protected function appendBookMarks($bookmarks, $options)
100
    {
101
        $dataJson = ['options' => $options];
102
        if (!empty($bookmarks)) {
103
            $dataJson['options']['bookmarks'] = $bookmarks;
104
105
            return $dataJson;
106
        }
107
108
        $dataJson = array_merge(
109
            $dataJson, [
110
                'module' => [
111
                    'name'    => $this->moduleSearchPage,
112
                    'options' => $options,
113
                ],
114
            ]
115
        );
116
117
        return $dataJson;
118
    }
119
120
    /**
121
     * Parses simple Pinterest search API response
122
     * on request with bookmarks.
123
     *
124
     * @param array $response
125
     *
126
     * @return array
127
     */
128
    protected function parseSearchResult($response)
129
    {
130
        $bookmarks = [];
131
132
        if (isset($response['module']['tree']['resource']['options']['bookmarks'][0])) {
133
            $bookmarks = $response['module']['tree']['resource']['options']['bookmarks'][0];
134
        }
135
136
        if (!empty($response['module']['tree']['data']['results'])) {
137
            return ['data' => $response['module']['tree']['data']['results'], 'bookmarks' => [$bookmarks]];
138
        }
139
140
        return [];
141
    }
142
}
143