Searchable   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 1
Metric Value
eloc 15
c 4
b 0
f 1
dl 0
loc 50
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getSearchScope() 0 3 2
A execSearchRequest() 0 15 2
A search() 0 5 1
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