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

TwitterType   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 92.31%

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 7
c 4
b 0
f 1
lcom 1
cbo 2
dl 0
loc 74
ccs 12
cts 13
cp 0.9231
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setSerializer() 0 4 1
A getSQLDeclaration() 0 4 1
A getName() 0 4 1
A convertToPHPValue() 0 4 1
A convertToDatabaseValue() 0 8 2
A getSerializer() 0 4 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