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

InvoiceItemsStrategy::extract()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 5
c 1
b 0
f 1
nc 3
nop 2
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 3
rs 10
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