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.
Completed
Push — develop ( fdbbb1...7b4ca9 )
by Christian
02:13
created

DatabaseFactory::getConnection()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 29
Code Lines 13

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 29
rs 8.8571
cc 3
eloc 13
nc 3
nop 0
1
<?php
2
3
/**
4
 * Class DatabaseFactory
5
 *
6
 * Use it like this:
7
 * $database = DatabaseFactory::getFactory()->getConnection();
8
 *
9
 * That's my personal favourite when creating a database connection.
10
 * It's a slightly modified version of Jon Raphaelson's excellent answer on StackOverflow:
11
 * http://stackoverflow.com/questions/130878/global-or-singleton-for-database-connection
12
 *
13
 * Full quote from the answer:
14
 *
15
 * "Then, in 6 months when your app is super famous and getting dugg and slashdotted and you decide you need more than
16
 * a single connection, all you have to do is implement some pooling in the getConnection() method. Or if you decide
17
 * that you want a wrapper that implements SQL logging, you can pass a PDO subclass. Or if you decide you want a new
18
 * connection on every invocation, you can do do that. It's flexible, instead of rigid."
19
 *
20
 * Thanks! Big up, mate!
21
 */
22
class DatabaseFactory
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
23
{
24
    private static $factory;
25
    private $database;
26
27
    public static function getFactory()
28
    {
29
        if (!self::$factory) {
30
            self::$factory = new DatabaseFactory();
31
        }
32
        return self::$factory;
33
    }
34
35
    public function getConnection() {
36
        if (!$this->database) {
37
38
            /**
39
             * Check DB connection in try/catch block. Also when PDO is not constructed properly,
40
             * prevent to exposing database host, username and password in plain text as:
41
             * PDO->__construct('mysql:host=127....', 'root', '12345678', Array)
42
             * by throwing custom error message
43
             */
44
            try {
45
                $options = array(PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ, PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING);
46
                $this->database = new PDO(
47
                   Config::get('DB_TYPE') . ':host=' . Config::get('DB_HOST') . ';dbname=' .
48
                   Config::get('DB_NAME') . ';port=' . Config::get('DB_PORT') . ';charset=' . Config::get('DB_CHARSET'),
49
                   Config::get('DB_USER'), Config::get('DB_PASS'), $options
50
                   );
51
            } catch (PDOException $e) {
52
53
                // Echo custom message. Echo error code gives you some info.
54
                echo 'Database connection can not be estabilished. Please try again later.' . '<br>';
55
                echo 'Error code: ' . $e->getCode();
56
57
                // Stop application :(
58
                // No connection, reached limit connections etc. so no point to keep it running
59
                exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method getConnection() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
60
            }
61
        }
62
        return $this->database;
63
    }
64
}
65