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

TableProperty   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 58.33%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 52
ccs 7
cts 12
cp 0.5833
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A initTable() 0 13 3
A setTable() 0 3 1
A getTable() 0 6 2
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