Completed
Branch master (23259a)
by Sergey
04:17 queued 48s
created

Provider::initTokenIfRequired()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 9
rs 9.6666
c 0
b 0
f 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 function seregazhuk\PinterestBot\class_uses_recursive;
8
use seregazhuk\PinterestBot\Helpers\Pagination;
9
use seregazhuk\PinterestBot\Api\ProvidersContainer;
10
11
/**
12
 * Class Provider.
13
 */
14
abstract class Provider
15
{
16
    /**
17
     * List of methods that require logged status.
18
     *
19
     * @return array
20
     */
21
    protected $loginRequiredFor = [];
22
23
    /**
24
     * Instance of the API Request.
25
     *
26
     * @var Request
27
     */
28
    protected $request;
29
30
    /**
31
     * @var Response
32
     */
33
    protected $response;
34
35
    /**
36
     * @var ProvidersContainer
37
     */
38
    protected $container;
39
40
    /**
41
     * @param ProvidersContainer $container
42
     */
43
    public function __construct(ProvidersContainer $container)
44
    {
45
        $this->container = $container;
46
        $this->request = $container->getRequest();
47
        $this->response = $container->getResponse();
48
    }
49
50
    /**
51
     * Executes a POST request to Pinterest API.
52
     *
53
     * @param string $resourceUrl
54
     * @param array $requestOptions
55
     * @param bool $returnData
56
     * @return bool|array
57
     */
58
    public function post($resourceUrl, array $requestOptions = [], $returnData = false)
59
    {
60
        $postString = $this->request->createQuery($requestOptions);
61
62
        // When executing POST request we need a csrf-token.
63
        $this->initTokenIfRequired();
64
65
        $this->execute($resourceUrl, $postString);
66
67
        return $returnData ?
68
            $this->response->getResponseData() :
69
            $this->response->isOk();
70
    }
71
72
    /**
73
     * Executes a GET request to Pinterest API.
74
     *
75
     * @param string $resourceUrl
76
     * @param array $requestOptions
77
     * @return array|bool
78
     */
79
    protected function get($resourceUrl = '', array $requestOptions = [])
80
    {
81
        $query = $this->request->createQuery(
82
            $requestOptions,
83
            $this->response->getBookmarks()
84
        );
85
86
        $this->execute($resourceUrl . '?' . $query);
87
88
        return $this->response->getResponseData();
89
    }
90
91
    /**
92
     * @param string $url
93
     * @param string $postString
94
     * @return $this
95
     */
96
    protected function execute($url, $postString = '')
97
    {
98
        $result = $this->request->exec($url, $postString);
99
100
        $this->response->fillFromJson($result);
101
102
        return $this;
103
    }
104
105
    /**
106
     * @param string $method
107
     *
108
     * @return bool
109
     */
110
    public function checkMethodRequiresLogin($method)
111
    {
112
        $methodsThatRequireLogin = array_merge($this->loginRequiredFor, self::requiresLoginFor());
0 ignored issues
show
Bug Best Practice introduced by
The method seregazhuk\PinterestBot\...der::requiresLoginFor() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

112
        $methodsThatRequireLogin = array_merge($this->loginRequiredFor, self::/** @scrutinizer ignore-call */ requiresLoginFor());
Loading history...
113
114
        return in_array($method, $methodsThatRequireLogin);
115
    }
116
117
    /**
118
     * @return array
119
     */
120
    protected function requiresLoginFor()
121
    {
122
        $loginRequired = [];
123
124
        foreach (class_parents($this) + class_uses_recursive($this) as $traitOrParent) {
125
            $class = basename(str_replace('\\', '/', $traitOrParent));
126
127
            if (method_exists($traitOrParent, $method = 'requiresLoginFor' . $class)) {
128
                $loginRequired = array_merge($loginRequired, forward_static_call([$this, $method]));
129
            }
130
        }
131
132
        return $loginRequired;
133
    }
134
135
    /**
136
     * @return bool
137
     */
138
    public function isLoggedIn()
139
    {
140
        return $this->request->isLoggedIn();
141
    }
142
143
    /**
144
     * @param string $resourceUrl
145
     * @param mixed $data
146
     * @param int $limit
147
     * @return Pagination
148
     */
149
    protected function paginate($resourceUrl, $data, $limit = Pagination::DEFAULT_LIMIT)
150
    {
151
        return $this
152
            ->paginateCustom(
153
                function () use ($data, $resourceUrl) {
154
                    $this->get($resourceUrl, $data);
155
                    return $this->response;
156
                }
157
            )->take($limit);
158
    }
159
160
    /**
161
     * Accepts callback which should return PaginatedResponse object.
162
     *
163
     * @param callable $callback
164
     * @param int $limit
165
     * @return Pagination
166
     */
167
    protected function paginateCustom(callable $callback, $limit = Pagination::DEFAULT_LIMIT)
168
    {
169
        $this->response->clear();
170
171
        return (new Pagination)
172
            ->paginateOver($callback)
173
            ->take($limit);
174
    }
175
176
    /**
177
     * @return Response
178
     */
179
    public function getResponse()
180
    {
181
        return $this->response;
182
    }
183
184
    /**
185
     * @return Request
186
     */
187
    public function getRequest()
188
    {
189
        return $this->request;
190
    }
191
192
    protected function initTokenIfRequired()
193
    {
194
        if ($this->request->hasToken()) {
195
            return;
196
        }
197
198
        // Simply visit main page to fill the cookies
199
        // and parse a token from them
200
        $this->get();
201
    }
202
}
203