Completed
Pull Request — master (#86)
by Sergey
02:55
created

Searchable::searchWithPagination()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 6
nc 1
nop 2
1
<?php
2
3
namespace seregazhuk\PinterestBot\Helpers\Providers\Traits;
4
5
use seregazhuk\PinterestBot\Api\Request;
6
use seregazhuk\PinterestBot\Helpers\Pagination;
7
use seregazhuk\PinterestBot\Helpers\UrlHelper;
8
9
trait Searchable
10
{
11
    use ProviderTrait;
12
13
    private $moduleSearchPage = 'SearchPage';
14
15
    /**
16
     * @return string
17
     */
18
    abstract protected function getScope();
19
20
    /**
21
     * Executes search to API. Query - search string.
22
     *
23
     * @param string $query
24
     * @param string $scope
25
     * @param array  $bookmarks
26
     *
27
     * @return array
28
     */
29
    public function searchCall($query, $scope, $bookmarks = [])
30
    {
31
        $url = UrlHelper::getSearchUrl(!empty($bookmarks));
32
        $get = $this->createSearchRequest($query, $scope, $bookmarks);
33
        $url = $url.'?'.UrlHelper::buildRequestString($get);
34
        $response = $this->getRequest()->exec($url);
35
36
        return $this->getResponse()->parseSearchResponse($response, !empty($bookmarks));
37
    }
38
39
    /**
40
     * Executes search to API with pagination.
41
     *
42
     * @param string $query
43
     * @param int    $batchesLimit
44
     *
45
     * @return \Iterator
46
     */
47
    public function searchWithPagination($query, $batchesLimit)
48
    {
49
        return Pagination::getPaginatedData(
50
            [$this, 'searchCall'], [
51
            'query' => $query,
52
            'scope' => $this->getScope(),
53
        ], $batchesLimit
54
        );
55
    }
56
57
    /**
58
     * Creates Pinterest API search request.
59
     *
60
     * @param       $query
61
     * @param       $scope
62
     * @param array $bookmarks
63
     *
64
     * @return array
65
     */
66
    public function createSearchRequest($query, $scope, $bookmarks = [])
67
    {
68
        $options = ['scope' => $scope, 'query' => $query];
69
        $dataJson = $this->appendBookMarks($bookmarks, $options);
70
71
        return Request::createRequestData(
72
            $dataJson, "/search/$scope/?q=".$query
73
        );
74
    }
75
76
    /**
77
     * Search entities by search query.
78
     *
79
     * @param string $query
80
     * @param int    $batchesLimit
81
     *
82
     * @return \Iterator
83
     */
84
    public function search($query, $batchesLimit = 0)
85
    {
86
        return $this->searchWithPagination($query, $batchesLimit);
87
    }
88
89
    /**
90
     * @param $bookmarks
91
     * @param $options
92
     *
93
     * @return array
94
     */
95
    protected function appendBookMarks($bookmarks, $options)
96
    {
97
        $dataJson = ['options' => $options];
98
        if (!empty($bookmarks)) {
99
            $dataJson['options']['bookmarks'] = $bookmarks;
100
101
            return $dataJson;
102
        } else {
103
            $dataJson = array_merge(
104
                $dataJson, [
105
                    'module' => [
106
                        'name'    => $this->moduleSearchPage,
107
                        'options' => $options,
108
                    ],
109
                ]
110
            );
111
112
            return $dataJson;
113
        }
114
    }
115
}
116