Passed
Push — master ( b704df...28e450 )
by Al3x
10:52
created

InvitationsStrategy   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 12.5%

Importance

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