Plan   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 132
Duplicated Lines 18.94 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 16
c 2
b 0
f 0
lcom 1
cbo 2
dl 25
loc 132
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A listPlans() 0 22 3
A getPlan() 18 18 2
B getAddonsForPlan() 0 24 6
A filterPlans() 7 12 3
A getPriceByPlanCode() 0 6 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
declare(strict_types=1);
3
4
namespace Zoho\Subscription\Api;
5
6
use Zoho\Subscription\Client\Client;
7
8
/**
9
 * Plan.
10
 *
11
 * @author Tristan Perchec <[email protected]>
12
 * @author Tristan Bessoussa <[email protected]>
13
 *
14
 * @link https://www.zoho.com/subscriptions/api/v1/#plans
15
 */
16
class Plan extends Client
17
{
18
    public static $addonTypes = [
19
        'recurring',
20
        'one_time',
21
    ];
22
23
    /**
24
     * Returns all plans.
25
     *
26
     * @param array $filters associative array of filters
27
     *
28
     * @throws \Exception
29
     *
30
     * @return array
31
     */
32
    public function listPlans(array $filters = [], bool $withAddons = true, string $addonType = null): array
33
    {
34
        $cacheKey = 'plans';
35
        $hit = $this->getFromCache($cacheKey);
36
37
        if (false === $hit) {
38
            $response = $this->sendRequest('GET', 'plans');
39
40
            $plans = $this->processResponse($response);
41
            $hit = $plans['plans'];
42
43
            $this->saveToCache($cacheKey, $hit);
44
        }
45
46
        $hit = $this->filterPlans($hit, $filters);
47
48
        if ($withAddons) {
49
            $hit = $this->getAddonsForPlan($hit, $addonType);
50
        }
51
52
        return $hit;
53
    }
54
55
    /**
56
     * Returns a Plan by its identifier.
57
     *
58
     * @param string $planCode
59
     *
60
     * @throws \Exception
61
     *
62
     * @return array
63
     */
64 View Code Duplication
    public function getPlan(string $planCode): array
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...
65
    {
66
        $cacheKey = sprintf('plan_%s', $planCode);
67
        $hit = $this->getFromCache($cacheKey);
68
69
        if (false === $hit) {
70
            $response = $this->sendRequest('GET', sprintf('plans/%s', $planCode));
71
72
            $data = $this->processResponse($response);
73
            $plan = $data['plan'];
74
75
            $this->saveToCache($cacheKey, $plan);
76
77
            return $plan;
78
        }
79
80
        return $hit;
81
    }
82
83
    /**
84
     * get reccurent addons for given plan.
85
     *
86
     * @param array  $plans
87
     * @param string|null $addonType
88
     *
89
     * @return array
90
     */
91
    public function getAddonsForPlan(array $plans, string $addonType = null): array
92
    {
93
        $addonApi = new Addon($this->token, $this->organizationId, $this->cache, $this->ttl);
94
95
        foreach ($plans as &$plan) {
96
            $addons = [];
97
98
            foreach ($plan['addons'] as $planAddon) {
99
                $addon = $addonApi->getAddon($planAddon['addon_code']);
100
101
                if (null !== $addonType) {
102
                    if (($addon['type'] == $addonType) && (in_array($addonType, self::$addonTypes))) {
103
                        $addons[] = $addon;
104
                    }
105
                } else {
106
                    $addons[] = $addon;
107
                }
108
            }
109
110
            $plan['addons'] = $addons;
111
        }
112
113
        return $plans;
114
    }
115
116
    /**
117
     * filter given plans with given filters.
118
     *
119
     * @param array $plans
120
     * @param array $filters
121
     *
122
     * @return array
123
     */
124
    public function filterPlans(array $plans, array $filters): array
125
    {
126 View Code Duplication
        foreach ($filters as $key => $filter) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
127
            if (array_key_exists($key, current($plans))) {
128
                $plans = array_filter($plans, function ($element) use ($key, $filter) {
129
                    return $element[$key] == $filter;
130
                });
131
            }
132
        }
133
134
        return $plans;
135
    }
136
137
    /**
138
     * get price by planCode
139
     *
140
     */
141
    public function getPriceByPlanCode(string $planCode): float
142
    {
143
        $plan = $this->getPlan($planCode);
144
145
        return (array_key_exists('recurring_price', $plan)) ? $plan['recurring_price'] : 0;
146
    }
147
}
148