Completed
Push — master ( 7d378c...9da044 )
by Sergey
03:36 queued 01:11
created

Provider::execGetRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
dl 8
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 2
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
     * Executes pagination GET request.
111
     *
112
     * @param array $data
113
     * @param string $url
114
     * @param array $bookmarks
115
     * @return Response
116
     */
117
    public function getPaginatedData(array $data, $url, $bookmarks = [])
118
    {
119
        return $this->execGetRequestWithPagination($data, $url, $bookmarks);
120
    }
121
122
    /**
123
     * @param string $method
124
     *
125
     * @return bool
126
     */
127
    public function checkMethodRequiresLogin($method)
128
    {
129
        return in_array($method, $this->loginRequiredFor);
130
    }
131
132
    /**
133
     * @return bool
134
     */
135
    public function isLoggedIn()
136
    {
137
        return $this->request->isLoggedIn();
138
    }
139
140
    /**
141
     * Simply makes GET request to some url.
142
     * @param string $url
143
     * @return array|bool
144
     */
145
    public function visitPage($url = '')
146
    {
147
        return $this->execGetRequest([], $url);
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