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

Provider   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 105
Duplicated Lines 23.81 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 10
Bugs 2 Features 3
Metric Value
wmc 9
c 10
b 2
f 3
lcom 2
cbo 5
dl 25
loc 105
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A execPostRequest() 12 12 2
A execGetRequest() 13 13 2
A getRequest() 0 4 1
A getResponse() 0 4 1
A checkMethodRequiresLogin() 0 4 1
A getPaginatedData() 0 7 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\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