Completed
Pull Request — master (#142)
by Sergey
03:08
created

Provider::getResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
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;
4
5
use seregazhuk\PinterestBot\Api\Request;
6
use seregazhuk\PinterestBot\Api\Response;
7
8
/**
9
 * Class Provider.
10
 * @property string entityIdName
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
     * @param Request $request
30
     */
31
    public function __construct(Request $request)
32
    {
33
        $this->request = $request;
34
    }
35
36
    /**
37
     * Executes a POST request to Pinterest API.
38
     *
39
     * @param array $requestOptions
40
     * @param string $resourceUrl
41
     * @param bool $returnResponse
42
     *
43
     * @return Response|bool
44
     */
45
    protected function execPostRequest($requestOptions, $resourceUrl, $returnResponse = false)
46
    {
47
        $postString = Request::createQuery($requestOptions);
48
        $response = $this->request->exec($resourceUrl, $postString);
49
50
        return $returnResponse ? $response : $response->isOk();
51
    }
52
53
    /**
54
     * Executes a GET request to Pinterest API.
55
     *
56
     * @param array $requestOptions
57
     * @param string $resourceUrl
58
     *
59
     * @return Response
60
     */
61
    protected function execGetRequest(array $requestOptions = [], $resourceUrl = '')
62
    {
63
        $query = Request::createQuery($requestOptions);
64
        return $this->request->exec($resourceUrl . "?{$query}");
65
    }
66
67
    /**
68
     * Executes a GET request to Pinterest API with pagination.
69
     *
70
     * @param array $requestOptions
71
     * @param string $resourceUrl
72
     * @param array $bookmarks
73
     * @return Response
74
     */
75
    protected function execGetRequestWithPagination(array $requestOptions, $resourceUrl, $bookmarks = [])
76
    {
77
        $query = Request::createQuery($requestOptions, $bookmarks);
78
        return $this->request->exec($resourceUrl . "?{$query}");
79
    }
80
81
    /**
82
     * @return string
83
     */
84
    public function getEntityIdName()
85
    {
86
        return property_exists($this, 'entityIdName') ? $this->entityIdName : '';
87
    }
88
    
89
    /**
90
     * Executes pagination GET request.
91
     *
92
     * @param array $data
93
     * @param string $url
94
     * @param array $bookmarks
95
     * @return array|bool
96
     */
97
    public function getPaginatedData(array $data, $url, $bookmarks = [])
98
    {
99
        return $this->execGetRequestWithPagination($data, $url, $bookmarks);
100
    }
101
102
    /**
103
     * @param string $method
104
     *
105
     * @return bool
106
     */
107
    public function checkMethodRequiresLogin($method)
108
    {
109
        return in_array($method, $this->loginRequiredFor);
110
    }
111
112
    /**
113
     * @return Request
114
     */
115
    public function getRequest()
116
    {
117
        return $this->request;
118
    }
119
}
120