MollieDriver   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 10
lcom 1
cbo 9
dl 0
loc 94
rs 10
c 1
b 1
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getBanks() 0 14 3
A execute() 0 18 2
A confirm() 0 10 2
A getFile() 0 8 2
1
<?php
2
/*
3
* (c) Wessel Strengholt <[email protected]>
4
*
5
* For the full copyright and license information, please view the LICENSE
6
* file that was distributed with this source code.
7
*/
8
9
namespace Usoft\IDealBundle\Driver;
10
11
use Mollie_API_Client;
12
use Mollie_API_Object_Method;
13
use Usoft\IDealBundle\Model\Bank;
14
use Usoft\IDealBundle\Exceptions\BankLoaderException;
15
use Symfony\Component\HttpFoundation\RedirectResponse;
16
use Usoft\IDealBundle\Exceptions\IDealExecuteException;
17
use Usoft\IDealBundle\Exceptions\InvalidPaymentKeyException;
18
19
/**
20
 * Class MollieDriver
21
 *
22
 * @author Wessel Strengholt <[email protected]>
23
 */
24
class MollieDriver implements IDealInterface
25
{
26
    /** @var string */
27
    private $description;
28
29
    /** @var Mollie_API_Client */
30
    private $mollie;
31
32
    /**
33
     * @param Mollie_API_Client $client
34
     * @param string            $key
35
     * @param string            $description
36
     */
37
    public function __construct(Mollie_API_Client $client, $key, $description)
38
    {
39
        $this->description = $description;
40
        $client->setApiKey($key);
41
        $this->mollie = $client;
42
    }
43
44
    /**
45
     * @return Bank[]
46
     */
47
    public function getBanks()
48
    {
49
        $bankList = [];
50
        try {
51
            $issuers = $this->mollie->issuers->all();
52
            foreach ($issuers as $issuer) {
53
                $bankList[] = new Bank($issuer->id, $issuer->name);
54
            }
55
56
            return $bankList;
57
        } catch (\Exception $exception) {
58
            throw new BankLoaderException('Cannot load issuers from mollie');
59
        }
60
    }
61
62
    /**
63
     * @param Bank   $bank
64
     * @param float  $amount
65
     * @param string $returnUrl
66
     *
67
     * @return RedirectResponse
68
     *
69
     * @throws IDealExecuteException
70
     */
71
    public function execute(Bank $bank, $amount, $returnUrl)
72
    {
73
        try {
74
            $payment = $this->mollie->payments->create([
75
                "amount"      => $amount,
76
                "description" => $this->description,
77
                "redirectUrl" => $returnUrl,
78
                "method"      => Mollie_API_Object_Method::IDEAL,
79
                "issuer"      => $bank->getId(),
80
            ]);
81
82
            file_put_contents($this->getFile(), $payment->id);
83
84
            return new RedirectResponse($payment->getPaymentUrl());
85
        } catch (\Exception $exception) {
86
            throw new IDealExecuteException($exception->getMessage());
87
        }
88
    }
89
90
    /**
91
     * @return bool
92
     */
93
    public function confirm()
94
    {
95
        try {
96
            $key = file_get_contents($this->getFile());
97
98
            return $this->mollie->payments->get($key)->isPaid();
99
        } catch (\Exception $exception) {
100
            return false;
101
        }
102
    }
103
104
    /**
105
     * @return string
106
     *
107
     * @throws InvalidPaymentKeyException
108
     */
109
    private function getFile()
110
    {
111
        try {
112
            return sys_get_temp_dir() . DIRECTORY_SEPARATOR . session_id();
113
        } catch (\Exception $exception) {
114
            throw new InvalidPaymentKeyException('Cannot resolve payment key');
115
        }
116
    }
117
}
118