Zarinpal   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 23
c 2
b 0
f 1
dl 0
loc 120
rs 10
wmc 8

7 Methods

Rating   Name   Duplication   Size   Complexity  
A verify() 0 3 1
A pay() 0 3 1
A getFreshStrategyInstance() 0 9 2
A purchase() 0 3 1
A strategyNotFound() 0 8 1
A __construct() 0 5 1
A getMode() 0 3 1
1
<?php
2
3
namespace Shetabit\Multipay\Drivers\Zarinpal;
4
5
use Shetabit\Multipay\Abstracts\Driver;
6
use Shetabit\Multipay\Contracts\DriverInterface;
7
use Shetabit\Multipay\Exceptions\InvalidPaymentException;
8
use Shetabit\Multipay\Exceptions\PurchaseFailedException;
9
use Shetabit\Multipay\Contracts\ReceiptInterface;
10
use Shetabit\Multipay\Drivers\Zarinpal\Strategies\Normal;
11
use Shetabit\Multipay\Drivers\Zarinpal\Strategies\Sandbox;
12
use Shetabit\Multipay\Drivers\Zarinpal\Strategies\Zaringate;
13
use Shetabit\Multipay\Exceptions\DriverNotFoundException;
14
use Shetabit\Multipay\Invoice;
15
use Shetabit\Multipay\RedirectionForm;
16
17
class Zarinpal extends Driver
18
{
19
    /**
20
     * Strategies map.
21
     *
22
     * @var array
23
     */
24
    public static $strategies = [
25
        'normal' => Normal::class,
26
        'sandbox' => Sandbox::class,
27
        'zaringate' => Zaringate::class,
28
    ];
29
30
    /**
31
     * Current strategy instance.
32
     *
33
     * @var DriverInterface $strategy
34
     */
35
    protected $strategy;
36
37
    /**
38
     * Invoice
39
     *
40
     * @var Invoice
41
     */
42
    protected $invoice;
43
44
    /**
45
     * Driver settings
46
     *
47
     * @var object
48
     */
49
    protected $settings;
50
51
    /**
52
     * Zarinpal constructor.
53
     * Construct the class with the relevant settings.
54
     *
55
     * @param Invoice $invoice
56
     * @param $settings
57
     */
58
    public function __construct(Invoice $invoice, $settings)
59
    {
60
        $this->invoice = $invoice;
61
        $this->settings = (object) $settings;
62
        $this->strategy = $this->getFreshStrategyInstance($this->invoice, $this->settings);
63
    }
64
65
    /**
66
     * Purchase Invoice.
67
     *
68
     * @return string
69
     *
70
     * @throws PurchaseFailedException
71
     * @throws \SoapFault
72
     */
73
    public function purchase()
74
    {
75
        return $this->strategy->purchase();
76
    }
77
78
    /**
79
     * Pay the Invoice
80
     *
81
     * @return RedirectionForm
82
     */
83
    public function pay() : RedirectionForm
84
    {
85
        return $this->strategy->pay();
86
    }
87
88
    /**
89
     * Verify payment
90
     *
91
     * @return ReceiptInterface
92
     *
93
     * @throws InvalidPaymentException
94
     * @throws \SoapFault
95
     */
96
    public function verify() : ReceiptInterface
97
    {
98
        return $this->strategy->verify();
99
    }
100
101
    /**
102
     * Get zarinpal payment's strategy according to config's mode.
103
     *
104
     * @param Invoice $invoice
105
     * @param $settings
106
     * @return DriverInterface
107
     */
108
    protected function getFreshStrategyInstance($invoice, $settings) : DriverInterface
109
    {
110
        $strategy = static::$strategies[$this->getMode()] ?? null;
111
112
        if (! $strategy) {
113
            $this->strategyNotFound();
114
        }
115
116
        return new $strategy($invoice, $settings);
117
    }
118
119
    protected function strategyNotFound()
120
    {
121
        $message = sprintf(
122
            'Zarinpal payment mode not found (check your settings), valid modes are: %s',
123
            implode(',', array_keys(static::$strategies))
124
        );
125
126
        throw new DriverNotFoundException($message);
127
    }
128
129
    /**
130
     * Retrieve payment mode.
131
     *
132
     * @return string
133
     */
134
    protected function getMode() : string
135
    {
136
        return strtolower($this->settings->mode);
137
    }
138
}
139