Passed
Pull Request — master (#65)
by Arman
06:12 queued 03:24
created

TableFactory::createInstance()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 2.7.0
13
 */
14
15
namespace Quantum\Factory;
16
17
use Quantum\Libraries\Database\Schema\Table;
18
use Quantum\Exceptions\MigrationException;
19
use Quantum\Libraries\Database\Database;
20
21
/**
22
 * Class TableFactory
23
 * @package Quantum\Factory
24
 */
25
class TableFactory
26
{
27
28
    /**
29
     * Creates new table
30
     * @param string $name
31
     * @return Table
32
     * @throws Quantum\Exceptions\MigrationException
33
     */
34
    public function create(string $name): Table
35
    {
36
        if ($this->checkTableExists($name)) {
37
            throw MigrationException::tableAlreadyExists($name);
38
        }
39
40
        return $this->createInstance($name)->setAction(Table::CREATE);
41
    }
42
43
    /**
44
     * Get the table
45
     * @param string $name
46
     * @return Table
47
     * @throws Quantum\Exceptions\MigrationException
48
     */
49
    public function get(string $name): Table
50
    {
51
        if (!$this->checkTableExists($name)) {
52
            throw MigrationException::tableDoesnotExists($name);
53
        }
54
55
        return $this->createInstance($name)->setAction(Table::ALTER);
56
    }
57
58
    /**
59
     * Renames the table
60
     * @param string $oldName
61
     * @param string $newName
62
     * @return bool
63
     */
64
    public function rename(string $oldName, string $newName): bool
65
    {
66
        if (!$this->checkTableExists($oldName)) {
67
            throw MigrationException::tableDoesnotExists($oldName);
68
        }
69
70
        $this->createInstance($oldName)->setAction(Table::RENAME, ['newName' => $newName]);
71
        return true;
72
    }
73
74
    /**
75
     * Drops the table
76
     * @param string $name
77
     * @return bool
78
     */
79
    public function drop(string $name): bool
80
    {
81
        if (!$this->checkTableExists($name)) {
82
            throw MigrationException::tableDoesnotExists($name);
83
        }
84
85
        $this->createInstance($name)->setAction(Table::DROP);
86
        return true;
87
    }
88
89
    /**
90
     * Checks if the DB table exists
91
     * @param string $name
92
     * @return bool
93
     * @throws \Quantum\Exceptions\DatabaseException
94
     */
95
    public function checkTableExists(string $name): bool
96
    {
97
        try {
98
            Database::query('SELECT 1 FROM ' . $name);
99
        } catch (\PDOException $e) {
100
            return false;
101
        }
102
103
        return true;
104
    }
105
106
    /**
107
     * Creates new Table instance
108
     * @param string $name
109
     * @return Table
110
     */
111
    private function createInstance(string $name)
112
    {
113
        return new Table($name);
114
    }
115
116
}
117