UserStrategy   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A extract() 0 8 2
A hydrate() 0 4 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