Passed
Branch psalm-3 (d5d890)
by Wilmer
02:56
created

ConvertException   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 10
dl 0
loc 20
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A run() 0 11 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Exception;
6
7
use PDOException;
8
9
final class ConvertException
10
{
11
    private const MSG_INTEGRITY_EXCEPTION_1 = 'SQLSTATE[23';
12
    private const MGS_INTEGRITY_EXCEPTION_2 = 'ORA-00001: unique constraint';
13
14
    public function __construct(private \Exception $e, private string $rawSql)
15
    {
16
    }
17
18
    public function run(): Exception
19
    {
20
        $message = $this->e->getMessage() . PHP_EOL . 'The SQL being executed was: ' . $this->rawSql;
21
        $errorInfo = $this->e instanceof PDOException ? $this->e->errorInfo : null;
22
23
        return match (
24
            str_contains($message, self::MSG_INTEGRITY_EXCEPTION_1) ||
25
            str_contains($message, self::MGS_INTEGRITY_EXCEPTION_2)
26
        ) {
27
            true => new IntegrityException($message, $errorInfo, $this->e),
28
            default => new Exception($message, $errorInfo, $this->e),
29
        };
30
    }
31
}
32