Completed
Push — master ( 467d47...6f2bb4 )
by Anton
14s
created

TableProperty::initTable()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
nc 2
dl 0
loc 14
ccs 0
cts 6
cp 0
c 0
b 0
f 0
cc 3
eloc 5
nop 0
crap 12
rs 9.4285
1
<?php
2
/**
3
 * Bluz Framework Component
4
 *
5
 * @copyright Bluz PHP Team
6
 * @link      https://github.com/bluzphp/framework
7
 */
8
9
declare(strict_types=1);
10
11
namespace Bluz\Db\Traits;
12
13
use Bluz\Db\Exception\TableNotFoundException;
14
use Bluz\Db\TableInterface;
15
16
/**
17
 * TableProperty
18
 *
19
 * @package  Bluz\Db\Traits
20
 * @author   Anton Shevchuk
21
 */
22
trait TableProperty
23
{
24
    /**
25
     * @var TableInterface instance
26
     */
27
    protected $table;
28
29
    /**
30
     * Setup Table instance
31
     *
32
     * @param  TableInterface $table
33
     *
34
     * @return void
35
     */
36 42
    public function setTable(TableInterface $table) : void
37
    {
38 42
        $this->table = $table;
39 42
    }
40
41
    /**
42
     * Return table instance for manipulation
43
     *
44
     * @return TableInterface
45
     * @throws TableNotFoundException
46
     */
47 40
    public function getTable() : TableInterface
48
    {
49 40
        if (!$this->table) {
50 1
            $this->initTable();
51
        }
52 39
        return $this->table;
53
    }
54
55
    /**
56
     * Init table instance for manipulation
57
     *
58
     * @return void
59
     * @throws TableNotFoundException
60
     */
61
    protected function initTable() : void
62
    {
63
        $tableClass = class_namespace(static::class) . '\\Table';
64
65
        // check class initialization
66
        if (!class_exists($tableClass) || !is_subclass_of($tableClass, TableInterface::class)) {
67
            throw new TableNotFoundException('`Table` class is not exists or not initialized');
68
        }
69
70
        /**
71
         * @var TableInterface $tableClass
72
         */
73
        $this->setTable($tableClass::getInstance());
74
    }
75
}
76