Completed
Push — master ( 19862b...baa3ae )
by Konstantin
02:12
created

DatabaseConnection   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 93.33%

Importance

Changes 0
Metric Value
wmc 6
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 51
ccs 14
cts 15
cp 0.9333
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A makeConnection() 0 17 3
A connection() 0 4 2
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 14 and the first side effect is on line 6.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
namespace GeoFixer\models;
4
5
use Cake\Database\Connection;
6
use Cake\Database\Driver\Mysql;;
7
use Exception;
8
9
/**
10
 * Class DatabaseConnection
11
 *
12
 * @package GeoFixer\models
13
 */
14
class DatabaseConnection
15
{
16
    /**
17
     * @var
18
     */
19
    protected static $connection;
20
21
    /**
22
     * DatabaseConnection constructor.
23
     */
24 1
    public function __construct()
25
    {
26 1
       self::makeConnection();
27 1
    }
28
29
    /**
30
     * Создаем подключение к БД
31
     *
32
     * @param null $config
33
     * @return Connection
34
     * @throws Exception
35
     */
36 3
    public static function makeConnection($config = null)
37
    {
38 3
        if (!$config) {
39 1
            $config = include dirname(dirname(__FILE__)) . '/config/database.php';
40 1
        }
41
42 3
        $driver = new Mysql($config);
43 3
        self::$connection = new Connection([
44
            'driver' => $driver
45 3
        ]);
46
47 3
        if (self::$connection === false) {
48
            throw new Exception('Database connection error');
49
        }
50
51 3
        return self::$connection;
52
    }
53
54
    /**
55
     * Возвращаем соединение, если существует, иначе создаем и возвращаем
56
     *
57
     * @return Connection
58
     */
59 32
    public static function connection()
60
    {
61 32
        return self::$connection ? self::$connection : self::makeConnection();
62
    }
63
64
}
65