Orm   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Test Coverage

Coverage 7.69%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 26
c 3
b 0
f 0
dl 0
loc 96
ccs 2
cts 26
cp 0.0769
rs 10
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getEmitter() 0 7 2
A finder() 0 8 2
A getConnection() 0 7 2
A transaction() 0 17 3
A setConnection() 0 3 1
1
<?php
2
3
namespace Ronanchilvers\Orm;
4
5
use Closure;
6
use Evenement\EventEmitter;
7
use Exception;
8
use PDO;
9
use Ronanchilvers\Orm\Finder;
10
use RuntimeException;
11
12
/**
13
 * Facade class that manages the db connection
14
 *
15
 * @author Ronan Chilvers <[email protected]>
16
 */
17
class Orm
18
{
19
    /**
20
     * @var \PDO|null
21
     */
22
    static protected $connection = null;
23
24
    /**
25
     * An event emitter instance
26
     *
27
     * @var \Evenement\EventEmitter|null
28
     */
29
    static protected $emitter = null;
30
31
    /**
32
     * Set the PDO connection to use
33
     *
34
     * @param \PDO $connection
35
     * @author Ronan Chilvers <[email protected]>
36
     */
37 2
    static public function setConnection(PDO $connection)
38
    {
39 2
        static::$connection = $connection;
40
    }
41
42
    /**
43
     * Get the current connection
44
     *
45
     * @return \PDO
46
     * @author Ronan Chilvers <[email protected]>
47
     */
48
    static public function getConnection()
49
    {
50
        if (is_null(static::$connection)) {
51
            throw new RuntimeException('No database connection configured');
52
        }
53
54
        return static::$connection;
55
    }
56
57
    /**
58
     * Get the event emitter object
59
     *
60
     * @return \Evenement\EventEmitter
61
     * @author Ronan Chilvers <[email protected]>
62
     */
63
    static public function getEmitter()
64
    {
65
        if (is_null(static::$emitter)) {
66
            static::$emitter = new EventEmitter();
67
        }
68
69
        return static::$emitter;
70
    }
71
72
    /**
73
     * Automated transaction handling
74
     *
75
     * @param \Closure
76
     * @author Ronan Chilvers <[email protected]>
77
     */
78
    static public function transaction(Closure $closure)
79
    {
80
        $connection = static::getConnection();
81
        try {
82
            $connection->beginTransaction();
83
            $result = $closure();
84
85
            if (false === $result) {
86
                $connection->rollback();
87
            } else {
88
                $connection->commit();
89
            }
90
91
            return $result;
92
        } catch (Exception $ex) {
93
            $connection->rollback();
94
            throw $ex;
95
        }
96
    }
97
98
    /**
99
     * Get a finder for a given model class
100
     *
101
     * @param string $modelClass
102
     * @return \Ronanchilvers\Orm\Finder
103
     * @author Ronan Chilvers <[email protected]>
104
     */
105
    static public function finder($modelClass)
106
    {
107
        $finderClass = $modelClass::finder();
108
        if (empty($finderClass)) {
109
            $finderClass = Finder::class;
110
        }
111
112
        return new $finderClass($modelClass);
113
    }
114
}
115