Completed
Pull Request — master (#236)
by Sergey
06:25
created

Searchable::search()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 7
Ratio 100 %

Importance

Changes 0
Metric Value
dl 7
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 2
1
<?php
2
3
namespace seregazhuk\PinterestBot\Api\Traits;
4
5
use seregazhuk\PinterestBot\Api\Request;
6
use seregazhuk\PinterestBot\Api\Response;
7
use seregazhuk\PinterestBot\Helpers\Pagination;
8
use seregazhuk\PinterestBot\Helpers\UrlBuilder;
9
use seregazhuk\PinterestBot\Api\SearchResponse;
10
11
/**
12
 * Trait Searchable
13
 *
14
 * @property string $searchScope
15
 * @property Request request
16
 * @property Response $response
17
 */
18
trait Searchable
19
{
20
    use HandlesRequest;
21
    
22
    protected $moduleSearchPage = 'SearchPage';
23
24
    /**
25
     * @return string
26
     */
27
    protected function getSearchScope()
28
    {
29
        return property_exists($this, 'searchScope') ? $this->searchScope : '';
30
    }
31
32
    /**
33
     * Executes search to API. Query - search string.
34
     *
35
     * @param string $query
36
     * @param string $scope
37
     * @param array  $bookmarks
38
     *
39
     * @return SearchResponse
40
     */
41
    public function execSearchRequest($query, $scope, $bookmarks = [])
42
    {
43
        $url = UrlBuilder::getSearchUrl($bookmarks);
44
        $get = $this->createSearchQuery($query, $scope, $bookmarks);
45
        $result = $this->request->exec($url . '?' . $get);
46
47
        $this->processResult($result);
48
49
        return new SearchResponse($this->response);
50
    }
51
52
    /**
53
     * Creates Pinterest API search request.
54
     *
55
     * @param string $query
56
     * @param string $scope
57
     * @param array $bookmarks
58
     *
59
     * @return string
60
     */
61
    protected function createSearchQuery($query, $scope, $bookmarks = [])
62
    {
63
        $dataJson = $this->appendBookMarks(
64
            $bookmarks,
65
            [
66
                'scope' => $scope,
67
                'query' => $query
68
            ]
69
        );
70
71
        $request = Request::createRequestData($dataJson, $bookmarks);
72
73
        return UrlBuilder::buildRequestString($request);
74
    }
75
76
    /**
77
     * Search entities by search query.
78
     *
79
     * @param string $query
80
     * @param int $limit
81
     *
82
     * @return Pagination
83
     */
84 View Code Duplication
    public function search($query, $limit = Pagination::DEFAULT_LIMIT)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
85
    {
86
        return (new Pagination($limit))
87
            ->paginateOver(function($bookmarks = []) use ($query) {
88
                return $this->execSearchRequest($query, $this->getSearchScope(), $bookmarks);
89
            });
90
    }
91
92
    /**
93
     * @param array $bookmarks
94
     * @param array $options
95
     *
96
     * @return array
97
     */
98
    protected function appendBookMarks($bookmarks, $options)
99
    {
100
        $dataJson = ['options' => $options];
101
        if (!empty($bookmarks)) {
102
            $dataJson['options']['bookmarks'] = $bookmarks;
103
104
            return $dataJson;
105
        }
106
107
        $dataJson = array_merge(
108
            $dataJson, [
109
                'module' => [
110
                    "name"    => $this->moduleSearchPage,
111
                    "options" => $options,
112
                ],
113
            ]
114
        );
115
116
        return $dataJson;
117
    }
118
119
    /**
120
     * @param string $res
121
     * @return Response
122
     */
123
    abstract protected function processResult($res);
124
}
125