Completed
Push — master ( 11b317...37df4d )
by Lucas
09:27
created

TranslatableToDefaultStringTransformer   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 57
ccs 0
cts 15
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A transform() 0 10 2
A reverseTransform() 0 9 2
1
<?php
2
/**
3
 * transform Translatable instance to default string value being stored on record
4
 */
5
6
namespace Graviton\I18nBundle\Form\DataTransformer;
7
8
use Symfony\Component\Form\DataTransformerInterface;
9
use Symfony\Component\Form\Exception\TransformationFailedException;
10
use Graviton\I18nBundle\Service\I18nUtils;
11
12
/**
13
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
14
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
15
 * @link     http://swisscom.ch
16
 */
17
class TranslatableToDefaultStringTransformer implements DataTransformerInterface
18
{
19
    /**
20
     * @var I18nUtils
21
     */
22
    private $utils;
23
24
    /**
25
     * @param I18nUtils $utils i18n utils for various needs
26
     */
27
    public function __construct(I18nUtils $utils)
28
    {
29
        $this->utils = $utils;
30
    }
31
32
    /**
33
     * return as array
34
     *
35
     * the value returned here will be translated by the i18n listener later on
36
     *
37
     * @param  Translatable|null $translatable value from model
38
     *
39
     * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be null|array<string,Translatable>?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
40
     */
41
    public function transform($translatable)
42
    {
43
        if ($translatable == null) {
44
            return null;
45
        }
46
        $translated = [
47
            $this->utils->getDefaultLanguage() => $translatable
48
        ];
49
        return $translated;
50
    }
51
52
    /**
53
     * return simple string
54
     *
55
     * all we want to store after going through a for is a simple string. If there
56
     * are any other languages being sent, this is where they get stored.
57
     *
58
     * @param  array $default value from client
59
     *
60
     * @return string
61
     *
62
     * @throws TransformationFailedException
63
     */
64
    public function reverseTransform($default)
65
    {
66
        $defaultLang = $this->utils->getDefaultLanguage();
67
        if (!isset($default[$defaultLang])) {
68
            throw new TransformationFailedException(sprintf('Value must contain "%s" string', $defaultLang));
69
        }
70
        $this->utils->insertTranslatable($default);
71
        return $default[$defaultLang];
72
    }
73
}
74