DatabasesServiceProvider::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
namespace Germania\Databases;
3
4
use Pimple\Container;
5
use Pimple\ServiceProviderInterface;
6
7
class DatabasesServiceProvider implements ServiceProviderInterface
8
{
9
10
    /**
11
     * Values may be either:
12
     *
13
     *   - 2: PDO::ERRMODE_EXCEPTION
14
     *   - 1: PDO::ERRMODE_WARNING
15
     *   - 0: PDO::ERRMODE_SILENT
16
     *
17
     * @var int
18
     */
19
    public $pdo_error_mode = 2;
20
21
22
    /**
23
     * @param int $pdo_error_mode Default: PDO::ERRMODE_EXCEPTION
24
     */
25 85
    public function __construct( $pdo_error_mode = \PDO::ERRMODE_EXCEPTION )
26
    {
27 85
        if (is_null($pdo_error_mode)):
28 15
            $pdo_error_mode = \PDO::ERRMODE_EXCEPTION;
29 3
        endif;
30 85
        $this->pdo_error_mode = $pdo_error_mode;
31 85
    }
32
33
34
    /**
35
     * @param Container $dic Pimple Containter instance
36
     *
37
     * @implements ServiceProviderInterface
38
     */
39 17
    public function register(Container $dic)
40
    {
41
42
        /**
43
         * @return int
44
         */
45 44
        $dic['PDO.ErrorMode'] = function($dic) {
0 ignored issues
show
Unused Code introduced by
The parameter $dic is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
46 55
            return $this->pdo_error_mode;
47
        };
48
49
50
        /**
51
         * @return array
52
         */
53 28
        $dic['PDO.Options'] = function( $dic ) {
54
            return array(
55 35
                \PDO::ATTR_ERRMODE => $dic['PDO.ErrorMode']
56 7
            );
57
        };
58
59
60
        /**
61
         * @return Callable
62
         */
63 85
        $dic['PDO.Factory'] = $dic->protect(function( $db ) use ($dic) {
64
65
            // Parameter check
66 25
            if ($db instanceOf \StdClass):
67 5
                $db = (array) $db;
68 21
            elseif (!is_array($db) and !$db instanceOf \ArrayAccess):
69 10
                throw new \InvalidArgumentException("Array or StdClass or ArrayAccess expected");
70
            endif;
71
72
            // Setup
73 15
            $pdo_options = $dic['PDO.Options'];
74 15
            return new \PDO( $db['dsn'], $db['user'], $db['pass'], $pdo_options);
75 85
        });
76 85
    }
77
78
}
79
80
81