Completed
Push — master ( c40fa9...515296 )
by Sergey
51s
created

Provider::visitPage()   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
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
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
 */
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
     * @param array $bookmarks
68
     * @return array|bool|Response
69
     */
70
    protected function execGetRequest(array $requestOptions = [], $resourceUrl = '', $bookmarks = null)
71
    {
72
        $query = Request::createQuery($requestOptions, $bookmarks);
73
74
        $this->execute($resourceUrl . "?{$query}");
75
76
        return is_null($bookmarks) ?
77
            $this->response->getResponseData() :
78
            $this->response;
79
    }
80
81
    /**
82
     * @param $url
83
     * @param string $postString
84
     * @return $this
85
     */
86
    protected function execute($url, $postString = "")
87
    {
88
        $result = $this->request->exec($url, $postString);
89
90
        $this->processResult($result);
91
92
        return $this;
93
    }
94
95
    /**
96
     * @param string $method
97
     *
98
     * @return bool
99
     */
100
    public function checkMethodRequiresLogin($method)
101
    {
102
        return in_array($method, $this->loginRequiredFor);
103
    }
104
105
    /**
106
     * @return bool
107
     */
108
    public function isLoggedIn()
109
    {
110
        return $this->request->isLoggedIn();
111
    }
112
113
    /**
114
     * @param array  $data
115
     * @param string $resourceUrl
116
     * @param int $limit
117
     *
118
     * @return Pagination
119
     */
120
    protected function paginate($data, $resourceUrl, $limit = Pagination::DEFAULT_LIMIT)
121
    {
122
        return (new Pagination($limit))
123
            ->paginateOver(function($bookmarks = []) use ($data, $resourceUrl) {
124
                return $this->execGetRequest($data, $resourceUrl, $bookmarks);
125
            });
126
    }
127
128
    /**
129
     * Simply makes GET request to some url.
130
     * @param string $url
131
     * @return array|bool
132
     */
133
    public function visitPage($url = '')
134
    {
135
        return $this->execGetRequest([], $url);
136
    }
137
138
    /**
139
     * @param string $res
140
     * @return Response
141
     */
142
    protected function processResult($res)
143
    {
144
        return $this->response->fill(json_decode($res, true));
145
    }
146
}
147