Completed
Pull Request — master (#100)
by Sergey
02:24
created

Provider::execGetRequest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 13
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 13
loc 13
rs 9.4285
cc 2
eloc 7
nc 2
nop 4
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 View Code Duplication
    public function execPostRequest($requestOptions, $resourceUrl, $returnData = false)
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...
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 execGetRequest($requestOptions, $resourceUrl, $needsPagination = false, $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
        $query = Request::createQuery(
74
            ['options' => $requestOptions], '', $bookmarks
75
        );
76
77
        $response = $this->request->exec($resourceUrl . "?{$query}");
78
        if ($needsPagination) {
79
            return $this->response->getPaginationData($response);
80
        }
81
82
        return $this->response->getData($response);
83
    }
84
    
85
    /**
86
     * @return Request
87
     */
88
    public function getRequest()
89
    {
90
        return $this->request;
91
    }
92
93
    /**
94
     * @return Response
95
     */
96
    public function getResponse()
97
    {
98
        return $this->response;
99
    }
100
101
    /**
102
     * @param string $method
103
     *
104
     * @return bool
105
     */
106
    public function checkMethodRequiresLogin($method)
107
    {
108
        return in_array($method, $this->loginRequired);
109
    }
110
111
    public function getPaginatedData($data, $url, $sourceUrl, $bookmarks = [])
112
    {
113
        $data['options'] = $data;
114
        $response = $this->getRequest()->exec($url . '?' . Request::createQuery($data, $sourceUrl, $bookmarks));
115
116
        return $this->getResponse()->getPaginationData($response);
117
    }
118
}
119