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

AbstractMoneyType::convertToPHPValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 9
ccs 4
cts 5
cp 0.8
rs 10
cc 2
nc 2
nop 2
crap 2.032
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