Completed
Push — master ( b8f763...47a15d )
by Craig
14:22 queued 07:28
created

AbstractUserFieldTransformer   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A transform() 0 12 4
A reverseTransform() 0 8 2
1
<?php
2
/**
3
 * Routes.
4
 *
5
 * @copyright Zikula contributors (Zikula)
6
 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
7
 * @author Zikula contributors <[email protected]>.
8
 * @link http://www.zikula.org
9
 * @link http://zikula.org
10
 * @version Generated by ModuleStudio 0.7.2 (http://modulestudio.de).
11
 */
12
13
namespace Zikula\RoutesModule\Form\DataTransformer\Base;
14
15
use Symfony\Component\Form\DataTransformerInterface;
16
use Zikula\UsersModule\Entity\RepositoryInterface\UserRepositoryInterface;
17
use Zikula\UsersModule\Entity\UserEntity;
18
19
/**
20
 * User field transformer base class.
21
 *
22
 * This data transformer treats user fields.
23
 */
24
abstract class AbstractUserFieldTransformer implements DataTransformerInterface
25
{
26
    /**
27
     * @var UserRepositoryInterface
28
     */
29
    protected $userRepository;
30
31
    /**
32
     * UserFieldTransformer constructor.
33
     *
34
     * @param UserRepositoryInterface $userRepository UserRepository service instance
35
     */
36
    public function __construct(UserRepositoryInterface $userRepository)
37
    {
38
        $this->userRepository = $userRepository;
39
    }
40
41
    /**
42
     * Transforms the object values to the normalised value.
43
     *
44
     * @param UserEntity|null $value
45
     *
46
     * @return int|null
47
     */
48
    public function transform($value)
49
    {
50
        if (null === $value || !$value) {
51
            return null;
52
        }
53
54
        if ($value instanceof UserEntity) {
55
            return $value->getUid();
56
        }
57
58
        return intval($value);
59
    }
60
61
    /**
62
     * Transforms the form value back to the user entity.
63
     *
64
     * @param int $value
65
     *
66
     * @return UserEntity|null
67
     */
68
    public function reverseTransform($value)
69
    {
70
        if (!$value) {
71
            return null;
72
        }
73
74
        return $this->userRepository->find($value);
75
    }
76
}
77