Failed Conditions
Pull Request — develop (#3525)
by Jonathan
65:04
created

ValueNotConvertible::new()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 17
c 0
b 0
f 0
rs 9.8666
cc 4
nc 2
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\DBAL\Types\Exception;
6
7
use Doctrine\DBAL\Types\ConversionException;
8
use function is_string;
9
use function sprintf;
10
use function strlen;
11
use function substr;
12
13
/**
14
 * Thrown when a Database to Doctrine Type Conversion fails.
15
 */
16
final class ValueNotConvertible extends ConversionException implements TypesException
17
{
18
    public static function new($value, $toType, ?string $message = null) : self
19
    {
20
        if ($message !== null) {
21
            return new self(
22
                sprintf(
23
                    "Could not convert database value to '%s' as an error was triggered by the unserialization: '%s'",
24
                    $toType,
25
                    $message
26
                )
27
            );
28
        }
29
30
        return new self(
31
            sprintf(
32
                'Could not convert database value "%s" to Doctrine Type %s',
33
                is_string($value) && strlen($value) > 32 ? substr($value, 0, 20) . '...' : $value,
34
                $toType
35
            )
36
        );
37
    }
38
}
39