Completed
Push — master ( 693283...8cba26 )
by Craig
06:26
created

UserFieldTransformer   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 53
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 4 4 1
A transform() 12 12 4
A reverseTransform() 8 8 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/*
4
 * This file is part of the Zikula package.
5
 *
6
 * Copyright Zikula Foundation - http://zikula.org/
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zikula\UsersModule\Form\DataTransformer;
13
14
use Symfony\Component\Form\DataTransformerInterface;
15
use Zikula\UsersModule\Entity\RepositoryInterface\UserRepositoryInterface;
16
use Zikula\UsersModule\Entity\UserEntity;
17
18
/**
19
 * User field transformer class.
20
 *
21
 * This data transformer treats user fields.
22
 */
23 View Code Duplication
class UserFieldTransformer implements DataTransformerInterface
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
24
{
25
    /**
26
     * @var UserRepositoryInterface
27
     */
28
    protected $userRepository;
29
30
    /**
31
     * UserFieldTransformer constructor.
32
     *
33
     * @param UserRepositoryInterface $userRepository UserRepository service instance
34
     */
35
    public function __construct(UserRepositoryInterface $userRepository)
36
    {
37
        $this->userRepository = $userRepository;
38
    }
39
40
    /**
41
     * Transforms the object values to the normalised value.
42
     *
43
     * @param UserEntity|null $value
44
     *
45
     * @return int|null
46
     */
47
    public function transform($value)
48
    {
49
        if (null === $value || !$value) {
50
            return null;
51
        }
52
53
        if ($value instanceof UserEntity) {
54
            return $value->getUid();
55
        }
56
57
        return intval($value);
58
    }
59
60
    /**
61
     * Transforms the form value back to the user entity.
62
     *
63
     * @param int $value
64
     *
65
     * @return UserEntity|null
66
     */
67
    public function reverseTransform($value)
68
    {
69
        if (!$value) {
70
            return null;
71
        }
72
73
        return $this->userRepository->find($value);
74
    }
75
}
76