Passed
Push — master ( 8fb633...075a6a )
by mahdi
06:46
created

Zarinpal::getFreshStrategyInstance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 4
c 1
b 0
f 1
nc 2
nop 2
dl 0
loc 9
rs 10
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
     * Zarinpal constructor.
39
     * Construct the class with the relevant settings.
40
     *
41
     * @param Invoice $invoice
42
     * @param $settings
43
     */
44
    public function __construct(Invoice $invoice, $settings)
45
    {
46
        $this->strategy = $this->getFreshStrategyInstance($invoice, $settings);
47
    }
48
49
    /**
50
     * Purchase Invoice.
51
     *
52
     * @return string
53
     *
54
     * @throws PurchaseFailedException
55
     * @throws \SoapFault
56
     */
57
    public function purchase()
58
    {
59
        return $this->strategy->purchase();
60
    }
61
62
    /**
63
     * Pay the Invoice
64
     *
65
     * @return RedirectionForm
66
     */
67
    public function pay() : RedirectionForm
68
    {
69
        return $this->strategy->pay();
70
    }
71
72
    /**
73
     * Verify payment
74
     *
75
     * @return ReceiptInterface
76
     *
77
     * @throws InvalidPaymentException
78
     * @throws \SoapFault
79
     */
80
    public function verify() : ReceiptInterface
81
    {
82
        return $this->strategy->verify();
83
    }
84
85
    /**
86
     * Get zarinpal payment's strategy according to config's mode.
87
     *
88
     * @param Invoice $invoice
89
     * @param $settings
90
     * @return DriverInterface
91
     */
92
    protected function getFreshStrategyInstance($invoice, $settings) : DriverInterface
93
    {
94
        $strategy = static::$strategies[$this->getMode()] ?? null;
95
96
        if (! $strategy) {
97
            $this->strategyNotFound();
98
        }
99
100
        return new $strategy($invoice, $settings);
101
    }
102
103
    protected function strategyNotFound()
104
    {
105
        $message = sprintf(
106
            'Zarinpal payment mode not found (check your settings), valid modes are: %s', 
107
            implode(',', array_keys(static::$strategies))
108
        );
109
110
        throw new DriverNotFoundException($message);
111
    }
112
113
    /**
114
     * Retrieve payment mode.
115
     *
116
     * @return string
117
     */
118
    protected function getMode() : string
119
    {
120
        return strtolower($this->settings->mode);
121
    }
122
}
123