Completed
Push — master ( 1ba8e6...74a8ab )
by Dmitry
04:31
created

ApiTransactionRepository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace hipanel\modules\finance\transaction;
4
5
use hiqdev\hiart\ConnectionInterface;
6
use hiqdev\hiart\ResponseErrorException;
7
use hiqdev\yii2\merchant\transactions\Transaction;
8
use hiqdev\yii2\merchant\transactions\TransactionException;
9
use hiqdev\yii2\merchant\transactions\TransactionRepositoryInterface;
10
use ReflectionObject;
11
use yii\helpers\Json;
12
13
class ApiTransactionRepository implements TransactionRepositoryInterface
14
{
15
    /**
16
     * @var ConnectionInterface
17
     */
18
    private $connection;
19
20
    /**
21
     * ApiTransactionRepository constructor.
22
     * @param ConnectionInterface $connection
23
     */
24
    function __construct(ConnectionInterface $connection)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
25
    {
26
        $this->connection = $connection;
27
    }
28
29
    /**
30
     * @param string $id
31
     * @return Transaction
32
     * @throws TransactionException when transaction with the specified ID
33
     * does not exists
34
     */
35
    public function findById($id)
36
    {
37
        try {
38
            $data = $this->callWithoutAuth(function () use ($id) {
39
                return $this->connection->createCommand()->perform('merchantTransactionGet', null, ['id' => $id]);
40
            });
41
        } catch (ResponseErrorException $e) {
42
            throw new TransactionException('Failed to get transaction information');
43
        }
44
45
        if (empty($data)) {
46
            throw new TransactionException('Transaction not found');
47
        }
48
49
        return $this->instantiate($data);
50
    }
51
52
    /**
53
     * @inheritdoc
54
     */
55
    public function insert($transaction)
56
    {
57
        return $this->save($transaction);
58
    }
59
60
    /**
61
     * @inheritdoc
62
     */
63
    public function create($id, $merchant, $parameters)
64
    {
65
        $transaction = new Transaction($id, $merchant);
66
        $transaction->setParameters($parameters);
67
68
        return $transaction;
69
    }
70
71
    /**
72
     * @inheritdoc
73
     */
74
    public function save($transaction)
75
    {
76
        try {
77
            $data = $transaction->toArray();
78
            $data['parameters'] = Json::encode($data['parameters']);
79
80
            $this->callWithoutAuth(function () use ($data) {
81
                return $this->connection->createCommand()->perform('merchantTransactionSet', null, $data);
82
            });
83
        } catch (ResponseErrorException $e) {
84
            throw new TransactionException('Failed to save transaction');
85
        }
86
87
        return $transaction;
88
    }
89
90
    private function callWithoutAuth(\Closure $function)
91
    {
92
        try {
93
            $this->connection->disableAuth();
94
            return call_user_func($function);
95
        } finally {
96
            $this->connection->enableAuth();
97
        }
98
    }
99
100
    /**
101
     * @param $data
102
     * @return Transaction
103
     */
104
    protected function instantiate($data)
105
    {
106
        $transaction = $this->create($data['id'], $data['merchant'], $data['parameters']);
107
108
        if ($data['success'] !== null) {
109
            $successReflection = (new ReflectionObject($transaction))->getProperty('success');
110
            $successReflection->setAccessible(true);
111
            $successReflection->setValue($transaction, $data['success']);
112
            $successReflection->setAccessible(false);
113
        }
114
115
        return $transaction;
116
    }
117
}
118