UserStrategy::hydrate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace JhFlexiTime\Stdlib\Hydrator\Strategy;
4
5
use JhUser\Repository\UserRepositoryInterface;
6
use Zend\Stdlib\Hydrator\Strategy\StrategyInterface;
7
use ZfcUser\Entity\UserInterface;
8
9
/**
10
 * Class UserStrategy
11
 * @package JhFlexiTime\Stdlib\Hydrator\Strategy
12
 * @author Aydin Hassan <[email protected]>
13
 */
14
class UserStrategy implements StrategyInterface
15
{
16
    /**
17
     * @var UserRepositoryInterface
18
     */
19
    protected $userRepository;
20
21
    /**
22
     * @param UserRepositoryInterface $userRepository
23
     */
24
    public function __construct(UserRepositoryInterface $userRepository)
25
    {
26
        $this->userRepository = $userRepository;
27
    }
28
29
    /**
30
     * Convert the User object to it's ID
31
     *
32
     * @param UserInterface $value
33
     * @return int User ID
34
     */
35
    public function extract($value)
36
    {
37
        if ($value instanceof UserInterface) {
38
            return $value->getId();
39
        } else {
40
            return $value;
41
        }
42
    }
43
44
    /**
45
     * Convert the User Id to a User object
46
     *
47
     * @param mixed $value The original value.
48
     * @return UserInterface
49
     */
50
    public function hydrate($value)
51
    {
52
        return $this->userRepository->find($value);
53
    }
54
}
55