Completed
Push — master ( 4d208b...c809e6 )
by Sergey
01:02
created

Provider::execPostRequest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 2
eloc 7
nc 2
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
use seregazhuk\PinterestBot\Contracts\RequestInterface;
8
use seregazhuk\PinterestBot\Contracts\ResponseInterface;
9
use seregazhuk\PinterestBot\Helpers\Providers\Traits\ProviderTrait;
10
11
/**
12
 * Class Provider.
13
 */
14
abstract class Provider
15
{
16
    use ProviderTrait;
17
18
    /**
19
     * List of methods that require logged status.
20
     *
21
     * @var array
22
     */
23
    protected $loginRequired = [];
24
25
    /**
26
     * Instance of the API RequestInterface.
27
     *
28
     * @var RequestInterface
29
     */
30
    protected $request;
31
32
    /**
33
     * Instance of the API ResponseInterface.
34
     *
35
     * @var ResponseInterface
36
     */
37
    protected $response;
38
39
    /**
40
     * @param RequestInterface  $request
41
     * @param ResponseInterface $response
42
     */
43
    public function __construct(RequestInterface $request, ResponseInterface $response)
44
    {
45
        $this->request = $request;
46
        $this->response = $response;
47
    }
48
49
    /**
50
     * Executes a POST request to Pinterest API.
51
     *
52
     * @param array  $requestOptions
53
     * @param string $resourceUrl
54
     * @param bool   $returnData
55
     *
56
     * @return mixed
57
     */
58
    public function execPostRequest($requestOptions, $resourceUrl, $returnData = false)
59
    {
60
        $data = ['options' => $requestOptions];
61
        $postString = Request::createQuery($data);
62
        $response = $this->request->exec($resourceUrl, $postString);
63
64
        if ($returnData) {
65
            return $this->response->getData($response);
66
        }
67
68
        return $this->response->checkResponse($response);
69
    }
70
71 View Code Duplication
    public function execPaginatedRequest($data, $url, $sourceUrl, $bookmarks = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
72
    {
73
        $data['options'] = $data;
74
        $response = $this->getRequest()->exec($url . '?' . Request::createQuery($data, $sourceUrl, $bookmarks));
75
76
        return $this->getResponse()->getPaginationData($response);
77
    }
78
79
    /**
80
     * @return Request
81
     */
82
    public function getRequest()
83
    {
84
        return $this->request;
85
    }
86
87
    /**
88
     * @return Response
89
     */
90
    public function getResponse()
91
    {
92
        return $this->response;
93
    }
94
95
    /**
96
     * @param string $method
97
     *
98
     * @return bool
99
     */
100
    public function checkMethodRequiresLogin($method)
101
    {
102
        return in_array($method, $this->loginRequired);
103
    }
104
}
105