Passed
Push — master ( d38468...eb5cf9 )
by Al3x
01:39
created

InvitationsStrategy   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 14
c 1
b 0
f 1
dl 0
loc 51
ccs 0
cts 17
cp 0
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\Interfaces\InvitationInterface;
7
use InvoiceNinjaModule\Model\Invitation;
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 InvitationsStrategy
15
 */
16
final class InvitationsStrategy implements StrategyInterface
17
{
18
    private HydratorInterface $hydrator;
19
20
    /**
21
     * InvitationsStrategy constructor.
22
     *
23
     * @param HydratorInterface $hydrator
24
     */
25
    public function __construct(HydratorInterface $hydrator)
26
    {
27
        $this->hydrator = $hydrator;
28
    }
29
30
    /**
31
     * Converts the given value so that it can be extracted by the hydrator.
32
     *
33
     * @param InvitationInterface[]  $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
    public function extract($value, ?object $object = null): array
39
    {
40
        $result = [];
41
        foreach ($value as $invitation) {
42
            if ($invitation instanceof InvitationInterface) {
43
                $result[] = $this->hydrator->extract($invitation);
44
            }
45
        }
46
        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 InvitationInterface[]
54
     * @throws BadMethodCallException for a non-object $contactObj
55
     */
56
    public function hydrate($value, ?array $data) :array
57
    {
58
        $result = [];
59
        if (is_array($value)) {
0 ignored issues
show
introduced by
The condition is_array($value) is always true.
Loading history...
60
            foreach ($value as $invitationArr) {
61
                $invitation = new Invitation();
62
                $this->hydrator->hydrate($invitationArr, $invitation);
63
                $result[] = $invitation;
64
            }
65
        }
66
        return $result;
67
    }
68
}
69