StoragePdo   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 84.62%

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 3
dl 0
loc 52
ccs 11
cts 13
cp 0.8462
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B createInstance() 0 12 5
A testExists() 0 18 3
1
<?php
2
3
namespace Simplario\Checker\Checker;
4
5
use Simplario\Checker\ResultException\FailException;
6
use Simplario\Checker\ResultException\SuccessException;
7
8
/**
9
 * Class StoragePdo
10
 *
11
 * @package Simplario\Checker\Checker
12
 */
13 1
class StoragePdo extends AbstractChecker
14
{
15
    /**
16 1
     * @var string
17
     */
18
    protected $target = 'connect';
19
20
    /**
21 1
     * @param array $connect
22
     *
23 1
     * @return \PDO
24 1
     */
25
    protected function createInstance(array $connect)
26
    {
27 1
        // Exception on fail connection
28 1
        $instance = new \PDO(
29 1
            isset($connect['dsn']) ? $connect['dsn'] : null,
30 1
            isset($connect['user']) ? $connect['user'] : null,
31
            isset($connect['password']) ? $connect['password'] : null,
32
            isset($connect['options']) ? $connect['options'] : null
33 1
        );
34
35
        return $instance;
36
    }
37 1
38
    /**
39
     * @param array   $connect
40
     * @param boolean $expectExists
41
     * @param array   $task
42
     *
43
     * @throws FailException
44
     * @throws SuccessException
45
     */
46
    protected function testExists(array $connect, $expectExists, array $task)
47
    {
48
        $exists = true;
49
        $msg = 'Connection exists';
50
51
        try {
52
            $pdo = $this->createInstance($connect);
0 ignored issues
show
Unused Code introduced by
$pdo is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
53
        } catch (\Exception $ex) {
54
            $exists = false;
55
            $msg = "Unable to connect to database. " . $ex->getMessage();
56
        }
57
58
        if ($exists === $expectExists) {
59
            throw new SuccessException('Ok', $task);
60
        }
61
62
        throw new FailException($msg, $task);
63
    }
64
}
65