Completed
Pull Request — master (#27)
by Sergey
03:41
created

SearchHelper::createSearchRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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