GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Db   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 6
dl 0
loc 108
c 0
b 0
f 0
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A makeDB() 0 18 3
A setupDB() 0 14 2
A addDB() 0 13 2
A configureDB() 0 14 2
A entity() 0 4 1
A cfgToDSN() 0 8 2
A makeDSN() 0 4 1
1
<?php
2
3
namespace Saltwater\RedBean\Provider;
4
5
use Saltwater\Server as S;
6
use Saltwater\Salt\Provider;
7
8
class Db extends Provider
9
{
10
    /**
11
     * @var \RedBean_Instance
12
     */
13
    private static $r;
14
15
    /**
16
     * @return \RedBean_Instance
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
17
     */
18
    public function __construct($config)
19
    {
20
        if (empty(self::$r)) {
21
            self::makeDB($config);
22
        }
23
24
        return self::$r;
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
25
    }
26
27
    protected static function makeDB($config)
28
    {
29
        $cfg = $config->database;
30
31
        if (empty(self::$r)) {
32
            self::$r = new \RedBean_Instance();
33
        }
34
35
        if (!isset($cfg->type)) {
36
            $cfg->type = 'mysql';
37
        }
38
39
        self::setupDB($cfg);
40
41
        self::addDB($cfg);
42
43
        self::configureDB($cfg);
44
    }
45
46
    private static function setupDB($cfg)
47
    {
48
        if (!empty(self::$r->toolboxes)) {
49
            return;
50
        }
51
52
        self::$r->setup(
53
            self::cfgToDSN($cfg),
54
            $cfg->user,
55
            $cfg->password
56
        );
57
58
        self::$r->setupPipeline();
59
    }
60
61
    private static function addDB($cfg)
62
    {
63
        if (isset(self::$r->toolboxes[$cfg->name])) {
64
            return;
65
        }
66
67
        self::$r->addDatabase(
68
            $cfg->name,
69
            self::cfgToDSN($cfg),
70
            $cfg->user,
71
            $cfg->password
72
        );
73
    }
74
75
    private static function configureDB($cfg)
76
    {
77
        self::$r->selectDatabase($cfg->name);
78
79
        if (!empty($cfg->prefix)) {
80
            self::$r->prefix($cfg->prefix);
81
        }
82
83
        self::$r->redbean->beanhelper->setModelFormatter(
84
            'Saltwater\RedBean\Provider\Db::entity'
85
        );
86
87
        self::$r->useWriterCache(true);
88
    }
89
90
    /**
91
     * Return an Entity class name from the EntityProvider
92
     *
93
     * @param string $name
94
     *
95
     * @return string
96
     */
97
    public static function entity($name)
98
    {
99
        return S::$n->entity->get($name);
100
    }
101
102
    private static function cfgToDSN($cfg)
103
    {
104
        if (isset($cfg->dsn)) {
105
            return $cfg->dsn;
106
        }
107
108
        return self::makeDSN($cfg->type, $cfg->host, $cfg->name);
109
    }
110
111
    private static function makeDSN($type, $host, $name)
112
    {
113
        return $type . ':host=' . $host . ';' . 'dbname=' . $name;
114
    }
115
}
116