Passed
Push — 2.x ( 34ebf1...cd0557 )
by Terry
02:02
created

DriverTrait::setDriver()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
/**
3
 * This file is part of the Shieldon package.
4
 *
5
 * (c) Terry L. <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 * 
10
 * php version 7.1.0
11
 * 
12
 * @category  Web-security
13
 * @package   Shieldon
14
 * @author    Terry Lin <[email protected]>
15
 * @copyright 2019 terrylinooo
16
 * @license   https://github.com/terrylinooo/shieldon/blob/2.x/LICENSE MIT
17
 * @link      https://github.com/terrylinooo/shieldon
18
 * @see       https://shieldon.io
19
 */
20
21
declare(strict_types=1);
22
23
namespace Shieldon\Firewall\Kernel;
24
25
use Shieldon\Firewall\Driver\DriverProvider;
26
use LogicException;
27
use RuntimeException;
28
29
/*
30
 * Messenger Trait is loaded in Kernel instance only.
31
 */
32
trait DriverTrait
33
{
34
    /**
35
     * Driver for storing data.
36
     *
37
     * @var \Shieldon\Firewall\Driver\DriverProvider
38
     */
39
    public $driver;
40
41
    /**
42
     * This is for creating data tables automatically
43
     * Turn it off, if you don't want to check data tables every connection.
44
     *
45
     * @var bool
46
     */
47
    protected $isCreateDatabase = true;
48
49
    /**
50
     * Set a data driver.
51
     *
52
     * @param DriverProvider $driver Query data from the driver you choose to use.
53
     *
54
     * @return void
55
     */
56
    public function setDriver(DriverProvider $driver): void
57
    {
58
        $this->driver = $driver;
59
    }
60
61
    /**
62
     * Set a data channel.
63
     *
64
     * This will create databases for the channel.
65
     *
66
     * @param string $channel Specify a channel.
67
     *
68
     * @return void
69
     */
70
    public function setChannel(string $channel): void
71
    {
72
        if (!$this->driver instanceof DriverProvider) {
0 ignored issues
show
introduced by
$this->driver is always a sub-type of Shieldon\Firewall\Driver\DriverProvider.
Loading history...
73
            throw new LogicException('setChannel method requires setDriver set first.');
74
        } else {
75
            $this->driver->setChannel($channel);
76
        }
77
    }
78
79
    /**
80
     * Shieldon creating data tables automatically.
81
     * Turning it off when the data tables exist overwise checling 
82
     * every pageview.
83
     * 
84
     * @return void
85
     */
86
    public function disableDbBuilder(): void
87
    {
88
        $this->isCreateDatabase = false;
89
    }
90
91
    /**
92
     * Check the data driver, throw an exception if not set.
93
     *
94
     * @return void
95
     */
96
    protected function assertDriver(): void
97
    {
98
        if (!isset($this->driver)) {
99
            throw new RuntimeException(
100
                'Data driver must be set.'
101
            );
102
        }
103
    }
104
}
105