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

ContactsStrategy   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

3 Methods

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