Completed
Pull Request — master (#221)
by Sergey
02:10
created

Provider::getPaginatedResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 3
1
<?php
2
3
namespace seregazhuk\PinterestBot\Api\Providers;
4
5
use Generator;
6
use seregazhuk\PinterestBot\Api\Request;
7
use seregazhuk\PinterestBot\Api\Response;
8
use seregazhuk\PinterestBot\Helpers\Pagination;
9
10
/**
11
 * Class Provider.
12
 * @property string entityIdName
13
 */
14
abstract class Provider
15
{
16
    /**
17
     * List of methods that require logged status.
18
     *
19
     * @var array
20
     */
21
    protected $loginRequiredFor = [];
22
23
    /**
24
     * Instance of the API Request.
25
     *
26
     * @var Request
27
     */
28
    protected $request;
29
30
    /**
31
     * @var Response
32
     */
33
    protected $response;
34
35
    /**
36
     * @param Request $request
37
     * @param Response $response
38
     */
39
    public function __construct(Request $request, Response $response)
40
    {
41
        $this->request = $request;
42
        $this->response = $response;
43
    }
44
45
    /**
46
     * Executes a POST request to Pinterest API.
47
     *
48
     * @param array $requestOptions
49
     * @param string $resourceUrl
50
     * @param bool $returnResponse
51
     *
52
     * @return Response|bool
53
     */
54
    protected function execPostRequest($requestOptions, $resourceUrl, $returnResponse = false)
55
    {
56
        $postString = Request::createQuery($requestOptions);
57
58
        $this->execute($resourceUrl, $postString);
59
60
        return $returnResponse ? $this->response : $this->response->isOk();
61
62
    }
63
64
    /**
65
     * Executes a GET request to Pinterest API.
66
     *
67
     * @param array $requestOptions
68
     * @param string $resourceUrl
69
     * @return array|bool
70
     */
71 View Code Duplication
    protected function execGetRequest(array $requestOptions = [], $resourceUrl = '')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
72
    {
73
        $query = Request::createQuery($requestOptions);
74
75
        $this->execute($resourceUrl . "?{$query}");
76
77
        return $this->response->getResponseData();
78
    }
79
80
    /**
81
     * Executes a GET request to Pinterest API with pagination.
82
     *
83
     * @param array $requestOptions
84
     * @param string $resourceUrl
85
     * @param array $bookmarks
86
     * @return Response
87
     */
88 View Code Duplication
    protected function execGetRequestWithPagination(array $requestOptions, $resourceUrl, $bookmarks = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
89
    {
90
        $query = Request::createQuery($requestOptions, $bookmarks);
91
92
        $this->execute($resourceUrl . "?{$query}");
93
94
        return $this->response;
95
    }
96
97
    /**
98
     * @param $url
99
     * @param string $postString
100
     * @return $this
101
     */
102
    protected function execute($url, $postString = "")
103
    {
104
        $result = $this->request->exec($url, $postString);
105
106
        $this->processResult($result);
107
108
        return $this;
109
    }
110
111
    /**
112
     * @return string
113
     */
114
    public function getEntityIdName()
115
    {
116
        return property_exists($this, 'entityIdName') ? $this->entityIdName : '';
117
    }
118
119
    /**
120
     * Executes pagination GET request.
121
     *
122
     * @param array $data
123
     * @param string $url
124
     * @param array $bookmarks
125
     * @return Response
126
     */
127
    public function getPaginatedData(array $data, $url, $bookmarks = [])
128
    {
129
        return $this->execGetRequestWithPagination($data, $url, $bookmarks);
130
    }
131
132
    /**
133
     * @param string $method
134
     *
135
     * @return bool
136
     */
137
    public function checkMethodRequiresLogin($method)
138
    {
139
        return in_array($method, $this->loginRequiredFor);
140
    }
141
142
    /**
143
     * @return bool
144
     */
145
    public function isLoggedIn()
146
    {
147
        return $this->request->isLoggedIn();
148
    }
149
150
    /**
151
     * @param string $res
152
     * @return Response
153
     */
154
    protected function processResult($res)
155
    {
156
        return $this->response->fill(json_decode($res, true));
157
    }
158
}
159