Completed
Push — master ( 60aec0...f632bc )
by Andreas
12:47 queued 10:41
created

TransactionBuilder::purchase()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1.0156

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 3
cts 4
cp 0.75
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 1.0156
1
<?php
2
3
namespace Larium\Pay;
4
5
use ReflectionClass;
6
use Larium\CreditCard\CreditCard;
7
8
/**
9
 * A fluent interface ({@link http://martinfowler.com/bliki/FluentInterface.html}) for creating transaction objects.
10
 *
11
 * @author Andreas Kollaros <[email protected]>
12
 */
13
class TransactionBuilder
14
{
15
    private $transaction;
16
17
    private $amount;
0 ignored issues
show
Unused Code introduced by
The property $amount is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
18
19
    private $currency;
20
21
    private $transactionInstance;
22
23
    private $transactionId;
0 ignored issues
show
Unused Code introduced by
The property $transactionId is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
24
25
    private $extraOptions = [];
26
27
    private $description;
28
29
    private $clientIp;
30
31
    private $merchantReference;
32
33
    private $constructArgs = [];
34
35
    private $address = [];
36
37
    private $customerEmail;
38
39
    private static $saleMethods = [
40
        'currency',
41
        'description',
42
        'clientIp',
43
        'address',
44
        'customerEmail',
45
    ];
46
47 1
    private function __construct($transaction)
48
    {
49 1
        $this->transaction = $transaction;
50 1
    }
51
52 1
    public static function purchase($amount)
53
    {
54
        $instance = new self('Larium\Pay\Transaction\PurchaseTransaction');
55 1
        $instance->constructArgs[] = $amount;
56
57 1
        return $instance;
58
    }
59
60
    public static function authorize($amount)
61
    {
62
        $instance = new self('Larium\Pay\Transaction\AuthorizeTransaction');
63
        $instance->constructArgs[] = $amount;
64
65
        return $instance;
66
    }
67
68
    public static function capture($amount)
69
    {
70
        $instance = new self('Larium\Pay\Transaction\CaptureTransaction');
71
        $instance->constructArgs[] = $amount;
72
73
        return $instance;
74
    }
75
76
    public static function refund($amount)
77
    {
78
        $instance = new self('Larium\Pay\Transaction\RefundTransaction');
79
        $instance->constructArgs[] = $amount;
80
81
        return $instance;
82
    }
83
84
    public static function cancel($transactionId)
85
    {
86
        $instance = new self('Larium\Pay\Transaction\CancelTransaction');
87
        $instance->constructArgs[] = $transactionId;
88
89
        return $instance;
90
    }
91
92 1
    public function charge(CreditCard $card)
93
    {
94 1
        $this->constructArgs[] = $card;
95
96 1
        return $this;
97 1
    }
98
99 1
    public function with($currency)
100
    {
101 1
        $this->currency = $currency;
102
103 1
        return $this;
104
    }
105
106 1
    public function withExtraOptions(array $extraOptions)
107
    {
108 1
        $this->extraOptions = $extraOptions;
109
110 1
        return $this;
111 1
    }
112
113 1
    public function describedAs($description)
114
    {
115 1
        $this->description = $description;
116
117 1
        return $this;
118
    }
119
120 1
    public function billTo(array $address)
121
    {
122 1
        $this->address = $address;
123
124 1
        return $this;
125 1
    }
126
127 1
    public function mailTo($customerEmail)
128
    {
129 1
        $this->customerEmail = $customerEmail;
130
131 1
        return $this;
132
    }
133
134
    public function withMerchantReference($merchantReference)
135
    {
136
        $this->merchantReference = $merchantReference;
137
138
        return $this;
139
    }
140
141 1
    public function withClientIp($clientIp)
142
    {
143 1
        $this->clientIp = $clientIp;
144
145 1
        return $this;
146
    }
147
148 1
    public function getTransaction()
149
    {
150
        $this->build();
151
152 1
        return $this->transactionInstance;
153
    }
154
155 1
    private function build()
156
    {
157
        $reflection = new ReflectionClass($this->transaction);
158
159 1
        $args = $this->constructArgs;
160 1
        $args[] = $this->extraOptions;
161
162
        $this->transactionInstance = $reflection->newInstanceArgs($args);
163
164 1
        if ($this->transactionInstance instanceof Transaction\Sale) {
165 1
            foreach (self::$saleMethods as $prop) {
166
                $method = 'set' . (ucwords($prop));
167
                $this->transactionInstance->$method($this->$prop);
168
            }
169
        }
170 1
    }
171
}
172