Completed
Push — master ( b86db6...b3547a )
by Sergey
39s
created

Provider::execGetRequestWithPagination()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 3
1
<?php
2
3
namespace seregazhuk\PinterestBot\Api\Providers;
4
5
use seregazhuk\PinterestBot\Api\Request;
6
use seregazhuk\PinterestBot\Api\Response;
7
use seregazhuk\PinterestBot\Helpers\Pagination;
8
9
/**
10
 * Class Provider.
11
 * @property string entityIdName
12
 */
13
abstract class Provider
14
{
15
    /**
16
     * List of methods that require logged status.
17
     *
18
     * @var array
19
     */
20
    protected $loginRequiredFor = [];
21
22
    /**
23
     * Instance of the API Request.
24
     *
25
     * @var Request
26
     */
27
    protected $request;
28
29
    /**
30
     * @param Request $request
31
     */
32
    public function __construct(Request $request)
33
    {
34
        $this->request = $request;
35
    }
36
37
    /**
38
     * Executes a POST request to Pinterest API.
39
     *
40
     * @param array $requestOptions
41
     * @param string $resourceUrl
42
     * @param bool $returnResponse
43
     *
44
     * @return Response|bool
45
     */
46
    protected function execPostRequest($requestOptions, $resourceUrl, $returnResponse = false)
47
    {
48
        $postString = Request::createQuery($requestOptions);
49
        $response = $this->request->exec($resourceUrl, $postString);
50
51
        return $returnResponse ? $response : $response->isOk();
52
    }
53
54
    /**
55
     * Executes a GET request to Pinterest API.
56
     *
57
     * @param array $requestOptions
58
     * @param string $resourceUrl
59
     * @return array|bool
60
     */
61
    protected function execGetRequest(array $requestOptions = [], $resourceUrl = '')
62
    {
63
        $query = Request::createQuery($requestOptions);
64
65
        $response = $this->request->exec($resourceUrl . "?{$query}");
66
67
        return $response->getResponseData();
68
    }
69
70
    /**
71
     * Executes a GET request to Pinterest API with pagination.
72
     *
73
     * @param array $requestOptions
74
     * @param string $resourceUrl
75
     * @param array $bookmarks
76
     * @return Response
77
     */
78
    protected function execGetRequestWithPagination(array $requestOptions, $resourceUrl, $bookmarks = [])
79
    {
80
        $query = Request::createQuery($requestOptions, $bookmarks);
81
82
        return $this->request->exec($resourceUrl . "?{$query}");
83
    }
84
85
    /**
86
     * @return string
87
     */
88
    public function getEntityIdName()
89
    {
90
        return property_exists($this, 'entityIdName') ? $this->entityIdName : '';
91
    }
92
93
    /**
94
     * Executes pagination GET request.
95
     *
96
     * @param array $data
97
     * @param string $url
98
     * @param array $bookmarks
99
     * @return Response
100
     */
101
    public function getPaginatedData(array $data, $url, $bookmarks = [])
102
    {
103
        return $this->execGetRequestWithPagination($data, $url, $bookmarks);
104
    }
105
106
    /**
107
     * @param string $method
108
     *
109
     * @return bool
110
     */
111
    public function checkMethodRequiresLogin($method)
112
    {
113
        return in_array($method, $this->loginRequiredFor);
114
    }
115
116
    /**
117
     * @return Request
118
     */
119
    public function getRequest()
120
    {
121
        return $this->request;
122
    }
123
124
    /**
125
     * @param array $params
126
     * @param int $limit
127
     * @param string $method
128
     * @return mixed
129
     */
130
    protected function getPaginatedResponse(array $params, $limit, $method = 'getPaginatedData')
131
    {
132
        return (new Pagination($this))->paginateOver($method, $params, $limit);
133
    }
134
135
    /**
136
     * @param $pinId
137
     * @return string
138
     */
139
    protected function convertId($pinId)
140
    {
141
        return (string)$pinId;
142
    }
143
}
144