Completed
Pull Request — master (#98)
by Sergey
02:49
created

Provider   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 91
Duplicated Lines 7.69 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 8
Bugs 2 Features 3
Metric Value
wmc 7
c 8
b 2
f 3
lcom 2
cbo 5
dl 7
loc 91
rs 10

6 Methods

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