Completed
Push — master ( 83cea8...e47c19 )
by Anton
17s queued 12s
created

TableProperty::initTable()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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