Completed
Push — master ( a22322...8bcfe7 )
by Rémi
04:01
created

TwitterType::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Twitter\Doctrine;
4
5
use Doctrine\DBAL\Platforms\AbstractPlatform;
6
use Doctrine\DBAL\Types\Type;
7
use Twitter\Serializer\TwitterJsonSerializer;
8
use Twitter\TwitterSerializable;
9
10
class TwitterType extends Type
11
{
12
    const TWITTER = 'twitter';
13
14
    /**
15
     * @var TwitterJsonSerializer
16
     */
17
    private $twitterSerializer;
18
19
    /**
20
     * Sets the serializer
21
     *
22
     * @param TwitterJsonSerializer $twitterSerializer
23
     */
24
    public function setSerializer(TwitterJsonSerializer $twitterSerializer)
25 3
    {
26
        $this->twitterSerializer = $twitterSerializer;
27 3
    }
28 3
29
    /**
30
     * Gets the SQL declaration snippet for a field of this type.
31
     *
32
     * @param array $fieldDeclaration The field declaration.
33
     * @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform The currently used database platform.
34
     *
35
     * @return string
36
     */
37
    public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
38 3
    {
39
        return 'TEXT';
40 3
    }
41
42
    /**
43
     * Gets the name of this type.
44
     *
45
     * @return string
46
     */
47
    public function getName()
48 3
    {
49
        return self::TWITTER;
50 3
    }
51
52
    /**
53
     * @param  string           $value
54
     * @param  AbstractPlatform $platform
55
     * @return TwitterSerializable
56
     */
57
    public function convertToPHPValue($value, AbstractPlatform $platform)
58 3
    {
59
        return $this->getSerializer()->unserialize($value);
60 3
    }
61
62
    /**
63
     * @param  TwitterSerializable $value
64
     * @param  AbstractPlatform    $platform
65
     * @return string
66
     */
67
    public function convertToDatabaseValue($value, AbstractPlatform $platform)
68 3
    {
69
        if (!$value instanceof TwitterSerializable) {
70 3
            throw new \InvalidArgumentException('Value is not serializable!');
71
        }
72
73
        return $this->getSerializer()->serialize($value);
74 3
    }
75
76
    /**
77
     * @return TwitterJsonSerializer
78
     */
79
    private function getSerializer()
80
    {
81
        return $this->twitterSerializer;
82
    }
83
}
84