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

AbstractMoneyType   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 80%

Importance

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

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
    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