Completed
Push — master ( 9da044...c40fa9 )
by Sergey
03:27 queued 01:08
created

Provider::execGetRequestWithPagination()   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 3
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
 */
11
abstract class Provider
12
{
13
    /**
14
     * List of methods that require logged status.
15
     *
16
     * @var array
17
     */
18
    protected $loginRequiredFor = [];
19
20
    /**
21
     * Instance of the API Request.
22
     *
23
     * @var Request
24
     */
25
    protected $request;
26
27
    /**
28
     * @var Response
29
     */
30
    protected $response;
31
32
    /**
33
     * @param Request $request
34
     * @param Response $response
35
     */
36
    public function __construct(Request $request, Response $response)
37
    {
38
        $this->request = $request;
39
        $this->response = $response;
40
    }
41
42
    /**
43
     * Executes a POST request to Pinterest API.
44
     *
45
     * @param array $requestOptions
46
     * @param string $resourceUrl
47
     * @param bool $returnResponse
48
     *
49
     * @return Response|bool
50
     */
51
    protected function execPostRequest($requestOptions, $resourceUrl, $returnResponse = false)
52
    {
53
        $postString = Request::createQuery($requestOptions);
54
55
        $this->execute($resourceUrl, $postString);
56
57
        return $returnResponse ? $this->response : $this->response->isOk();
58
59
    }
60
61
    /**
62
     * Executes a GET request to Pinterest API.
63
     *
64
     * @param array $requestOptions
65
     * @param string $resourceUrl
66
     * @param array $bookmarks
67
     * @return array|bool|Response
68
     */
69
    protected function execGetRequest(array $requestOptions = [], $resourceUrl = '', $bookmarks = null)
70
    {
71
        $query = Request::createQuery($requestOptions, $bookmarks);
0 ignored issues
show
Bug introduced by
It seems like $bookmarks defined by parameter $bookmarks on line 69 can also be of type null; however, seregazhuk\PinterestBot\Api\Request::createQuery() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
72
73
        $this->execute($resourceUrl . "?{$query}");
74
75
        return is_null($bookmarks) ?
76
            $this->response->getResponseData() :
77
            $this->response;
78
    }
79
80
    /**
81
     * @param $url
82
     * @param string $postString
83
     * @return $this
84
     */
85
    protected function execute($url, $postString = "")
86
    {
87
        $result = $this->request->exec($url, $postString);
88
89
        $this->processResult($result);
90
91
        return $this;
92
    }
93
94
    /**
95
     * @param string $method
96
     *
97
     * @return bool
98
     */
99
    public function checkMethodRequiresLogin($method)
100
    {
101
        return in_array($method, $this->loginRequiredFor);
102
    }
103
104
    /**
105
     * @return bool
106
     */
107
    public function isLoggedIn()
108
    {
109
        return $this->request->isLoggedIn();
110
    }
111
112
    /**
113
     * Simply makes GET request to some url.
114
     * @param string $url
115
     * @return array|bool
116
     */
117
    public function visitPage($url = '')
118
    {
119
        return $this->execGetRequest([], $url);
120
    }
121
122
    /**
123
     * @param string $res
124
     * @return Response
125
     */
126
    protected function processResult($res)
127
    {
128
        return $this->response->fill(json_decode($res, true));
129
    }
130
}
131