Skip to content

Commit 96aa4ec

Browse files
committed
cleaning
1 parent 782a9d5 commit 96aa4ec

File tree

1 file changed

+39
-18
lines changed

1 file changed

+39
-18
lines changed

src/Transaction.php

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,36 @@ class Transaction extends \yii\base\BaseObject
3434
*/
3535
public $clientSession;
3636

37+
/**
38+
* Set debug message if `enableLogging` property is enable in yii\mongodb\Connection
39+
* @var string $message please see $this->yiiDebug()
40+
* @var string $category please see $this->yiiDebug()
41+
*/
42+
protected function yiiDebug($message, $category = 'mongodb'){
43+
if($this->clientSession->db->enableLogging)
44+
$this->yiiDebug($message,$category);
45+
}
46+
47+
/**
48+
* Begin profile if `enableProfiling` property is enable in yii\mongodb\Connection
49+
* @var string $token please see $this->yiiBeginProfile()
50+
* @var string $category please see $this->yiiBeginProfile()
51+
*/
52+
protected function yiiBeginProfile($token, $category = 'mongodb'){
53+
if($this->clientSession->db->enableProfiling)
54+
$this->yiiBeginProfile($token,$category);
55+
}
56+
57+
/**
58+
* End profile if `enableProfiling` property is enable in yii\mongodb\Connection
59+
* @var string $token please see $this->yiiEndProfile()
60+
* @var string $category please see $this->yiiEndProfile()
61+
*/
62+
protected function yiiEndProfile($token, $category = 'mongodb'){
63+
if($this->clientSession->db->enableProfiling)
64+
$this->yiiEndProfile($token,$category);
65+
}
66+
3767
/**
3868
* Returns a value indicating whether this transaction is active.
3969
* @return bool whether this transaction is active. Only an active transaction
@@ -53,30 +83,24 @@ public function getIsActive(){
5383
*/
5484
public function start($transactionOptions = []){
5585
Command::prepareCPOptions($transactionOptions);
56-
if($this->clientSession->db->enableLogging)
57-
Yii::debug('Starting mongodb transaction ...', __METHOD__);
86+
$this->yiiDebug('Starting mongodb transaction ...', __METHOD__);
5887
if($this->clientSession->getInTransaction())
5988
throw new Exception('Nested transaction not supported');
6089
$this->clientSession->db->trigger(Connection::EVENT_START_TRANSACTION);
61-
if($this->clientSession->db->enableProfiling)
62-
Yii::beginProfile('mongodb > start transaction(session id => '.$this->clientSession->getId().')');
90+
$this->yiiBeginProfile('mongodb > start transaction(session id => '.$this->clientSession->getId().')');
6391
$this->clientSession->mongoSession->startTransaction($transactionOptions);
64-
if($this->clientSession->db->enableLogging)
65-
Yii::debug('MongoDB transaction started.', __METHOD__);
92+
$this->yiiDebug('MongoDB transaction started.', __METHOD__);
6693
}
6794

6895
/**
6996
* Commit a transaction.
7097
* @see https://www.php.net/manual/en/mongodb-driver-session.committransaction.php
7198
*/
7299
public function commit(){
73-
if($this->clientSession->db->enableLogging)
74-
Yii::debug('Committing mongodb transaction ...', __METHOD__);
100+
$this->yiiDebug('Committing mongodb transaction ...', __METHOD__);
75101
$this->clientSession->mongoSession->commitTransaction();
76-
if($this->clientSession->db->enableProfiling)
77-
Yii::endProfile('mongodb > start transaction(session id => '.$this->clientSession->getId().')');
78-
if($this->clientSession->db->enableLogging)
79-
Yii::debug('Commit mongodb transaction.', __METHOD__);
102+
$this->yiiEndProfile('mongodb > start transaction(session id => '.$this->clientSession->getId().')');
103+
$this->yiiDebug('Commit mongodb transaction.', __METHOD__);
80104
$this->clientSession->db->trigger(Connection::EVENT_COMMIT_TRANSACTION);
81105
}
82106

@@ -85,13 +109,10 @@ public function commit(){
85109
* @see https://www.php.net/manual/en/mongodb-driver-session.aborttransaction.php
86110
*/
87111
public function rollBack(){
88-
if($this->clientSession->db->enableLogging)
89-
Yii::debug('Rolling back mongodb transaction ...', __METHOD__);
112+
$this->yiiDebug('Rolling back mongodb transaction ...', __METHOD__);
90113
$this->clientSession->mongoSession->abortTransaction();
91-
if($this->clientSession->db->enableProfiling)
92-
Yii::endProfile('mongodb > start transaction(session id => '.$this->clientSession->getId().')');
93-
if($this->clientSession->db->enableLogging)
94-
Yii::debug('Roll back mongodb transaction.', __METHOD__);
114+
$this->yiiEndProfile('mongodb > start transaction(session id => '.$this->clientSession->getId().')');
115+
$this->yiiDebug('Roll back mongodb transaction.', __METHOD__);
95116
$this->clientSession->db->trigger(Connection::EVENT_ROLLBACK_TRANSACTION);
96117
}
97118
}

0 commit comments

Comments
 (0)