forked from yii2tech/balance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathManagerMongoDb.php
140 lines (126 loc) · 3.72 KB
/
ManagerMongoDb.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
/**
* @link https://github.com/yii2tech
* @copyright Copyright (c) 2015 Yii2tech
* @license [New BSD License](http://www.opensource.org/licenses/bsd-license.php)
*/
namespace yii2tech\balance;
use yii\di\Instance;
use yii\mongodb\Connection;
use yii\mongodb\Query;
/**
* ManagerMongoDb is a balance manager, which uses MongoDB as data storage.
*
* This storage requires [yiisoft/yii2-mongodb](https://github.com/yiisoft/yii2-mongodb) extension installed.
*
* Configuration example:
*
* ```php
* return [
* 'components' => [
* 'balanceManager' => [
* 'class' => 'yii2tech\balance\ManagerMongoDb',
* 'accountCollection' => 'BalanceAccount',
* 'transactionCollection' => 'BalanceTransaction',
* 'accountBalanceAttribute' => 'balance',
* 'extraAccountLinkAttribute' => 'extraAccountId',
* ],
* ],
* ...
* ];
* ```
*
* Since MongoDB is schema-less this manager allows to save any transaction data as plain record attribute, which will
* be searchable. However MongoDB does not support transactions, thus usage of this manager may be risk prone.
*
* @see Manager
*
* @author Paul Klimov <[email protected]>
* @since 1.0
*/
class ManagerMongoDb extends Manager
{
/**
* @var Connection|array|string the MongoDB connection object or the application component ID of the MongoDB connection.
* After the ManagerMongoDb object is created, if you want to change this property, you should only assign it
* with a MongoDB connection object.
*/
public $db = 'mongodb';
/**
* @var string|array name of the MongoDB collection, which should store account records.
*/
public $accountCollection = 'BalanceAccount';
/**
* @var string|array name of the MongoDB collection, which should store transaction records.
*/
public $transactionCollection = 'BalanceTransaction';
/**
* @inheritdoc
*/
public function init()
{
parent::init();
$this->db = Instance::ensure($this->db, Connection::className());
}
/**
* @inheritdoc
*/
protected function findAccountId($attributes)
{
$row = (new Query())
->select(['_id'])
->from($this->accountCollection)
->andWhere($attributes)
->one($this->db);
if ($row === false) {
return null;
}
return $row['_id'];
}
/**
* @inheritdoc
*/
protected function findTransaction($id)
{
$row = (new Query())
->from($this->transactionCollection)
->andWhere(['_id' => $id])
->one($this->db);
if ($row === false) {
return null;
}
return $row;
}
/**
* @inheritdoc
*/
protected function createAccount($attributes)
{
return $this->db->getCollection($this->accountCollection)->insert($attributes);
}
/**
* @inheritdoc
*/
protected function createTransaction($attributes)
{
return $this->db->getCollection($this->transactionCollection)->insert($attributes);
}
/**
* @inheritdoc
*/
protected function incrementAccountBalance($accountId, $amount)
{
return $this->db->getCollection($this->accountCollection)->update(['_id' => $accountId], ['$inc' => [$this->accountBalanceAttribute => $amount]]);
}
/**
* @inheritdoc
*/
public function calculateBalance($account)
{
$accountId = $this->fetchAccountId($account);
return (new Query())
->from($this->transactionCollection)
->andWhere([$this->accountLinkAttribute => $accountId])
->sum($this->amountAttribute, $this->db);
}
}