Passed
Push — master ( e32a81...12baf4 )
by Antonio Carlos
02:26
created

Database::getConnectionName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PragmaRX\Health\Checkers;
4
5
use Illuminate\Support\Facades\DB;
6
use PragmaRX\Health\Support\Timer;
7
use PragmaRX\Health\Support\Result;
8
9
class Database extends Base
10
{
11
    /**
12
     * @return Result
13
     */
14 1
    public function check()
15
    {
16
        try {
17 1
            switch ($this->target->type) {
0 ignored issues
show
Bug introduced by
The property type does not seem to exist in PragmaRX\Health\Support\Target.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
18
                case 'find_first_model':
19 1
                    return $this->findFirstModel();
20
                case 'raw_query':
21 1
                    return $this->rawQuery();
22
            }
23
24
            throw new \Exception(
25
                "Target type '{$this->target->type}' does not exists"
26
            );
27 1
        } catch (\Exception $exception) {
28 1
            report($exception);
29
30 1
            return $this->makeResultFromException($exception);
31
        }
32
    }
33
34 1
    protected function findFirstModel()
35
    {
36
        collect($this->target->models)->each(function ($model) {
0 ignored issues
show
Bug introduced by
The property models does not seem to exist in PragmaRX\Health\Support\Target.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
37 1
            instantiate($model)->first();
38 1
        });
39
40
        return $this->makeHealthyResult();
41
    }
42
43 1
    protected function getConnectionName()
44
    {
45 1
        return $this->target->connection == 'default'
0 ignored issues
show
Bug introduced by
The property connection does not seem to exist in PragmaRX\Health\Support\Target.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
46 1
            ? config('database.default')
47 1
            : $this->target->connection;
48
    }
49
50 1
    protected function rawQuery()
51
    {
52 1
        Timer::start();
53
54 1
        DB::connection($this->getConnectionName())->select(
55 1
            DB::raw($this->target->query)
0 ignored issues
show
Bug introduced by
The property query does not seem to exist in PragmaRX\Health\Support\Target.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
56
        );
57
58
        $took = round(Timer::stop(), 5);
59
        $tookHuman = "{$took}s";
60
61
        $this->target->setDisplay($this->target->name." ({$tookHuman})");
62
63
        $result =
64
            $took > $this->target->maximumTime
0 ignored issues
show
Bug introduced by
The property maximumTime does not seem to exist in PragmaRX\Health\Support\Target.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
65
                ? $this->makeResult(
66
                    false,
67
                    sprintf(
68
                        $this->target->errorMessage,
0 ignored issues
show
Bug introduced by
The property errorMessage does not seem to exist in PragmaRX\Health\Support\Target.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
69
                        $took,
70
                        $this->target->maximumTime
71
                    )
72
                )
73
                : $this->makeHealthyResult();
74
75
        $result->setValue($took)->setValueHuman($tookHuman);
76
77
        return $result;
78
    }
79
}
80