Completed
Push — master ( 932d72...8b424c )
by Sergey
03:15 queued 41s
created

Provider::execPostRequest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 4
nc 2
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
8
/**
9
 * Class Provider.
10
 * @property string entityIdName
11
 */
12
abstract class Provider
13
{
14
    /**
15
     * List of methods that require logged status.
16
     *
17
     * @var array
18
     */
19
    protected $loginRequiredFor = [];
20
21
    /**
22
     * Instance of the API Request.
23
     *
24
     * @var Request
25
     */
26
    protected $request;
27
28
    /**
29
     * @var Response
30
     */
31
    protected $response;
32
33
    /**
34
     * @param Request $request
35
     * @param Response $response
36
     */
37
    public function __construct(Request $request, Response $response)
38
    {
39
        $this->request = $request;
40
        $this->response = $response;
41
    }
42
43
    /**
44
     * Executes a POST request to Pinterest API.
45
     *
46
     * @param array $requestOptions
47
     * @param string $resourceUrl
48
     * @param bool $returnResponse
49
     *
50
     * @return Response|bool
51
     */
52
    protected function execPostRequest($requestOptions, $resourceUrl, $returnResponse = false)
53
    {
54
        $postString = Request::createQuery($requestOptions);
55
56
        $this->execute($resourceUrl, $postString);
57
58
        return $returnResponse ? $this->response : $this->response->isOk();
59
60
    }
61
62
    /**
63
     * Executes a GET request to Pinterest API.
64
     *
65
     * @param array $requestOptions
66
     * @param string $resourceUrl
67
     * @return array|bool
68
     */
69 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...
70
    {
71
        $query = Request::createQuery($requestOptions);
72
73
        $this->execute($resourceUrl . "?{$query}");
74
75
        return $this->response->getResponseData();
76
    }
77
78
    /**
79
     * Executes a GET request to Pinterest API with pagination.
80
     *
81
     * @param array $requestOptions
82
     * @param string $resourceUrl
83
     * @param array $bookmarks
84
     * @return Response
85
     */
86 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...
87
    {
88
        $query = Request::createQuery($requestOptions, $bookmarks);
89
90
        $this->execute($resourceUrl . "?{$query}");
91
92
        return $this->response;
93
    }
94
95
    /**
96
     * @param $url
97
     * @param string $postString
98
     * @return $this
99
     */
100
    protected function execute($url, $postString = "")
101
    {
102
        $result = $this->request->exec($url, $postString);
103
104
        $this->processResult($result);
105
106
        return $this;
107
    }
108
109
    /**
110
     * @return string
111
     */
112
    public function getEntityIdName()
113
    {
114
        return property_exists($this, 'entityIdName') ? $this->entityIdName : '';
115
    }
116
117
    /**
118
     * Executes pagination GET request.
119
     *
120
     * @param array $data
121
     * @param string $url
122
     * @param array $bookmarks
123
     * @return Response
124
     */
125
    public function getPaginatedData(array $data, $url, $bookmarks = [])
126
    {
127
        return $this->execGetRequestWithPagination($data, $url, $bookmarks);
128
    }
129
130
    /**
131
     * @param string $method
132
     *
133
     * @return bool
134
     */
135
    public function checkMethodRequiresLogin($method)
136
    {
137
        return in_array($method, $this->loginRequiredFor);
138
    }
139
140
    /**
141
     * @return bool
142
     */
143
    public function isLoggedIn()
144
    {
145
        return $this->request->isLoggedIn();
146
    }
147
148
    /**
149
     * Simply makes GET request to some url.
150
     * @param string $url
151
     * @return array|bool
152
     */
153
    public function visitPage($url = '')
154
    {
155
        return $this->execGetRequest([], $url);
156
    }
157
158
    /**
159
     * @param string $res
160
     * @return Response
161
     */
162
    protected function processResult($res)
163
    {
164
        return $this->response->fill(json_decode($res, true));
165
    }
166
}
167