Passed
Push — 2.x ( dbf03f...44da33 )
by Terry
01:51
created

DriverTrait   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 12
c 2
b 0
f 0
dl 0
loc 71
rs 10
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A assertDriver() 0 5 2
A setDriver() 0 3 1
A createDatabase() 0 3 1
A setChannel() 0 6 2
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
11
declare(strict_types=1);
12
13
namespace Shieldon\Firewall\Kernel;
14
15
use Shieldon\Firewall\Driver\DriverProvider;
16
use LogicException;
17
use RuntimeException;
18
19
/*
20
 * Messenger Trait is loaded in Kernel instance only.
21
 */
22
trait DriverTrait
23
{
24
    /**
25
     * Driver for storing data.
26
     *
27
     * @var \Shieldon\Firewall\Driver\DriverProvider
28
     */
29
    public $driver;
30
31
    /**
32
     * This is for creating data tables automatically
33
     * Turn it off, if you don't want to check data tables every connection.
34
     *
35
     * @var bool
36
     */
37
    protected $autoCreateDatabase = true;
38
39
    /**
40
     * Set a data driver.
41
     *
42
     * @param DriverProvider $driver Query data from the driver you choose to use.
43
     *
44
     * @return void
45
     */
46
    public function setDriver(DriverProvider $driver): void
47
    {
48
        $this->driver = $driver;
49
    }
50
51
    /**
52
     * Set a data channel.
53
     *
54
     * This will create databases for the channel.
55
     *
56
     * @param string $channel Specify a channel.
57
     *
58
     * @return void
59
     */
60
    public function setChannel(string $channel): void
61
    {
62
        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...
63
            throw new LogicException('setChannel method requires setDriver set first.');
64
        } else {
65
            $this->driver->setChannel($channel);
66
        }
67
    }
68
69
    /**
70
     * For first time installation only. This is for creating data tables automatically.
71
     * Turning it on will check the data tables exist or not at every single pageview, 
72
     * it's not good for high traffic websites.
73
     *
74
     * @param bool $bool
75
     * 
76
     * @return void
77
     */
78
    public function createDatabase(bool $bool): void
79
    {
80
        $this->autoCreateDatabase = $bool;
81
    }
82
83
    /**
84
     * Check the data driver, throw an exception if not set.
85
     *
86
     * @return void
87
     */
88
    protected function assertDriver(): void
89
    {
90
        if (!isset($this->driver)) {
91
            throw new RuntimeException(
92
                'Data driver must be set.'
93
            );
94
        }
95
    }
96
}
97