Completed
Pull Request — 23 (#431)
by Harald
10:27
created

Subscription::_initialize()   D

Complexity

Conditions 10
Paths 32

Size

Total Lines 40
Code Lines 24

Duplication

Lines 10
Ratio 25 %

Importance

Changes 0
Metric Value
cc 10
eloc 24
nc 32
nop 1
dl 10
loc 40
rs 4.8196
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace Braintree;
3
4
/**
5
 * Braintree Subscription module
6
 *
7
 * <b>== More information ==</b>
8
 *
9
 * For more detailed information on Subscriptions, see {@link http://www.braintreepayments.com/gateway/subscription-api http://www.braintreepaymentsolutions.com/gateway/subscription-api}
10
 *
11
 * PHP Version 5
12
 *
13
 * @package   Braintree
14
 * @copyright 2015 Braintree, a division of PayPal, Inc.
15
 */
16
class Subscription extends Base
17
{
18
    const ACTIVE = 'Active';
19
    const CANCELED = 'Canceled';
20
    const EXPIRED = 'Expired';
21
    const PAST_DUE = 'Past Due';
22
    const PENDING = 'Pending';
23
24
    // Subscription Sources
25
    const API           = 'api';
26
    const CONTROL_PANEL = 'control_panel';
27
    const RECURRING     = 'recurring';
28
29
    /**
30
     * @ignore
31
     */
32
    public static function factory($attributes)
33
    {
34
        $instance = new self();
35
        $instance->_initialize($attributes);
36
37
        return $instance;
38
    }
39
40
    /**
41
     * @ignore
42
     */
43
    protected function _initialize($attributes)
44
    {
45
        $this->_attributes = $attributes;
46
47
        $addOnArray = [];
48 View Code Duplication
        if (isset($attributes['addOns'])) {
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...
49
            foreach ($attributes['addOns'] AS $addOn) {
50
                $addOnArray[] = AddOn::factory($addOn);
51
            }
52
        }
53
        $this->_attributes['addOns'] = $addOnArray;
54
55
        $discountArray = [];
56 View Code Duplication
        if (isset($attributes['discounts'])) {
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...
57
            foreach ($attributes['discounts'] AS $discount) {
58
                $discountArray[] = Discount::factory($discount);
59
            }
60
        }
61
        $this->_attributes['discounts'] = $discountArray;
62
63
        if (isset($attributes['descriptor'])) {
64
            $this->_set('descriptor', new Descriptor($attributes['descriptor']));
65
        }
66
67
        $statusHistory = [];
68
        if (isset($attributes['statusHistory'])) {
69
            foreach ($attributes['statusHistory'] AS $history) {
70
                $statusHistory[] = new Subscription\StatusDetails($history);
71
            }
72
        }
73
        $this->_attributes['statusHistory'] = $statusHistory;
74
75
        $transactionArray = [];
76
        if (isset($attributes['transactions'])) {
77
            foreach ($attributes['transactions'] AS $transaction) {
78
                $transactionArray[] = Transaction::factory($transaction);
79
            }
80
        }
81
        $this->_attributes['transactions'] = $transactionArray;
82
    }
83
84
    /**
85
     * returns a string representation of the customer
86
     * @return string
87
     */
88
    public function  __toString()
89
    {
90
        $excludedAttributes = ['statusHistory'];
91
92
        $displayAttributes = [];
93
        foreach($this->_attributes as $key => $val) {
94
            if (!in_array($key, $excludedAttributes)) {
95
                $displayAttributes[$key] = $val;
96
            }
97
        }
98
99
        return __CLASS__ . '[' .
100
                Util::attributesToString($displayAttributes) .']';
101
    }
102
103
104
    // static methods redirecting to gateway
105
106
    public static function create($attributes)
107
    {
108
        return Configuration::gateway()->subscription()->create($attributes);
109
    }
110
111
    public static function find($id)
112
    {
113
        return Configuration::gateway()->subscription()->find($id);
114
    }
115
116
    public static function search($query)
117
    {
118
        return Configuration::gateway()->subscription()->search($query);
119
    }
120
121
    public static function fetch($query, $ids)
122
    {
123
        return Configuration::gateway()->subscription()->fetch($query, $ids);
124
    }
125
126
    public static function update($subscriptionId, $attributes)
127
    {
128
        return Configuration::gateway()->subscription()->update($subscriptionId, $attributes);
129
    }
130
131
    public static function retryCharge($subscriptionId, $amount = null)
132
    {
133
        return Configuration::gateway()->subscription()->retryCharge($subscriptionId, $amount);
134
    }
135
136
    public static function cancel($subscriptionId)
137
    {
138
        return Configuration::gateway()->subscription()->cancel($subscriptionId);
139
    }
140
}
141
class_alias('Braintree\Subscription', 'Braintree_Subscription');
142