Orm::setConnection()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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