Completed
Push — 4.0 ( 0d7cd1...0c0da2 )
by Arjay
01:59
created

Comment   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 5
Bugs 1 Features 0
Metric Value
wmc 10
c 5
b 1
f 0
lcom 1
cbo 2
dl 0
loc 85
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setComments() 0 11 1
A commentTable() 0 9 2
A fluentComments() 0 8 3
A commentColumn() 0 4 1
A commentColumns() 0 6 2
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
        $this->commentTable($blueprint);
31
32
        // Comments set by $table->string('column')->comment('comment');
33
        $this->fluentComments($blueprint);
34
35
        // Comments set by $table->commentColumns = ['column' => 'comment'];
36
        $this->commentColumns($blueprint);
37
    }
38
39
    /**
40
     * Run the comment on table statement.
41
     *
42
     * @param \Yajra\Oci8\Schema\OracleBlueprint $blueprint
43
     */
44
    private function commentTable(OracleBlueprint $blueprint)
45
    {
46
        if ($blueprint->comment != null) {
47
            $this->connection->statement(sprintf(
48
                'comment on table %s is \'%s\'', $blueprint->getTable(),
49
                $blueprint->comment
50
            ));
51
        }
52
    }
53
54
    /**
55
     * Add comments set via fluent setter.
56
     *
57
     * @param \Yajra\Oci8\Schema\OracleBlueprint $blueprint
58
     */
59
    private function fluentComments(OracleBlueprint $blueprint)
60
    {
61
        foreach ($blueprint->getColumns() as $column) {
62
            if (isset($column['comment'])) {
63
                $this->commentColumn($blueprint->getTable(), $column['name'], $column['comment']);
64
            }
65
        }
66
    }
67
68
    /**
69
     * Run the comment on column statement
70
     *
71
     * @param  string $table
72
     * @param  string $column
73
     * @param  string $comment
74
     */
75
    private function commentColumn($table, $column, $comment)
76
    {
77
        $this->connection->statement(sprintf('comment on column %s.%s is \'%s\'', $table, $column, $comment));
78
    }
79
80
    /**
81
     * Add comments on columns.
82
     *
83
     * @param \Yajra\Oci8\Schema\OracleBlueprint $blueprint
84
     */
85
    private function commentColumns(OracleBlueprint $blueprint)
86
    {
87
        foreach ($blueprint->commentColumns as $column => $comment) {
88
            $this->commentColumn($blueprint->getTable(), $column, $comment);
89
        }
90
    }
91
}
92