SqlSession   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 0
dl 0
loc 38
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A executeAtomically() 0 15 3
1
<?php
2
3
/*
4
 * This file is part of the BenGorUser package.
5
 *
6
 * (c) Beñat Espiña <[email protected]>
7
 * (c) Gorka Laucirica <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace BenGorUser\CarlosBuenosvinosDddBridge\Infrastructure\Application\Service;
14
15
use Ddd\Application\Service\TransactionalSession;
16
17
/**
18
 * Class SqlSession.
19
 *
20
 * @author Beñat Espiña <[email protected]>
21
 */
22
class SqlSession implements TransactionalSession
23
{
24
    /**
25
     * The database abstraction layer.
26
     *
27
     * @var \PDO
28
     */
29
    private $pdo;
30
31
    /**
32
     * Constructor.
33
     *
34
     * @param \PDO $pdo The database abstraction layer
35
     */
36
    public function __construct(\PDO $pdo)
37
    {
38
        $this->pdo = $pdo;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function executeAtomically(callable $operation)
45
    {
46
        $this->pdo->beginTransaction();
47
        $this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
48
49
        try {
50
            $return = call_user_func($operation, $this);
51
            $this->pdo->commit();
52
53
            return $return ?: true;
54
        } catch (\Exception $exception) {
55
            $this->pdo->rollback();
56
            throw $exception;
57
        }
58
    }
59
}
60