Completed
Push — master ( 523146...d3886c )
by Sergey
03:44
created

Provider   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 7
Bugs 0 Features 1
Metric Value
wmc 10
c 7
b 0
f 1
lcom 1
cbo 6
dl 0
loc 104
rs 10

6 Methods

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