Completed
Push — master ( 1d87d0...2a331f )
by Al3x
04:12
created

InvoiceItemsStrategy   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 55
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A extract() 0 11 3
A hydrate() 0 12 3
1
<?php
2
declare(strict_types=1);
3
4
namespace InvoiceNinjaModule\Strategy;
5
6
use InvoiceNinjaModule\Model\Interfaces\ContactInterface;
7
use InvoiceNinjaModule\Model\Interfaces\InvoiceItemInterface;
8
use InvoiceNinjaModule\Model\InvoiceItem;
9
use Zend\Hydrator\Exception\BadMethodCallException;
10
use Zend\Hydrator\HydratorInterface;
11
use Zend\Hydrator\Strategy\StrategyInterface;
12
13
/**
14
 * Class InvoiceItemsStrategy
15
 */
16
class InvoiceItemsStrategy implements StrategyInterface
17
{
18
    /** @var HydratorInterface  */
19
    private $hydrator;
20
21
    /**
22
     * InvoiceItemsStrategy constructor.
23
     *
24
     * @param HydratorInterface $hydrator
25
     */
26 6
    public function __construct(HydratorInterface $hydrator)
27
    {
28 6
        $this->hydrator = $hydrator;
29 6
    }
30
31
    /**
32
     * Converts the given value so that it can be extracted by the hydrator.
33
     *
34
     * @param ContactInterface[]  $value  The original value.
35
     *
36
     * @return array Returns the value that should be extracted.
37
     * @throws BadMethodCallException for a non-object $contactObj
38
     */
39 2
    public function extract($value) :array
40
    {
41 2
        $result = [];
42
        /** @var InvoiceItemInterface $invoiceItem */
43 2
        foreach ($value as $invoiceItem) {
44 1
            if ($invoiceItem instanceof InvoiceItemInterface) {
45 1
                $result[] = $this->hydrator->extract($invoiceItem);
46
            }
47
        }
48 2
        return $result;
49
    }
50
51
    /**
52
     * Converts the given value so that it can be hydrated by the hydrator.
53
     * @param array $value
54
     *
55
     * @return ContactInterface[]
56
     * @throws BadMethodCallException for a non-object $contactObj
57
     */
58 2
    public function hydrate($value) :array
59
    {
60 2
        $result = [];
61 2
        if (is_array($value)) {
62 2
            foreach ($value as $invoiceItemArr) {
63 1
                $invoiceItem = new InvoiceItem();
64 1
                $this->hydrator->hydrate($invoiceItemArr, $invoiceItem);
65 1
                $result[] = $invoiceItem;
66
            }
67
        }
68 2
        return $result;
69
    }
70
}
71