Completed
Pull Request — master (#151)
by Sergey
03:00
created

Provider::getPaginatedResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
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
     *
60
     * @return Response
61
     */
62
    protected function execGetRequest(array $requestOptions = [], $resourceUrl = '')
63
    {
64
        $query = Request::createQuery($requestOptions);
65
66
        return $this->request->exec($resourceUrl . "?{$query}");
67
    }
68
69
    /**
70
     * Executes a GET request to Pinterest API with pagination.
71
     *
72
     * @param array $requestOptions
73
     * @param string $resourceUrl
74
     * @param array $bookmarks
75
     * @return Response
76
     */
77
    protected function execGetRequestWithPagination(array $requestOptions, $resourceUrl, $bookmarks = [])
78
    {
79
        $query = Request::createQuery($requestOptions, $bookmarks);
80
81
        return $this->request->exec($resourceUrl . "?{$query}");
82
    }
83
84
    /**
85
     * @return string
86
     */
87
    public function getEntityIdName()
88
    {
89
        return property_exists($this, 'entityIdName') ? $this->entityIdName : '';
90
    }
91
92
    /**
93
     * Executes pagination GET request.
94
     *
95
     * @param array $data
96
     * @param string $url
97
     * @param array $bookmarks
98
     * @return Response
99
     */
100
    public function getPaginatedData(array $data, $url, $bookmarks = [])
101
    {
102
        return $this->execGetRequestWithPagination($data, $url, $bookmarks);
103
    }
104
105
    /**
106
     * @param string $method
107
     *
108
     * @return bool
109
     */
110
    public function checkMethodRequiresLogin($method)
111
    {
112
        return in_array($method, $this->loginRequiredFor);
113
    }
114
115
    /**
116
     * @return Request
117
     */
118
    public function getRequest()
119
    {
120
        return $this->request;
121
    }
122
123
    /**
124
     * @param array $params
125
     * @param int $limit
126
     * @param string $method
127
     * @return mixed
128
     */
129
    protected function getPaginatedResponse(array $params, $limit, $method = 'getPaginatedData')
130
    {
131
        return (new Pagination($this))->paginateOver($method, $params, $limit);
132
    }
133
}
134