Completed
Push — master ( 31d905...b5d7ba )
by Sergey
05:31 queued 02:23
created

Provider   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 124
Duplicated Lines 11.29 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 17
Bugs 3 Features 3
Metric Value
wmc 9
c 17
b 3
f 3
lcom 2
cbo 3
dl 14
loc 124
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A execPostRequest() 0 11 2
A execGetRequest() 7 7 1
A execGetRequestWithPagination() 7 7 1
A getPaginatedData() 0 4 1
A checkMethodRequiresLogin() 0 4 1
A getRequest() 0 4 1
A getResponse() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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