Failed Conditions
Pull Request — 3.0.x (#3980)
by Guilherme
07:55
created

SchemaException::indexAlreadyExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Doctrine\DBAL\Schema;
4
5
use Doctrine\DBAL\DBALException;
6
use function implode;
7
use function sprintf;
8
9
/**
10
 * @psalm-immutable
11
 */
12
class SchemaException extends DBALException
13
{
14
    public const TABLE_DOESNT_EXIST       = 10;
15
    public const TABLE_ALREADY_EXISTS     = 20;
16
    public const COLUMN_DOESNT_EXIST      = 30;
17
    public const COLUMN_ALREADY_EXISTS    = 40;
18
    public const INDEX_DOESNT_EXIST       = 50;
19
    public const INDEX_ALREADY_EXISTS     = 60;
20
    public const SEQUENCE_DOENST_EXIST    = 70;
21
    public const SEQUENCE_ALREADY_EXISTS  = 80;
22
    public const INDEX_INVALID_NAME       = 90;
23
    public const FOREIGNKEY_DOESNT_EXIST  = 100;
24
    public const CONSTRAINT_DOESNT_EXIST  = 110;
25
    public const NAMESPACE_ALREADY_EXISTS = 120;
26
27
    /**
28
     * @param string $tableName
29
     *
30
     * @return SchemaException
31
     */
32 22
    public static function tableDoesNotExist($tableName)
33
    {
34 22
        return new self("There is no table with name '" . $tableName . "' in the schema.", self::TABLE_DOESNT_EXIST);
35
    }
36
37
    /**
38
     * @param string $indexName
39
     *
40
     * @return SchemaException
41
     */
42 22
    public static function indexNameInvalid($indexName)
43
    {
44 22
        return new self(
45 22
            sprintf('Invalid index-name %s given, has to be [a-zA-Z0-9_]', $indexName),
46 22
            self::INDEX_INVALID_NAME
47
        );
48
    }
49
50
    /**
51
     * @param string $indexName
52
     * @param string $table
53
     *
54
     * @return SchemaException
55
     */
56 44
    public static function indexDoesNotExist($indexName, $table)
57
    {
58 44
        return new self(
59 44
            sprintf("Index '%s' does not exist on table '%s'.", $indexName, $table),
60 44
            self::INDEX_DOESNT_EXIST
61
        );
62
    }
63
64
    /**
65
     * @param string $indexName
66
     * @param string $table
67
     *
68
     * @return SchemaException
69
     */
70 66
    public static function indexAlreadyExists($indexName, $table)
71
    {
72 66
        return new self(
73 66
            sprintf("An index with name '%s' was already defined on table '%s'.", $indexName, $table),
74 66
            self::INDEX_ALREADY_EXISTS
75
        );
76
    }
77
78
    /**
79
     * @param string $columnName
80
     * @param string $table
81
     *
82
     * @return SchemaException
83
     */
84 88
    public static function columnDoesNotExist($columnName, $table)
85
    {
86 88
        return new self(
87 88
            sprintf("There is no column with name '%s' on table '%s'.", $columnName, $table),
88 88
            self::COLUMN_DOESNT_EXIST
89
        );
90
    }
91
92
    /**
93
     * @param string $namespaceName
94
     *
95
     * @return SchemaException
96
     */
97 22
    public static function namespaceAlreadyExists($namespaceName)
98
    {
99 22
        return new self(
100 22
            sprintf("The namespace with name '%s' already exists.", $namespaceName),
101 22
            self::NAMESPACE_ALREADY_EXISTS
102
        );
103
    }
104
105
    /**
106
     * @param string $tableName
107
     *
108
     * @return SchemaException
109
     */
110 22
    public static function tableAlreadyExists($tableName)
111
    {
112 22
        return new self("The table with name '" . $tableName . "' already exists.", self::TABLE_ALREADY_EXISTS);
113
    }
114
115
    /**
116
     * @param string $tableName
117
     * @param string $columnName
118
     *
119
     * @return SchemaException
120
     */
121 22
    public static function columnAlreadyExists($tableName, $columnName)
122
    {
123 22
        return new self(
124 22
            "The column '" . $columnName . "' on table '" . $tableName . "' already exists.",
125 22
            self::COLUMN_ALREADY_EXISTS
126
        );
127
    }
128
129
    /**
130
     * @param string $sequenceName
131
     *
132
     * @return SchemaException
133
     */
134 22
    public static function sequenceAlreadyExists($sequenceName)
135
    {
136 22
        return new self("The sequence '" . $sequenceName . "' already exists.", self::SEQUENCE_ALREADY_EXISTS);
137
    }
138
139
    /**
140
     * @param string $sequenceName
141
     *
142
     * @return SchemaException
143
     */
144 22
    public static function sequenceDoesNotExist($sequenceName)
145
    {
146 22
        return new self("There exists no sequence with the name '" . $sequenceName . "'.", self::SEQUENCE_DOENST_EXIST);
147
    }
148
149
    /**
150
     * @param string $constraintName
151
     * @param string $table
152
     *
153
     * @return SchemaException
154
     */
155
    public static function uniqueConstraintDoesNotExist($constraintName, $table)
156
    {
157
        return new self(
158
            sprintf('There exists no unique constraint with the name "%s" on table "%s".', $constraintName, $table),
159
            self::CONSTRAINT_DOESNT_EXIST
160
        );
161
    }
162
163
    /**
164
     * @param string $fkName
165
     * @param string $table
166
     *
167
     * @return SchemaException
168
     */
169
    public static function foreignKeyDoesNotExist($fkName, $table)
170
    {
171
        return new self(
172
            sprintf("There exists no foreign key with the name '%s' on table '%s'.", $fkName, $table),
173
            self::FOREIGNKEY_DOESNT_EXIST
174
        );
175
    }
176
177
    /**
178
     * @return SchemaException
179
     */
180
    public static function namedForeignKeyRequired(Table $localTable, ForeignKeyConstraint $foreignKey)
181
    {
182
        return new self(
183
            'The performed schema operation on ' . $localTable->getName() . ' requires a named foreign key, ' .
184
            'but the given foreign key from (' . implode(', ', $foreignKey->getColumns()) . ') onto foreign table ' .
185
            "'" . $foreignKey->getForeignTableName() . "' (" . implode(', ', $foreignKey->getForeignColumns()) . ') is currently ' .
186
            'unnamed.'
187
        );
188
    }
189
190
    /**
191
     * @param string $changeName
192
     *
193
     * @return SchemaException
194
     */
195
    public static function alterTableChangeNotSupported($changeName)
196
    {
197
        return new self(
198
            sprintf("Alter table change not supported, given '%s'", $changeName)
199
        );
200
    }
201
}
202