Completed
Push — master ( 02758b...d248a0 )
by Olivier
02:14
created

Plan   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 69
Duplicated Lines 18.84 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 29.63%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 4
dl 13
loc 69
ccs 8
cts 27
cp 0.2963
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 10 2
A all() 0 10 2
A create() 0 18 3
A update() 13 13 2

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
declare(strict_types=1);
4
5
/*
6
 * This software may be modified and distributed under the terms
7
 * of the MIT license. See the LICENSE file for details.
8
 */
9
10
namespace Shapin\Stripe\Api;
11
12
use Shapin\Stripe\Configuration;
13
use Shapin\Stripe\Exception;
14
use Shapin\Stripe\Model\Plan\Plan as PlanModel;
15
use Shapin\Stripe\Model\Plan\PlanCollection;
16
use Symfony\Component\Config\Definition\Processor;
17
18
final class Plan extends HttpApi
19
{
20
    /**
21
     * @throws Exception
22
     */
23 1
    public function get(string $id)
24
    {
25 1
        $response = $this->httpGet("/v1/plans/$id");
26
27 1
        if (200 !== $response->getStatusCode()) {
28
            $this->handleErrors($response);
29
        }
30
31 1
        return $this->hydrator->hydrate($response, PlanModel::class);
32
    }
33
34
    /**
35
     * @throws Exception
36
     */
37 1
    public function all(array $params = [])
38
    {
39 1
        $response = $this->httpGet('/v1/plans'.http_build_query($params));
40
41 1
        if (200 !== $response->getStatusCode()) {
42
            $this->handleErrors($response);
43
        }
44
45 1
        return $this->hydrator->hydrate($response, PlanCollection::class);
46
    }
47
48
    /**
49
     * @throws Exception
50
     */
51
    public function create(array $params)
52
    {
53
        $processor = new Processor();
54
        $params = $processor->processConfiguration(new Configuration\PlanCreate(), [$params]);
55
56
        if (isset($params['existing_product'])) {
57
            $params['product'] = $params['existing_product'];
58
            unset($params['existing_product']);
59
        }
60
61
        $response = $this->httpPostRaw('/v1/plans', http_build_query($params), ['Content-Type' => 'application/x-www-form-urlencoded']);
62
63
        if (200 !== $response->getStatusCode()) {
64
            $this->handleErrors($response);
65
        }
66
67
        return $this->hydrator->hydrate($response, PlanModel::class);
68
    }
69
70
    /**
71
     * @throws Exception
72
     */
73 View Code Duplication
    public function update(string $id, array $params)
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
        $processor = new Processor();
76
        $processor->processConfiguration(new Configuration\PlanUpdate(), [$params]);
77
78
        $response = $this->httpPostRaw("/v1/plans/$id", http_build_query($params), ['Content-Type' => 'application/x-www-form-urlencoded']);
79
80
        if (200 !== $response->getStatusCode()) {
81
            $this->handleErrors($response);
82
        }
83
84
        return $this->hydrator->hydrate($response, PlanModel::class);
85
    }
86
}
87