Completed
Push — master ( 5a7237...3d9ddd )
by Grégoire
12s queued 10s
created

ProductIdType::convertToPHPValue()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 4
nc 3
nop 2
1
<?php
2
declare(strict_types=1);
3
4
/*
5
 * This file is part of the Sonata Project package.
6
 *
7
 * (c) Thomas Rabaix <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Sonata\DoctrineORMAdminBundle\Tests\Fixtures\DoctrineType;
14
15
use Doctrine\DBAL\Platforms\AbstractPlatform;
16
use Doctrine\DBAL\Types\ConversionException;
17
use Doctrine\DBAL\Types\Type;
18
use Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Entity\ProductId;
19
20
final class ProductIdType extends Type
21
{
22
    const NAME = 'ProductId';
23
24
    public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform): string
25
    {
26
        return $platform->getIntegerTypeDeclarationSQL($fieldDeclaration);
27
    }
28
29
    public function requiresSQLCommentHint(AbstractPlatform $platform): bool
30
    {
31
        return true;
32
    }
33
34
    /**
35
     * @param mixed $value
36
     */
37
    public function convertToPHPValue($value, AbstractPlatform $platform): ?ProductId
38
    {
39
        if ($value === null || $value instanceof ProductId) {
40
            return $value;
41
        }
42
43
        try {
44
            return new ProductId((int) $value);
45
        } catch (\Throwable $e) {
46
            throw ConversionException::conversionFailed($value, $this->getName());
47
        }
48
    }
49
50
    /**
51
     * @param mixed $value
52
     */
53
    public function convertToDatabaseValue($value, AbstractPlatform $platform): ?int
54
    {
55
        $value = $this->convertToPHPValue($value, $platform);
56
57
        return $value !== null ? $value->getId() : null;
58
    }
59
60
    public function getName(): string
61
    {
62
        return self::NAME;
63
    }
64
}
65