Passed
Push — master ( c2faf8...afa411 )
by Al3x
11:51
created

InvoiceItemsStrategy   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 14
c 1
b 0
f 1
dl 0
loc 51
ccs 17
cts 17
cp 1
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A extract() 0 9 3
A hydrate() 0 11 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 Laminas\Hydrator\Exception\BadMethodCallException;
10
use Laminas\Hydrator\HydratorInterface;
11
use Laminas\Hydrator\Strategy\StrategyInterface;
12
use function is_array;
13
14
/**
15
 * Class InvoiceItemsStrategy
16
 */
17
final class InvoiceItemsStrategy implements StrategyInterface
18
{
19
    private HydratorInterface $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 InvoiceItemInterface[]  $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, ?object $object = null): array
40
    {
41 2
        $result = [];
42 2
        foreach ($value as $invoiceItem) {
43 1
            if ($invoiceItem instanceof InvoiceItemInterface) {
44 1
                $result[] = $this->hydrator->extract($invoiceItem);
45
            }
46
        }
47 2
        return $result;
48
    }
49
50
    /**
51
     * Converts the given value so that it can be hydrated by the hydrator.
52
     * @param array $value
53
     *
54
     * @return ContactInterface[]
55
     * @throws BadMethodCallException for a non-object $contactObj
56
     */
57 2
    public function hydrate($value, ?array $data) :array
58
    {
59 2
        $result = [];
60 2
        if (is_array($value)) {
0 ignored issues
show
introduced by
The condition is_array($value) is always true.
Loading history...
61 2
            foreach ($value as $invoiceItemArr) {
62 1
                $invoiceItem = new InvoiceItem();
63 1
                $this->hydrator->hydrate($invoiceItemArr, $invoiceItem);
64 1
                $result[] = $invoiceItem;
65
            }
66
        }
67 2
        return $result;
68
    }
69
}
70