Failed Conditions
Pull Request — develop (#3525)
by Jonathan
09:58
created

ValueNotConvertible::new()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 17
c 0
b 0
f 0
ccs 12
cts 12
cp 1
rs 9.8666
cc 4
nc 2
nop 3
crap 4
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 328
    public static function new($value, $toType, ?string $message = null) : self
19
    {
20 328
        if ($message !== null) {
21 304
            return new self(
22 304
                sprintf(
23 4
                    "Could not convert database value to '%s' as an error was triggered by the unserialization: '%s'",
24 304
                    $toType,
25 304
                    $message
26
                )
27
            );
28
        }
29
30 299
        return new self(
31 299
            sprintf(
32 24
                'Could not convert database value "%s" to Doctrine Type %s',
33 299
                is_string($value) && strlen($value) > 32 ? substr($value, 0, 20) . '...' : $value,
34 299
                $toType
35
            )
36
        );
37
    }
38
}
39