Passed
Push — master ( 699016...741d83 )
by Michał
03:32
created

Log::classExists()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
ccs 7
cts 7
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace BlueRegister;
4
5
use \SimpleLog\LogInterface;
6
7
class Log
8
{
9
    /**
10
     * @var null|LogInterface
11
     */
12
    protected $log;
13
14
    /**
15
     * @param mixed $logObject
16
     * @throws \LogicException
17
     * @throws \InvalidArgumentException
18
     */
19 7
    public function __construct($logObject)
20
    {
21 7
        switch (true) {
22 7
            case $logObject instanceof LogInterface:
23 3
                $this->log = $logObject;
24 3
                break;
25
26 4
            case is_string($logObject) && $this->classExists($logObject):
27 2
                $this->log = new $logObject();
28
29 2
                if (!$this->log instanceof LogInterface) {
30 1
                    $message = 'Log should be instance of SimpleLog\LogInterface: ' . get_class($this->log);
31 1
                    throw new \LogicException($message);
32
                }
33
34 1
                break;
35
36 1
            default:
37 1
                throw new \LogicException('Cannot create Log instance: ' . get_class($logObject));
0 ignored issues
show
Bug introduced by
It seems like $logObject can also be of type string; however, parameter $object of get_class() does only seem to accept object, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

37
                throw new \LogicException('Cannot create Log instance: ' . get_class(/** @scrutinizer ignore-type */ $logObject));
Loading history...
38
                break;
39 1
        }
40 4
    }
41
42
    /**
43
     * check that class exists and throw exception if not
44
     *
45 6
     * @param string $namespace
46
     * @return $this
47 6
     * @throws \InvalidArgumentException
48
     */
49 9
    protected function classExists($namespace)
50 6
    {
51 3
        if (!class_exists($namespace)) {
52 7
            throw new \InvalidArgumentException('Class don\'t exists: ' . $namespace);
53 6
        }
54 6
55 2
        return $this;
56
    }
57
58
    /**
59
     * @param string|array $message
60
     * @return $this
61
     */
62 2
    public function makeLog($message)
63 2
    {
64 2
        if (!is_null($this->log)) {
65 4
            $this->log->makeLog($message);
66 2
        }
67 2
68 2
        return $this;
69
    }
70
}
71