Skip to content

Commit 71ec1ee

Browse files
committed
Added new traits.
Signed-off-by: Joshua Parker <joshua@joshuaparker.dev>
1 parent d82ef1c commit 71ec1ee

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

Support/Traits/ContainerAware.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Codefy\Framework\Support\Traits;
6+
7+
use Codefy\Framework\Codefy;
8+
9+
trait ContainerAware
10+
{
11+
public static function make(): static
12+
{
13+
return Codefy::$PHP->make(name: static::class);
14+
}
15+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Codefy\Framework\Support\Traits;
6+
7+
use Codefy\Framework\Codefy;
8+
use Qubus\Exception\Exception;
9+
10+
trait DbTransactionsAware
11+
{
12+
/**
13+
* Determines whether class uses transaction.
14+
*/
15+
protected bool $useTransaction = false;
16+
17+
/**
18+
* Enable transaction in pipeline.
19+
*/
20+
public function withTransaction(): static
21+
{
22+
$this->useTransaction = true;
23+
24+
return $this;
25+
}
26+
27+
/**
28+
* Begin the transaction if enabled.
29+
*
30+
* @throws Exception
31+
*/
32+
protected function beginTransaction(): void
33+
{
34+
if (!$this->useTransaction) {
35+
return;
36+
}
37+
38+
Codefy::$PHP->getDB()->beginTransaction();
39+
}
40+
41+
/**
42+
* Commit the transaction if enabled.
43+
*
44+
* @throws Exception
45+
*/
46+
protected function commitTransaction(): void
47+
{
48+
if (!$this->useTransaction) {
49+
return;
50+
}
51+
52+
Codefy::$PHP->getDB()->commit();
53+
}
54+
55+
/**
56+
* Rollback the transaction if enabled.
57+
*
58+
* @throws Exception
59+
*/
60+
protected function rollbackTransaction(): void
61+
{
62+
if (!$this->useTransaction) {
63+
return;
64+
}
65+
66+
Codefy::$PHP->getDB()->rollback();
67+
}
68+
}

0 commit comments

Comments
 (0)