Passed
Pull Request — master (#23)
by Adrien
17:46 queued 04:32
created

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