Searchable::search()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 5
rs 10
1
<?php
2
3
namespace seregazhuk\PinterestBot\Api\Traits;
4
5
use seregazhuk\PinterestBot\Api\SearchResponse;
6
use seregazhuk\PinterestBot\Helpers\Pagination;
7
use seregazhuk\PinterestBot\Helpers\UrlBuilder;
8
9
/**
10
 * Trait Searchable
11
 *
12
 * @property string $searchScope
13
 */
14
trait Searchable
15
{
16
    use HasPagination, HandlesRequest;
17
18
    /**
19
     * @return string
20
     */
21
    protected function getSearchScope()
22
    {
23
        return property_exists($this, 'searchScope') ? $this->searchScope : '';
24
    }
25
26
    /**
27
     * Executes search to API. Query - search string.
28
     *
29
     * @param string $query
30
     * @param string $scope
31
     * @return SearchResponse
32
     */
33
    protected function execSearchRequest($query, $scope)
34
    {
35
        $url = $this->getResponse()->hasBookmarks() ?
36
            UrlBuilder::RESOURCE_SEARCH_WITH_PAGINATION :
37
            UrlBuilder::RESOURCE_SEARCH;
38
39
        $requestOptions = [
40
            'scope' => $scope,
41
            'query' => $query,
42
        ];
43
44
        $this->get($url, $requestOptions);
45
46
        return new SearchResponse(
47
            $this->getResponse()->getRawData()
48
        );
49
    }
50
51
    /**
52
     * Search entities by search query.
53
     *
54
     * @param string $query
55
     * @param int $limit
56
     *
57
     * @return Pagination
58
     */
59
    public function search($query, $limit = Pagination::DEFAULT_LIMIT)
60
    {
61
        return $this->paginateCustom(function () use ($query) {
62
            return $this->execSearchRequest($query, $this->getSearchScope());
63
        })->take($limit);
64
    }
65
}
66