MongoDbDuplicateError::from()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * Created by gerk on 30.10.17 06:59
4
 */
5
6
namespace PeekAndPoke\Component\Slumber\Data\MongoDb\Error;
7
8
use MongoDB\Driver\Exception\WriteException;
9
use PeekAndPoke\Component\Slumber\Data\Error\DuplicateError;
10
11
/**
12
 *
13
 *
14
 * @author Karsten J. Gerber <[email protected]>
15
 */
16
class MongoDbDuplicateError extends DuplicateError
17
{
18 2
    public static function from(WriteException $exception)
19
    {
20 2
        preg_match('/^E11000 duplicate key error collection: (.*) index: (.*) dup key: (.*)$/', $exception->getMessage(), $matches);
21
22 2
        $table = $matches[1] ?? '';
23 2
        $index = $matches[2] ?? '';
24 2
        $data  = $matches[3] ?? '';
25
26 2
        return new static($exception->getMessage(), $table, $index, $data, $exception);
27
    }
28
}
29