Completed
Push — master ( 15775c...8cc55e )
by Dmitry
10:34
created

ApiTransactionRepository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
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
12
class ApiTransactionRepository implements TransactionRepositoryInterface
13
{
14
    /**
15
     * @var ConnectionInterface
16
     */
17
    private $connection;
18
19
    /**
20
     * ApiTransactionRepository constructor.
21
     * @param ConnectionInterface $connection
22
     */
23
    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...
24
    {
25
        $this->connection = $connection;
26
    }
27
28
    /**
29
     * @param string $id
30
     * @return Transaction
31
     * @throws TransactionException when transaction with the specified ID
32
     * does not exists
33
     */
34
    public function findById($id)
35
    {
36
        try {
37
            $data = $this->connection->createCommand()->perform('merchantTransactionGet', null, ['id' => $id]);
38
        } catch (ResponseErrorException $e) {
39
            throw new TransactionException('Failed to get transaction information');
40
        }
41
42
        if (empty($data)) {
43
            throw new TransactionException('Transaction not found');
44
        }
45
46
        return $this->instantiate($data);
47
    }
48
49
    /**
50
     * @inheritdoc
51
     */
52
    public function insert($transaction)
53
    {
54
        return $this->save($transaction);
55
    }
56
57
    /**
58
     * @inheritdoc
59
     */
60
    public function create($id, $merchant, $parameters)
61
    {
62
        $transaction = new Transaction($id, $merchant);
63
        $transaction->setParameters($parameters);
64
65
        return $transaction;
66
    }
67
68
    /**
69
     * @inheritdoc
70
     */
71
    public function save($transaction)
72
    {
73
        try {
74
            $data = $transaction->toArray();
75
            $data['parameters'] = json_encode($data['parameters']);
76
77
            $this->connection->createCommand()->perform('merchantTransactionSet', null, $data);
78
        } catch (ResponseErrorException $e) {
79
            throw new TransactionException('Failed to save transaction');
80
        }
81
82
        return $transaction;
83
    }
84
85
    /**
86
     * @param $data
87
     * @return Transaction
88
     */
89
    protected function instantiate($data)
90
    {
91
        $transaction = $this->create($data['id'], $data['merchant'], $data['parameters']);
92
93
        if ($data['status'] !== null) {
94
            $statusReflection = (new ReflectionObject($transaction))->getProperty('status');
95
            $statusReflection->setAccessible(true);
96
            $statusReflection->setValue($transaction, $data['status']);
97
            $statusReflection->setAccessible(false);
98
        }
99
100
        return $transaction;
101
    }
102
}
103