Passed
Pull Request — develop (#3453)
by Evgeniy
19:01
created

ObjectType::getBindingType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Doctrine\DBAL\Types;
4
5
use Doctrine\DBAL\ParameterType;
6
use Doctrine\DBAL\Platforms\AbstractPlatform;
7
use function is_resource;
8
use function restore_error_handler;
9
use function serialize;
10
use function set_error_handler;
11
use function stream_get_contents;
12
use function unserialize;
13
14
/**
15
 * Type that maps a PHP object to a clob SQL type.
16
 */
17
class ObjectType extends Type
18
{
19
    /**
20
     * {@inheritdoc}
21
     */
22 607
    public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
23
    {
24 607
        return $platform->getBlobTypeDeclarationSQL($fieldDeclaration);
25
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30 81
    public function convertToDatabaseValue($value, AbstractPlatform $platform)
31
    {
32 81
        return serialize($value);
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 168
    public function convertToPHPValue($value, AbstractPlatform $platform)
39
    {
40 168
        if ($value === null) {
41 29
            return null;
42
        }
43
44 139
        $value = is_resource($value) ? stream_get_contents($value) : $value;
45
46
        set_error_handler(function (int $code, string $message) : void {
47 29
            throw ConversionException::conversionFailedUnserialization($this->getName(), $message);
48 139
        });
49
50
        try {
51 139
            return unserialize($value);
52
        } finally {
53 139
            restore_error_handler();
54
        }
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 28
    public function getBindingType() : int
61
    {
62 28
        return ParameterType::BINARY;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68 984
    public function getName()
69
    {
70 984
        return Type::OBJECT;
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76 17768
    public function requiresSQLCommentHint(AbstractPlatform $platform)
77
    {
78 17768
        return true;
79
    }
80
}
81