Failed Conditions
Pull Request — master (#23)
by Adrien
03:02
created

AbstractMoneyType   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 70.59%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 13
c 1
b 0
f 0
dl 0
loc 44
rs 10
ccs 12
cts 17
cp 0.7059

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getBindingType() 0 3 1
A getName() 0 3 1
A convertToPHPValue() 0 9 2
A getSQLDeclaration() 0 3 1
A convertToDatabaseValue() 0 11 3
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 1
    public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
18
    {
19 1
        return $platform->getIntegerTypeDeclarationSQL($column);
20
    }
21
22
    public function getBindingType(): ParameterType
23
    {
24
        return ParameterType::INTEGER;
25
    }
26
27
    public function getName(): string
28
    {
29
        return 'Money';
30
    }
31
32
    /**
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 1
            return null;
39
        }
40
41 2
        $val = $this->createMoney((string) $value);
42
43 1
        return $val;
44
    }
45
46 2
    public function convertToDatabaseValue(mixed $value, AbstractPlatform $platform): ?string
47
    {
48 2
        if ($value instanceof Money) {
49
            return $value->getAmount();
50
        }
51
52 2
        if ($value === null) {
53 1
            return null;
54
        }
55
56 1
        throw new InvalidArgumentException('Cannot convert to database value: ' . var_export($value, true));
57
    }
58
}
59