Completed
Pull Request — master (#19)
by Sergey
03:32
created

SearchHelper   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 2
cbo 5
dl 0
loc 92
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
getScope() 0 1 ?
A searchCall() 0 9 1
A searchWithPagination() 0 9 1
B createSearchRequest() 0 26 2
A search() 0 4 1
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 = [
64
            "scope" => $scope,
65
            "query" => $query,
66
        ];
67
68
        $dataJson = ["options" => $options];
69
70
        if ( ! empty($bookmarks)) {
71
            $dataJson['options']['bookmarks'] = $bookmarks;
72
        } else {
73
            $dataJson = array_merge(
74
                $dataJson, [
75
                    'module' => [
76
                        "name"    => $this->moduleSearchPage,
77
                        "options" => $options,
78
                    ],
79
                ]
80
            );
81
        }
82
83
        return Request::createRequestData(
84
            $dataJson, "/search/$scope/?q=".$query
85
        );
86
    }
87
88
    /**
89
     * Search entities by search query
90
     *
91
     * @param string $query
92
     * @param int    $batchesLimit
93
     * @return \Iterator
94
     */
95
    public function search($query, $batchesLimit = 0)
96
    {
97
        return $this->searchWithPagination($query, $batchesLimit);
98
    }
99
}