Completed
Push — master ( 9b777b...ee89da )
by Arjay
14:21 queued 10s
created

Sequence   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 150
Duplicated Lines 22 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 23.33%

Importance

Changes 0
Metric Value
dl 33
loc 150
ccs 7
cts 30
cp 0.2333
rs 10
c 0
b 0
f 0
wmc 18
lcom 1
cbo 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A create() 0 16 4
A wrap() 0 8 2
A drop() 0 20 3
A exists() 12 12 2
A nextValue() 10 10 2
A currentValue() 0 4 1
A lastInsertId() 11 11 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Yajra\Oci8\Schema;
4
5
use Illuminate\Database\Connection;
6
7
class Sequence
8
{
9
    /**
10
     * @var \Illuminate\Database\Connection|\Yajra\Oci8\Oci8Connection
11
     */
12
    protected $connection;
13
14
    /**
15
     * @param Connection $connection
16
     */
17 3
    public function __construct(Connection $connection)
18
    {
19 3
        $this->connection = $connection;
20 3
    }
21
22
    /**
23
     * function to create oracle sequence.
24
     *
25
     * @param  string $name
26
     * @param  int $start
27
     * @param  bool $nocache
28
     * @param  int $min
29
     * @param  bool $max
30 3
     * @param  int $increment
31
     * @return bool
32 3
     */
33
    public function create($name, $start = 1, $nocache = false, $min = 1, $max = false, $increment = 1)
34
    {
35
        if (! $name) {
36 3
            return false;
37
        }
38 3
39
        $name = $this->wrap($name);
40
41
        $nocache = $nocache ? 'nocache' : '';
42
43
        $max = $max ? " maxvalue {$max}" : '';
44
45
        $sequence_stmt = "create sequence {$name} minvalue {$min} {$max} start with {$start} increment by {$increment} {$nocache}";
46
47
        return $this->connection->statement($sequence_stmt);
48
    }
49
50
    /**
51
     * Wrap sequence name with schema prefix.
52
     *
53
     * @param string $name
54
     * @return string
55
     */
56
    public function wrap($name)
57
    {
58
        if ($this->connection->getConfig('prefix_schema')) {
59
            return $this->connection->getConfig('prefix_schema') . '.' . $name;
60
        }
61
62
        return $name;
63
    }
64
65
    /**
66
     * function to safely drop sequence db object.
67
     *
68
     * @param  string $name
69
     * @return bool
70
     */
71
    public function drop($name)
72
    {
73
        // check if a valid name and sequence exists
74
        if (! $name || ! $this->exists($name)) {
75
            return false;
76
        }
77
78
        $name = $this->wrap($name);
79
80
        return $this->connection->statement("
81
            declare
82
                e exception;
83
                pragma exception_init(e,-02289);
84
            begin
85
                execute immediate 'drop sequence {$name}';
86
            exception
87
            when e then
88
                null;
89
            end;");
90
    }
91
92
    /**
93
     * function to check if sequence exists.
94
     *
95
     * @param  string $name
96
     * @return bool
97
     */
98 View Code Duplication
    public function exists($name)
99
    {
100
        if (! $name) {
101
            return false;
102
        }
103
104
        $name = $this->wrap($name);
105
106
        return $this->connection->selectOne(
107
            "select * from all_sequences where sequence_name=upper('{$name}') and sequence_owner=upper(user)"
108
        );
109
    }
110
111
    /**
112
     * get sequence next value.
113
     *
114
     * @param  string $name
115
     * @return int
116
     */
117 View Code Duplication
    public function nextValue($name)
118
    {
119
        if (! $name) {
120
            return 0;
121
        }
122
123
        $name = $this->wrap($name);
124
125
        return $this->connection->selectOne("SELECT $name.NEXTVAL as id FROM DUAL")->id;
126
    }
127
128
    /**
129
     * same function as lastInsertId. added for clarity with oracle sql statement.
130
     *
131
     * @param  string $name
132
     * @return int
133
     */
134
    public function currentValue($name)
135
    {
136
        return $this->lastInsertId($name);
137
    }
138
139
    /**
140
     * function to get oracle sequence last inserted id.
141
     *
142
     * @param  string $name
143
     * @return int
144
     */
145 View Code Duplication
    public function lastInsertId($name)
146
    {
147
        // check if a valid name and sequence exists
148
        if (! $name || ! $this->exists($name)) {
149
            return 0;
150
        }
151
152
        $name = $this->wrap($name);
153
154
        return $this->connection->selectOne("select {$name}.currval as id from dual")->id;
155
    }
156
}
157