Completed
Pull Request — master (#258)
by Sergey
02:44
created

Provider::isLoggedIn()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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
use seregazhuk\PinterestBot\Helpers\UrlBuilder;
9
10
/**
11
 * Class Provider.
12
 */
13
abstract class Provider
14
{
15
    /**
16
     * List of methods that require logged status.
17
     *
18
     * @var array
19
     */
20
    protected $loginRequiredFor = [];
21
22
    /**
23
     * Instance of the API Request.
24
     *
25
     * @var Request
26
     */
27
    protected $request;
28
29
    /**
30
     * @var Response
31
     */
32
    protected $response;
33
34
    /**
35
     * @param Request $request
36
     * @param Response $response
37
     */
38
    public function __construct(Request $request, Response $response)
39
    {
40
        $this->request = $request;
41
        $this->response = $response;
42
    }
43
44
    /**
45
     * Executes a POST request to Pinterest API.
46
     *
47
     * @param array $requestOptions
48
     * @param string $resourceUrl
49
     *
50
     * @return Response|bool
51
     */
52
    protected function post($requestOptions, $resourceUrl)
53
    {
54
        $postString = Request::createQuery($requestOptions);
55
56
        $this->execute($resourceUrl, $postString);
57
58
        return $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->getResponseData();
79
80
    }
81
82
    /**
83
     * @param $url
84
     * @param string $postString
85
     * @return $this
86
     */
87
    protected function execute($url, $postString = "")
88
    {
89
        $result = $this->request->exec($url, $postString);
90
91
        $this->response->fillFromJson($result);
92
93
        return $this;
94
    }
95
96
    /**
97
     * @param string $method
98
     *
99
     * @return bool
100
     */
101
    public function checkMethodRequiresLogin($method)
102
    {
103
        return in_array($method, $this->loginRequiredFor);
104
    }
105
106
    /**
107
     * @return bool
108
     */
109
    public function isLoggedIn()
110
    {
111
        return $this->request->isLoggedIn();
112
    }
113
114
    /**
115
     * @param mixed $data
116
     * @param string $resourceUrl
117
     * @param int $limit
118
     *
119
     * @return Pagination
120
     */
121
    protected function paginate($data, $resourceUrl, $limit = Pagination::DEFAULT_LIMIT)
122
    {
123
        return $this
124
            ->paginateCustom(function () use ($data, $resourceUrl) {
125
                $this->get($data, $resourceUrl);
126
127
                return $this->response;
128
            })->take($limit);
129
    }
130
131
    /**
132
     * Accepts callback which should return PaginatedResponse object.
133
     *
134
     * @param callable $callback
135
     * @param int $limit
136
     * @return Pagination
137
     */
138
    protected function paginateCustom(callable $callback, $limit = Pagination::DEFAULT_LIMIT)
139
    {
140
        $this->response->clear();
141
142
        return (new Pagination)
143
            ->paginateOver($callback)
144
            ->take($limit);
145
    }
146
147
    /**
148
     * Simply makes GET request to some url.
149
     * @param string $url
150
     * @return array|bool
151
     */
152
    public function visitPage($url = '')
153
    {
154
        return $this->get([], $url);
155
    }
156
157
    /**
158
     * @return string|bool
159
     */
160
    protected function resolveCurrentUsername()
161
    {
162
        $currentUserProfile = $this->get([], UrlBuilder::RESOURCE_GET_USER_SETTINGS);
163
164
        if (!isset($currentUserProfile['username'])) return false;
165
166
        return $currentUserProfile['username'];
167
    }
168
}
169