Completed
Push — 4.0 ( a84d33...0d7cd1 )
by Arjay
02:03
created

Comment::setComments()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
c 4
b 1
f 0
dl 0
loc 20
rs 8.8571
cc 5
eloc 9
nc 12
nop 1
1
<?php
2
3
namespace Yajra\Oci8\Schema;
4
5
use Illuminate\Database\Connection;
6
7
class Comment
8
{
9
    /**
10
     * @var \Illuminate\Database\Connection
11
     */
12
    protected $connection;
13
14
    /**
15
     * @param Connection $connection
16
     */
17
    public function __construct(Connection $connection)
18
    {
19
        $this->connection = $connection;
20
    }
21
22
    /**
23
     * Set table and column comments.
24
     *
25
     * @param  \Yajra\Oci8\Schema\OracleBlueprint $blueprint
26
     */
27
    public function setComments(OracleBlueprint $blueprint)
28
    {
29
        // Comment set by $table->comment = 'comment';
30
        if ($blueprint->comment != null) {
31
            $this->connection->statement(sprintf('comment on table %s is \'%s\'', $blueprint->getTable(),
32
                $blueprint->comment));
33
        }
34
35
        // Comments set by $table->string('column')->comment('comment');
36
        foreach ($blueprint->getColumns() as $column) {
37
            if (isset($column['comment'])) {
38
                $this->commentColumn($blueprint->getTable(), $column['name'], $column['comment']);
39
            }
40
        }
41
42
        // Comments set by $table->commentColumns = ['column' => 'comment'];
43
        foreach ($blueprint->commentColumns as $column => $comment) {
44
            $this->commentColumn($blueprint->getTable(), $column, $comment);
45
        }
46
    }
47
48
    /**
49
     * Run the comment on column statement
50
     *
51
     * @param  string $table
52
     * @param  string $column
53
     * @param  string $comment
54
     */
55
    private function commentColumn($table, $column, $comment)
56
    {
57
        $this->connection->statement(sprintf('comment on column %s.%s is \'%s\'', $table, $column, $comment));
58
    }
59
}
60