Completed
Push — master ( 116dd8...98663d )
by Al3x
02:26
created

InvoiceItemsStrategy   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 53
c 0
b 0
f 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A extract() 0 9 2
A hydrate() 0 12 3
1
<?php
2
3
namespace InvoiceNinjaModule\Strategy;
4
5
use InvoiceNinjaModule\Model\Interfaces\ContactInterface;
6
use InvoiceNinjaModule\Model\Interfaces\InvoiceItemInterface;
7
use InvoiceNinjaModule\Model\InvoiceItem;
8
use Zend\Hydrator\ClassMethods;
9
use Zend\Hydrator\Exception\BadMethodCallException;
10
use Zend\Hydrator\HydratorInterface;
11
use Zend\Hydrator\Strategy\StrategyInterface;
12
13
class InvoiceItemsStrategy implements StrategyInterface
14
{
15
    /** @var ClassMethods  */
16
    private $hydrator;
17
18
    /**
19
     * InvoiceItemsStrategy constructor.
20
     *
21
     * @param HydratorInterface $hydrator
22
     */
23
    public function __construct(HydratorInterface $hydrator)
24
    {
25
        $this->hydrator = $hydrator;
0 ignored issues
show
Documentation Bug introduced by
$hydrator is of type object<Zend\Hydrator\HydratorInterface>, but the property $hydrator was declared to be of type object<Zend\Hydrator\ClassMethods>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
26
    }
27
28
    /**
29
     * Converts the given value so that it can be extracted by the hydrator.
30
     *
31
     * @param ContactInterface[]  $value  The original value.
32
     *
33
     * @return array Returns the value that should be extracted.
34
     * @throws BadMethodCallException for a non-object $contactObj
35
     */
36
    public function extract($value)
37
    {
38
        $result = [];
39
        /** @var InvoiceItemInterface $invoiceItem */
40
        foreach ($value as $invoiceItem) {
41
            $result[] = $this->hydrator->extract($invoiceItem);
42
        }
43
        return $result;
44
    }
45
46
    /**
47
     * Converts the given value so that it can be hydrated by the hydrator.
48
     * @param array $value
49
     *
50
     * @return ContactInterface[]
51
     * @throws BadMethodCallException for a non-object $contactObj
52
     */
53
    public function hydrate($value)
54
    {
55
        $result = [];
56
        if (is_array($value)) {
57
            foreach ($value as $invoiceItemArr) {
58
                $invoiceItem = new InvoiceItem();
59
                $this->hydrator->hydrate($invoiceItemArr, $invoiceItem);
60
                $result[] = $invoiceItem;
61
            }
62
        }
63
        return $result;
64
    }
65
}
66