DriverAwareTrait   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
lcom 1
cbo 1
dl 0
loc 42
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setDriver() 0 5 1
A hasDriver() 0 4 1
A getDriver() 0 10 2
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Db
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Db\Traits;
16
17
use Phossa2\Db\Message\Message;
18
use Phossa2\Db\Exception\LogicException;
19
use Phossa2\Db\Interfaces\DriverInterface;
20
use Phossa2\Db\Interfaces\DriverAwareInterface;
21
22
/**
23
 * DriverAwareTrait
24
 *
25
 * @package Phossa2\Db
26
 * @author  Hong Zhang <[email protected]>
27
 * @see     DriverAwareInterface
28
 * @version 2.0.1
29
 * @since   2.0.0 added
30
 * @since   2.0.1 changed setDriver() signature
31
 */
32
trait DriverAwareTrait
33
{
34
    /**
35
     * the driver
36
     *
37
     * @var    DriverInterface
38
     * @access protected
39
     */
40
    protected $driver;
41
42
    /**
43
     * {@inheritDoc}
44
     * @since  2.0.1 able to set NULL
45
     */
46
    public function setDriver(DriverInterface $driver = null)
47
    {
48
        $this->driver = $driver;
49
        return $this;
50
    }
51
52
    /**
53
     * {@inheritDoc}
54
     */
55
    public function hasDriver()/*# : bool */
56
    {
57
        return null !== $this->driver;
58
    }
59
60
    /**
61
     * {@inheritDoc}
62
     */
63
    public function getDriver()/*# : DriverInterface */
64
    {
65
        if (null === $this->driver) {
66
            throw new LogicException(
67
                Message::get(Message::DB_DRIVER_NOTSET),
68
                Message::DB_DRIVER_NOTSET
69
            );
70
        }
71
        return $this->driver;
72
    }
73
}
74