Passed
Push — master ( 3ccf9f...992767 )
by Anton
08:04 queued 06:49
created

Service::addBlock()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 11
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 19
rs 9.9
1
<?php
2
/**
3
 * @copyright Bluz PHP Team
4
 * @link      https://github.com/bluzphp/skeleton
5
 */
6
namespace Application\Wallets;
7
8
use Application\Transactions\Table as TransactionsTable;
9
use Application\Wallets\Exceptions\InsufficientFundsException;
10
use Application\Wallets\Exceptions\WalletsException;
11
use Bluz\Proxy\Db;
0 ignored issues
show
Bug introduced by
The type Bluz\Proxy\Db was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
13
/**
14
 * Class Table for `wallets`
15
 *
16
 * @package  Application\Wallets
17
 *
18
 * @method   static Row findRow($primaryKey)
19
 * @see      \Bluz\Db\Table::findRow()
20
 * @method   static Row findRowWhere($whereList)
21
 * @see      \Bluz\Db\Table::findRowWhere()
22
 *
23
 * @author   Anton Shevchuk
24
 * @created  2017-10-19 15:21:27
25
 */
26
class Service extends \Bluz\Db\Table
0 ignored issues
show
Bug introduced by
The type Bluz\Db\Table was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
27
{
28
    /**
29
     * Add Debit record
30
     *
31
     * @param int $userId
32
     * @param int $amount
33
     *
34
     * @return \Application\Transactions\Row|false
35
     */
36
    public static function addDebit(int $userId, int $amount)
37
    {
38
        return Db::transaction(static function () use ($userId, $amount) {
39
            $wallet = Table::getWallet($userId);
40
41
            $row = TransactionsTable::create(
42
                [
43
                    'userId' => $userId,
44
                    'amount' => $amount,
45
                    'type' => TransactionsTable::TYPE_DEBIT
46
                ]
47
            );
48
            $row->save();
49
50
            $wallet->amount += $amount;
51
            $wallet->save();
52
53
            return $row;
54
        });
55
    }
56
57
    /**
58
     * Add Credit record
59
     *
60
     * @param int $userId
61
     * @param int $amount
62
     *
63
     * @return \Application\Transactions\Row|false
64
     */
65
    public static function addCredit(int $userId, int $amount)
66
    {
67
        return Db::transaction(static function () use ($userId, $amount) {
68
69
            $wallet = Table::getWallet($userId);
70
            if ($wallet->amount - $wallet->blocked < $amount) {
71
                throw new InsufficientFundsException;
72
            }
73
74
            $row = TransactionsTable::create(
75
                [
76
                    'userId' => $userId,
77
                    'amount' => $amount,
78
                    'type' => TransactionsTable::TYPE_CREDIT
79
                ]
80
            );
81
            $row->save();
82
83
            $wallet->amount -= $amount;
84
            $wallet->save();
85
86
            return $row;
87
        });
88
    }
89
90
    /**
91
     * Block some money
92
     *
93
     * @param int $userId
94
     * @param int $amount
95
     *
96
     * @return \Application\Transactions\Row|false
97
     */
98
    public static function addBlock(int $userId, int $amount)
99
    {
100
        return Db::transaction(static function () use ($userId, $amount) {
101
            $wallet = Table::getWallet($userId);
102
            if ($wallet->amount - $wallet->blocked < $amount) {
103
                throw new InsufficientFundsException;
104
            }
105
106
            $row = TransactionsTable::create(
107
                [
108
                    'userId' => $userId,
109
                    'amount' => $amount,
110
                    'type' => TransactionsTable::TYPE_BLOCK
111
                ]
112
            );
113
            $row->save();
114
115
            $wallet->blocked += $amount;
116
            return $row;
117
        });
118
    }
119
120
    /**
121
     * Add Credit record
122
     *
123
     * @param int $userId
124
     * @param int $amount
125
     *
126
     * @return \Application\Transactions\Row|false
127
     */
128
    public static function removeBlock(int $userId, int $amount)
129
    {
130
        return Db::transaction(static function () use ($userId, $amount) {
131
            $wallet = Table::getWallet($userId);
132
133
            $row = TransactionsTable::create(
134
                [
135
                    'userId' => $userId,
136
                    'amount' => $amount,
137
                    'type' => TransactionsTable::TYPE_UNBLOCK
138
                ]
139
            );
140
            $row->save();
141
142
            if ($wallet->blocked < $amount) {
143
                $wallet->blocked = 0;
144
            } else {
145
                $wallet->blocked -= $amount;
146
            }
147
148
            return $wallet->save();
149
        });
150
    }
151
152
    /**
153
     * Send money from wallet to wallet
154
     *
155
     * @param int $fromUserId
156
     * @param int $toUserId
157
     * @param int $amount
158
     *
159
     * @return bool
160
     * @throws WalletsException
161
     */
162
    public static function send(int $fromUserId, int $toUserId, int $amount): bool
163
    {
164
        if ($amount < 0) {
165
            throw new WalletsException('Amount is lower than zero');
166
        }
167
168
        return Db::transaction(static function () use ($fromUserId, $toUserId, $amount) {
169
            $fromWallet = Table::getWallet($fromUserId);
170
171
            if ($fromWallet->amount - $fromWallet->blocked < $amount) {
172
                throw new InsufficientFundsException;
173
            }
174
175
            // Update wallets
176
            $fromWallet->amount -= $amount;
177
            $fromWallet->save();
178
179
            $toWallet = Table::getWallet($toUserId);
180
            $toWallet->amount += $amount;
181
            $toWallet->save();
182
183
            // Credit transaction
184
            $creditTransaction = TransactionsTable::create();
185
            $creditTransaction->userId = $fromUserId;
186
            $creditTransaction->chainUserId = $toUserId;
187
            $creditTransaction->amount = $amount;
188
            $creditTransaction->type = TransactionsTable::TYPE_CREDIT;
189
            $creditTransaction->save();
190
191
            // Debit transaction
192
            $debitTransaction = TransactionsTable::create();
193
            $debitTransaction->userId = $toUserId;
194
            $debitTransaction->chainUserId = $fromUserId;
195
            $debitTransaction->amount = $amount;
196
            $debitTransaction->type = TransactionsTable::TYPE_DEBIT;
197
            $debitTransaction->save();
198
199
            return true;
200
        });
201
    }
202
}
203