Passed
Push — 2.x ( d6434d...dbf03f )
by Terry
01:59
created

DriverTrait   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 59
rs 10
wmc 4

3 Methods

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