Completed
Push — master ( f63129...3a8a63 )
by Sergey
03:49
created

Provider::getResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
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
use seregazhuk\PinterestBot\Helpers\UrlHelper;
8
use seregazhuk\PinterestBot\Contracts\RequestInterface;
9
use seregazhuk\PinterestBot\Contracts\ResponseInterface;
10
use seregazhuk\PinterestBot\Helpers\Providers\Traits\ProviderTrait;
11
12
/**
13
 * Class Provider
14
 *
15
 * @package seregazhuk\PinterestBot\Contracts
16
 */
17
abstract class Provider
18
{
19
    use ProviderTrait;
20
21
    /**
22
     * List of methods that require logged status
23
     * @var array
24
     */
25
    protected $loginRequired = [];
26
27
    /**
28
     * Instance of the API RequestInterface
29
     *
30
     * @var RequestInterface
31
     */
32
    protected $request;
33
34
    /**
35
     * Instance of the API ResponseInterface
36
     *
37
     * @var ResponseInterface
38
     */
39
    protected $response;
40
41
    /**
42
     * @param RequestInterface $request
43
     * @param ResponseInterface $response
44
     */
45
    public function __construct(RequestInterface $request, ResponseInterface $response)
46
    {
47
        $this->request = $request;
48
        $this->response = $response;
49
    }
50
51
    /**
52
     * Executes a POST request to Pinterest API
53
     *
54
     * @param array  $requestOptions
55
     * @param string $resourceUrl
56
     * @param bool   $returnData
57
     * @return mixed
58
     */
59
    public function callPostRequest($requestOptions, $resourceUrl, $returnData = null)
60
    {
61
        $data = array("options" => $requestOptions);
62
        $request = Request::createRequestData($data);
63
64
        $postString = UrlHelper::buildRequestString($request);
65
        $response = $this->request->exec($resourceUrl, $postString);
66
67
        if ($returnData) {
68
            return $this->response->getData($response);
69
        }
70
71
        return $this->response->checkResponse($response);
72
    }
73
74
    /**
75
     * Run login check before every method if needed
76
     *
77
     * @param $method
78
     * @param $arguments
79
     * @return mixed
80
     */
81
    public function __call($method, $arguments)
82
    {
83
84
        if (method_exists($this, $method)) {
85
            $this->checkMethodForLoginNeed($method);
86
87
            return call_user_func_array(array($this, $method), $arguments);
88
        }
89
    }
90
91
    /**
92
     * Checks if method requires login
93
     *
94
     * @param $method
95
     */
96
    protected function checkMethodForLoginNeed($method)
97
    {
98
        if (in_array($method, $this->loginRequired)) {
99
            $this->request->checkLoggedIn();
100
        }
101
    }
102
103
104
    /**
105
     * @return Request
106
     */
107
    protected function getRequest()
108
    {
109
        return $this->request;
110
    }
111
112
    /**
113
     * @return Response
114
     */
115
    protected function getResponse()
116
    {
117
        return $this->response;
118
    }
119
}
120