Completed
Pull Request — master (#32)
by Sascha-Oliver
19:51
created

MoneyHydrator   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 90.91%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 4
c 3
b 0
f 0
lcom 0
cbo 2
dl 0
loc 30
ccs 10
cts 11
cp 0.9091
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A extract() 0 7 1
A hydrate() 0 11 3
1
<?php
2
3
namespace ZFBrasil\DoctrineMoneyModule\Hydrator;
4
5
use Zend\Hydrator\HydratorInterface;
6
use Money\Money;
7
use Money\Currency;
8
9
/**
10
 * Hydrator for Money object
11
 *
12
 * @author Fábio Carneiro <[email protected]>
13
 * @license MIT
14
 */
15
class MoneyHydrator implements HydratorInterface
16
{
17
    /**
18
     * {@inheritDoc}
19
     */
20 5
    public function extract($object)
21
    {
22
        return [
23 5
            'amount' => $object->getAmount(),
24 5
            'currency' => $object->getCurrency()->getName()
25 5
        ];
26
    }
27
28
    /**
29
     * {@inheritDoc}
30
     *
31
     * @return Money
32
     */
33 3
    public function hydrate(array $data, $object)
34
    {
35 3
        if (empty($data['amount']) || empty($data['currency'])) {
36
            return null;
37
        }
38
39 3
        return new Money(
40 3
            $data['amount'],
41 3
            new Currency($data['currency'])
42 3
        );
43
    }
44
}
45