UserHydrator   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 9
c 0
b 0
f 0
lcom 0
cbo 2
dl 0
loc 90
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A extract() 0 9 2
A hydrate() 0 5 1
A getCryptoService() 0 4 1
A mapField() 0 8 2
A guardUserObject() 0 8 2
1
<?php
2
3
namespace ZfcUser\Mapper;
4
5
use Zend\Crypt\Password\PasswordInterface as ZendCryptPassword;
6
use Zend\Stdlib\Hydrator\ClassMethods;
7
use ZfcUser\Entity\UserInterface as UserEntity;
8
9
class UserHydrator extends ClassMethods implements HydratorInterface
0 ignored issues
show
Deprecated Code introduced by
The class Zend\Stdlib\Hydrator\ClassMethods has been deprecated with message: Use Zend\Hydrator\ClassMethods from zendframework/zend-hydrator instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
10
{
11
    /**
12
     * @var ZendCryptPassword
13
     */
14
    private $cryptoService;
15
16
    /**
17
     * @param ZendCryptPassword $cryptoService
18
     * @param bool|array        $underscoreSeparatedKeys
19
     */
20
    public function __construct(
21
        ZendCryptPassword $cryptoService,
22
        $underscoreSeparatedKeys = true
23
    ) {
24
        parent::__construct($underscoreSeparatedKeys);
25
        $this->cryptoService = $cryptoService;
26
    }
27
28
    /**
29
     * Extract values from an object
30
     *
31
     * @param  UserEntity $object
32
     * @return array
33
     * @throws Exception\InvalidArgumentException
34
     */
35
    public function extract($object)
36
    {
37
        $this->guardUserObject($object);
38
        $data = parent::extract($object);
39
        if ($data['id'] === null) {
40
            unset($data['id']);
41
        }
42
        return $data;
43
    }
44
45
    /**
46
     * Hydrate $object with the provided $data.
47
     *
48
     * @param  array               $data
49
     * @param  UserEntity $object
50
     * @return UserEntity
51
     * @throws Exception\InvalidArgumentException
52
     */
53
    public function hydrate(array $data, $object)
54
    {
55
        $this->guardUserObject($object);
56
        return parent::hydrate($data, $object);
57
    }
58
59
    /**
60
     * @return ZendCryptPassword
61
     */
62
    public function getCryptoService()
63
    {
64
        return $this->cryptoService;
65
    }
66
67
    /**
68
     * Remap an array key
69
     *
70
     * @param  string $keyFrom
71
     * @param  string $keyTo
72
     * @param  array  $array
73
     * @return array
74
     */
75
    protected function mapField($keyFrom, $keyTo, array $array)
76
    {
77
        if (isset($array[$keyFrom])) {
78
            $array[$keyTo] = $array[$keyFrom];
79
        }
80
        unset($array[$keyFrom]);
81
        return $array;
82
    }
83
84
    /**
85
     * Ensure $object is an UserEntity
86
     *
87
     * @param  mixed $object
88
     * @throws Exception\InvalidArgumentException
89
     */
90
    protected function guardUserObject($object)
91
    {
92
        if (!$object instanceof UserEntity) {
93
            throw new Exception\InvalidArgumentException(
94
                '$object must be an instance of ZfcUser\Entity\UserInterface'
95
            );
96
        }
97
    }
98
}
99