MenuapplicazioneTransformer   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 40%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
dl 0
loc 55
ccs 8
cts 20
cp 0.4
rs 10
c 1
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A reverseTransform() 0 7 2
A transform() 0 24 3
1
<?php
2
3
// src/Form/DataTransformer/IssueToNumberTransformer.php
4
namespace Cdf\BiCoreBundle\Form\Datatransformer;
5
6
use Doctrine\ORM\EntityManagerInterface;
7
use Symfony\Component\Form\DataTransformerInterface;
8
use Symfony\Component\Form\Exception\TransformationFailedException;
9
use Cdf\BiCoreBundle\Entity\Menuapplicazione;
10
11
class MenuapplicazioneTransformer implements DataTransformerInterface
12
{
13
    private $entityManager;
14
15 1
    public function __construct(EntityManagerInterface $entityManager)
16
    {
17 1
        $this->entityManager = $entityManager;
18
    }
19
20
    /**
21
     * Transforms an object (Menuapplicazione) to a string (number).
22
     *
23
     * @param  Menuapplicazione|null $menu
24
     * @return string
25
     */
26 1
    public function reverseTransform($menu)
27
    {
28 1
        if (null === $menu) {
29 1
            return null;
30
        }
31
32
        return $menu->getId();
33
    }
34
35
    /**
36
     * Transforms a string (number) to an object (Menuapplicazione).
37
     *
38
     * @param  string $menuId
39
     * @return Menuapplicazione|null
40
     * @throws TransformationFailedException if object (Menuapplicazione) is not found.
41
     */
42 1
    public function transform($menuId)
43
    {
44
        // no issue number? It's optional, so that's ok
45 1
        if (!$menuId) {
46 1
            return;
47
        }
48
49
        $menu = $this->entityManager
50
            ->getRepository(Menuapplicazione::class)
51
            // query for the issue with this id
52
            ->find($menuId)
53
        ;
54
55
        if (null === $menuId) {
0 ignored issues
show
introduced by
The condition null === $menuId is always false.
Loading history...
56
            // causes a validation error
57
            // this message is not shown to the user
58
            // see the invalid_message option
59
            throw new TransformationFailedException(sprintf(
60
                'Un menu con id "%s" non esiste!',
61
                $menu
62
            ));
63
        }
64
65
        return $menu;
66
    }
67
}
68