Passed
Branch master (10fdc5)
by Beniamin
02:43
created

TableFactory::createNewTable()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 15
cts 15
cp 1
rs 8.6737
c 0
b 0
f 0
cc 5
eloc 16
nc 5
nop 2
crap 5
1
<?php
2
3
/**
4
 * This file is part of Phuria SQL Builder package.
5
 *
6
 * Copyright (c) 2016 Beniamin Jonatan Šimko
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Phuria\UnderQuery\TableFactory;
13
14
use Phuria\UnderQuery\QueryBuilder\BuilderInterface;
15
use Phuria\UnderQuery\Table\SubQueryTable;
16
use Phuria\UnderQuery\Table\UnknownTable;
17
use Phuria\UnderQuery\TableRecognizer;
18
use Phuria\UnderQuery\TableRegistry;
19
20
/**
21
 * @author Beniamin Jonatan Šimko <[email protected]>
22
 */
23
class TableFactory implements TableFactoryInterface
24
{
25
    /**
26
     * @var TableRegistry $registry
27
     */
28
    private $registry;
29
30
    /**
31
     * @var TableRecognizer $tableRecognizer
32
     */
33
    private $tableRecognizer;
34
35
    /**
36
     * @param TableRegistry $registry
37
     */
38 3
    public function __construct(TableRegistry $registry)
39
    {
40 3
        $this->registry = $registry;
41 3
        $this->tableRecognizer = new TableRecognizer();
42 3
    }
43
44
    /**
45
     * @inheritdoc
46
     */
47 3
    public function createNewTable($table, BuilderInterface $qb)
48
    {
49 3
        $tableType = $this->tableRecognizer->recognizeType($table);
50
51 3
        $tableClass = null;
52
53
        switch ($tableType) {
54 3
            case TableRecognizer::TYPE_CLOSURE:
55 1
                $tableClass = $this->tableRecognizer->extractClassName($table);
56 1
                break;
57 3
            case TableRecognizer::TYPE_CLASS_NAME:
58 1
                $tableClass = $table;
59 1
                break;
60 3
            case TableRecognizer::TYPE_TABLE_NAME:
61 2
                $tableClass = $this->registry->getTableClass($table);
62 2
                break;
63 1
            case TableRecognizer::TYPE_SUB_QUERY:
64 1
                return $this->createSubQueryTable($table, $qb);
65
        }
66
67 2
        return $this->doCreate($table, $tableClass, $qb);
68
    }
69
70
    /**
71
     * @param string           $requestedTable
72
     * @param string           $tableClass
73
     * @param BuilderInterface $qb
74
     *
75
     * @return mixed
76
     */
77 2
    private function doCreate($requestedTable, $tableClass, BuilderInterface $qb)
78
    {
79 2
        $tableObject = new $tableClass($qb);
80
81 2
        if ($tableObject instanceof UnknownTable) {
82 1
            $tableObject->setTableName($requestedTable);
83 1
        }
84
85 2
        return $tableObject;
86
    }
87
88
    /**
89
     * @param BuilderInterface $subQb
90
     * @param BuilderInterface $qb
91
     *
92
     * @return SubQueryTable
93
     */
94 1
    private function createSubQueryTable(BuilderInterface $subQb, BuilderInterface $qb)
95
    {
96 1
        return new SubQueryTable($subQb, $qb);
97
    }
98
}