Completed
Push — master ( a682ba...4851b3 )
by Paweł
40:50
created

MenuItemToIdTransformer   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 94.12%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 0
loc 61
ccs 16
cts 17
cp 0.9412
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A transform() 0 12 3
A reverseTransform() 0 17 3
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Superdesk Web Publisher Menu Bundle.
7
 *
8
 * Copyright 2016 Sourcefabric z.ú. and contributors.
9
 *
10
 * For the full copyright and license information, please see the
11
 * AUTHORS and LICENSE files distributed with this source code.
12
 *
13
 * @copyright 2016 Sourcefabric z.ú
14
 * @license http://www.superdesk.org/license
15
 */
16
17
namespace SWP\Bundle\MenuBundle\Form\DataTransformer;
18
19
use SWP\Bundle\MenuBundle\Doctrine\MenuItemRepositoryInterface;
20
use SWP\Bundle\MenuBundle\Model\MenuItemInterface;
21
use Symfony\Component\Form\DataTransformerInterface;
22
use Symfony\Component\Form\Exception\TransformationFailedException;
23
use Symfony\Component\Form\Exception\UnexpectedTypeException;
24
25
final class MenuItemToIdTransformer implements DataTransformerInterface
26
{
27
    /**
28
     * @var MenuItemRepositoryInterface
29
     */
30
    private $repository;
31
32 5
    public function __construct(MenuItemRepositoryInterface $repository)
33
    {
34 5
        $this->repository = $repository;
35 5
    }
36
37
    /**
38
     * Transforms an object (menu item) to a string (id).
39
     *
40
     * @param MenuItemInterface|string $menuItem
41
     *
42
     * @return string|null
43
     *
44
     * @throws TransformationFailedException if object (menu item) is of wrong type
45
     */
46 5
    public function transform($menuItem)
47
    {
48 5
        if (null === $menuItem) {
49 5
            return;
50
        }
51
52 1
        if (!$menuItem instanceof MenuItemInterface) {
53
            throw new UnexpectedTypeException($menuItem, MenuItemInterface::class);
54
        }
55
56 1
        return $menuItem->getId();
57
    }
58
59
    /**
60
     * Transforms an id to an object (menu item).
61
     *
62
     * @param string $menuItemId
63
     *
64
     * @return MenuItemInterface|null
65
     *
66
     * @throws TransformationFailedException if object (menu item) is not found
67
     */
68 4
    public function reverseTransform($menuItemId)
69
    {
70 4
        if (null === $menuItemId) {
71 3
            return;
72
        }
73
74 3
        $menuItem = $this->repository->getOneMenuItemById((int) $menuItemId);
75
76 3
        if (null === $menuItem) {
77 1
            throw new TransformationFailedException(sprintf(
78 1
                'Menu with id "%s" does not exist!',
79
                $menuItem
80
            ));
81
        }
82
83 2
        return $menuItem;
84
    }
85
}
86