Completed
Push — master ( cc9e5c...67ffd6 )
by Sergey
13:28 queued 10:12
created

Provider::getEntityIdName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 2
eloc 2
nc 2
nop 0
1
<?php
2
3
namespace seregazhuk\PinterestBot\Api\Providers;
4
5
use seregazhuk\PinterestBot\Api\Request;
6
use seregazhuk\PinterestBot\Contracts\RequestInterface;
7
use seregazhuk\PinterestBot\Contracts\ResponseInterface;
8
9
/**
10
 * Class Provider.
11
 */
12
abstract class Provider
13
{
14
    /**
15
     * List of methods that require logged status.
16
     *
17
     * @var array
18
     */
19
    protected $loginRequiredFor = [];
20
21
    /**
22
     * Instance of the API RequestInterface.
23
     *
24
     * @var RequestInterface
25
     */
26
    protected $request;
27
28
    /**
29
     * Instance of the API ResponseInterface.
30
     *
31
     * @var ResponseInterface
32
     */
33
    protected $response;
34
35
    /**
36
     * @param RequestInterface  $request
37
     * @param ResponseInterface $response
38
     */
39
    public function __construct(RequestInterface $request, ResponseInterface $response)
40
    {
41
        $this->request = $request;
42
        $this->response = $response;
43
    }
44
45
    /**
46
     * Executes a POST request to Pinterest API.
47
     *
48
     * @param array  $requestOptions
49
     * @param string $resourceUrl
50
     * @param bool   $returnData
51
     *
52
     * @return mixed
53
     */
54
    protected function execPostRequest($requestOptions, $resourceUrl, $returnData = false)
55
    {
56
        $postString = Request::createQuery(['options' => $requestOptions]);
57
        $response = $this->request->exec($resourceUrl, $postString);
58
59
        if ($returnData) {
60
            return $this->response->getData($response);
61
        }
62
63
        return $this->response->hasErrors($response);
64
    }
65
66
    /**
67
     * Executes a GET request to Pinterest API.
68
     *
69
     * @param array $requestOptions
70
     * @param string $resourceUrl
71
     * @return array|bool
72
     */
73 View Code Duplication
    protected function execGetRequest(array $requestOptions, $resourceUrl)
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...
74
    {
75
        $query = Request::createQuery(['options' => $requestOptions]);
76
        $response = $this->request->exec($resourceUrl . "?{$query}");
77
        
78
        return $this->response->getData($response);
79
    }
80
81
    /**
82
     * Executes a GET request to Pinterest API with pagination.
83
     *
84
     * @param array $requestOptions
85
     * @param string $resourceUrl
86
     * @param array $bookmarks
87
     * @return array|bool
88
     */
89 View Code Duplication
    protected function execGetRequestWithPagination(array $requestOptions, $resourceUrl, $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...
90
    {
91
        $query = Request::createQuery(['options' => $requestOptions], $bookmarks);
92
        $response = $this->request->exec($resourceUrl . "?{$query}");
93
94
        return $this->response->getPaginationData($response);
95
    }
96
97
    protected function getEntityIdName()
98
    {
99
        return property_exists($this, 'entityIdName') ? $this->entityIdName : '';
0 ignored issues
show
Bug introduced by
The property entityIdName does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
100
    }
101
    
102
    /**
103
     * Executes pagination GET request.
104
     *
105
     * @param array $data
106
     * @param string $url
107
     * @param array $bookmarks
108
     * @return array|bool
109
     */
110
    public function getPaginatedData(array $data, $url, $bookmarks = [])
111
    {
112
        return $this->execGetRequestWithPagination($data, $url, $bookmarks);
113
    }
114
115
    /**
116
     * @param string $method
117
     *
118
     * @return bool
119
     */
120
    public function checkMethodRequiresLogin($method)
121
    {
122
        return in_array($method, $this->loginRequiredFor);
123
    }
124
125
    /**
126
     * @return RequestInterface
127
     */
128
    public function getRequest()
129
    {
130
        return $this->request;
131
    }
132
133
    /**
134
     * @return ResponseInterface
135
     */
136
    public function getResponse()
137
    {
138
        return $this->response;
139
    }
140
}
141