Passed
Push — master ( 2d5d0a...7d78c5 )
by Adrien
02:23
created

AbstractMoneyType::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ecodev\Felix\DBAL\Types;
6
7
use Doctrine\DBAL\Platforms\AbstractPlatform;
8
use Doctrine\DBAL\Types\IntegerType;
9
use Money\Money;
10
11
abstract class AbstractMoneyType extends IntegerType
12
{
13
    /**
14
     * @param string $value
15
     *
16
     * @return Money
17
     */
18
    abstract protected function createMoney(string $value): Money;
19
20
    public function getName()
21
    {
22
        return 'Money';
23
    }
24
25 2
    public function convertToPHPValue($value, AbstractPlatform $platform)
26
    {
27 2
        if ($value === null) {
28 1
            return $value;
29
        }
30
31 2
        $val = $this->createMoney((string) $value);
32
33 1
        return $val;
34
    }
35
36 2
    public function convertToDatabaseValue($value, AbstractPlatform $platform)
37
    {
38 2
        if ($value instanceof Money) {
39
            return $value->getAmount();
40
        }
41
42 2
        if ($value === null) {
43 1
            return $value;
44
        }
45
46 1
        throw new \InvalidArgumentException('Cannot convert to dababase value: ' . var_export($value, true));
47
    }
48
49 1
    public function requiresSQLCommentHint(AbstractPlatform $platform)
50
    {
51 1
        return true;
52
    }
53
}
54