Completed
Pull Request — master (#245)
by Sergey
04:32
created

Searchable::createSearchQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 14
rs 9.4285
cc 1
eloc 7
nc 1
nop 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A Searchable::search() 0 6 1
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\Api\SearchResponse;
8
use seregazhuk\PinterestBot\Helpers\Pagination;
9
use seregazhuk\PinterestBot\Helpers\UrlBuilder;
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 HasPagination, HandlesRequest;
21
22
    /**
23
     * @return string
24
     */
25
    protected function getSearchScope()
26
    {
27
        return property_exists($this, 'searchScope') ? $this->searchScope : '';
28
    }
29
30
    /**
31
     * Executes search to API. Query - search string.
32
     *
33
     * @param string $query
34
     * @param string $scope
35
     * @return SearchResponse
36
     */
37
    protected function execSearchRequest($query, $scope)
38
    {
39
        $url = $this->response->hasBookmarks() ?
40
            UrlBuilder::RESOURCE_SEARCH_WITH_PAGINATION :
41
            UrlBuilder::RESOURCE_SEARCH;
42
43
        $requestOptions = [
44
            'scope' => $scope,
45
            'query' => $query,
46
        ];
47
48
        $this->get($requestOptions, $url);
49
50
        return new SearchResponse(
51
            $this->response->getRawData()
52
        );
53
    }
54
55
    /**
56
     * Search entities by search query.
57
     *
58
     * @param string $query
59
     * @param int $limit
60
     *
61
     * @return Pagination
62
     */
63
    public function search($query, $limit = Pagination::DEFAULT_LIMIT)
64
    {
65
        return $this->paginateCustom(function() use ($query) {
66
                return $this->execSearchRequest($query, $this->getSearchScope());
67
            })->take($limit);
68
    }
69
}
70