Completed
Push — master ( f77348...ad6f80 )
by Sergey
03:41
created

Provider::__call()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
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\Exceptions\AuthException;
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
        $postString = Request::createQuery($data);
63
        $response = $this->request->exec($resourceUrl, $postString);
64
65
        if ($returnData) {
66
            return $this->response->getData($response);
67
        }
68
69
        return $this->response->checkResponse($response);
70
    }
71
72
    /**
73
     * Run login check before every method if needed
74
     *
75
     * @param $method
76
     * @param $arguments
77
     * @return mixed
78
     */
79
    public function __call($method, $arguments)
80
    {
81
        if (method_exists($this, $method)) {
82
            $this->checkMethodForLoginRequired($method);
83
84
            return call_user_func_array(array($this, $method), $arguments);
85
        }
86
    }
87
88
    /**
89
     * Checks if method requires login and if true,
90
     * checks logged in status.
91
     *
92
     * @param $method
93
     * @throws AuthException if is not logged in
94
     */
95
    protected function checkMethodForLoginRequired($method)
96
    {
97
        if (in_array($method, $this->loginRequired) && ! $this->request->isLoggedIn()) {
98
            throw new AuthException("You must log in before.");
99
        }
100
    }
101
102
    /**
103
     * @return Request
104
     */
105
    protected function getRequest()
106
    {
107
        return $this->request;
108
    }
109
110
    /**
111
     * @return Response
112
     */
113
    protected function getResponse()
114
    {
115
        return $this->response;
116
    }
117
}
118