Completed
Push — master ( 31d905...b5d7ba )
by Sergey
05:31 queued 02:23
created

Searchable::parseSearchResult()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
rs 9.4285
cc 3
eloc 7
nc 4
nop 1
1
<?php
2
3
namespace seregazhuk\PinterestBot\Helpers\Providers\Traits;
4
5
use seregazhuk\PinterestBot\Api\Request;
6
use seregazhuk\PinterestBot\Helpers\UrlHelper;
7
use seregazhuk\PinterestBot\Helpers\Pagination;
8
9
trait Searchable
10
{
11
    private $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
     *
25
     * @return array
26
     */
27
    public function searchCall($query, $scope, $bookmarks = [])
28
    {
29
        $url = UrlHelper::getSearchUrl($bookmarks);
30
        $get = $this->createSearchQuery($query, $scope, $bookmarks);
31
        $result = $this->getRequest()->exec($url . '?' . $get);
0 ignored issues
show
Bug introduced by
It seems like getRequest() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
32
33
        /*
34
         * It was a first time search, we grab data and bookmarks for pagination.
35
         */
36
        if (empty($bookmarks)) {
37
            return $this->parseSearchResult($result);
38
        }
39
40
        /*
41
         * Process a response with bookmarks
42
         */
43
44
        return $this->getResponse()->getPaginationData($result);
0 ignored issues
show
Bug introduced by
It seems like getResponse() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
45
    }
46
47
    /**
48
     * Creates Pinterest API search request.
49
     *
50
     * @param       $query
51
     * @param       $scope
52
     * @param array $bookmarks
53
     *
54
     * @return array
55
     */
56
    protected function createSearchQuery($query, $scope, $bookmarks = [])
57
    {
58
        $options = ['scope' => $scope, 'query' => $query];
59
        $dataJson = $this->appendBookMarks($bookmarks, $options);
60
61
        return Request::createQuery($dataJson);
62
    }
63
64
    /**
65
     * Search entities by search query.
66
     *
67
     * @param string $query
68
     * @param int $limit
69
     *
70
     * @return \Iterator
71
     */
72
    public function search($query, $limit = 0)
73
    {
74
        return (new Pagination($this))->paginateOver(
75
            'searchCall', [
76
            'query' => $query,
77
            'scope' => $this->getScope(),
78
        ], $limit
79
        );
80
    }
81
82
    /**
83
     * @param $bookmarks
84
     * @param $options
85
     *
86
     * @return array
87
     */
88
    protected function appendBookMarks($bookmarks, $options)
89
    {
90
        $dataJson = ['options' => $options];
91
        if (!empty($bookmarks)) {
92
            $dataJson['options']['bookmarks'] = $bookmarks;
93
94
            return $dataJson;
95
        }
96
97
        $dataJson = array_merge(
98
            $dataJson, [
99
                'module' => [
100
                    'name'    => $this->moduleSearchPage,
101
                    'options' => $options,
102
                ],
103
            ]
104
        );
105
106
        return $dataJson;
107
    }
108
109
    /**
110
     * Parses simple Pinterest search API response
111
     * on request with bookmarks.
112
     *
113
     * @param array $response
114
     *
115
     * @return array
116
     */
117
    protected function parseSearchResult($response)
118
    {
119
        $bookmarks = [];
120
121
        if (isset($response['module']['tree']['resource']['options']['bookmarks'][0])) {
122
            $bookmarks = $response['module']['tree']['resource']['options']['bookmarks'][0];
123
        }
124
125
        if (!empty($response['module']['tree']['data']['results'])) {
126
            return ['data' => $response['module']['tree']['data']['results'], 'bookmarks' => [$bookmarks]];
127
        }
128
129
        return [];
130
    }
131
}
132