Completed
Push — master ( 8612fa...f1c4fe )
by Sergey
06:46 queued 03:23
created

Provider::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
namespace seregazhuk\PinterestBot\Api\Providers\Core;
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 post($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|Response
68
     */
69
    protected function get(array $requestOptions = [], $resourceUrl = '')
70
    {
71
        $query = Request::createQuery(
72
            $requestOptions,
73
            $this->response->getBookmarks()
74
        );
75
76
        $this->execute($resourceUrl . '?' . $query);
77
78
        return $this->response->hasBookmarks() ?
79
            $this->response :
80
            $this->response->getResponseData();
81
82
    }
83
84
    /**
85
     * @param $url
86
     * @param string $postString
87
     * @return $this
88
     */
89
    protected function execute($url, $postString = "")
90
    {
91
        $result = $this->request->exec($url, $postString);
92
93
        $this->response->fillFromJson($result);
94
95
        return $this;
96
    }
97
98
    /**
99
     * @param string $method
100
     *
101
     * @return bool
102
     */
103
    public function checkMethodRequiresLogin($method)
104
    {
105
        return in_array($method, $this->loginRequiredFor);
106
    }
107
108
    /**
109
     * @return bool
110
     */
111
    public function isLoggedIn()
112
    {
113
        return $this->request->isLoggedIn();
114
    }
115
116
    /**
117
     * @param mixed $data
118
     * @param string $resourceUrl
119
     * @param int $limit
120
     *
121
     * @return Pagination
122
     */
123
    protected function paginate($data, $resourceUrl, $limit = Pagination::DEFAULT_LIMIT)
124
    {
125
        return $this->paginateCustom(function() use ($data, $resourceUrl) {
126
                return $this->get($data, $resourceUrl);
127
            })->take($limit);
128
    }
129
130
    /**
131
     * @param callable $callback
132
     * @param int $limit
133
     * @return Pagination
134
     */
135
    protected function paginateCustom(callable $callback, $limit = Pagination::DEFAULT_LIMIT)
136
    {
137
        $this->response->clear();
138
139
        return (new Pagination)
140
            ->paginateOver($callback)
141
            ->take($limit);
142
    }
143
144
    /**
145
     * Simply makes GET request to some url.
146
     * @param string $url
147
     * @return array|bool
148
     */
149
    public function visitPage($url = '')
150
    {
151
        return $this->get([], $url);
152
    }
153
}
154