Completed
Pull Request — master (#290)
by Leny
07:15
created

ViewTranslationManager::addTranslation()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 27
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 27
rs 8.439
cc 5
eloc 21
nc 6
nop 3
1
<?php
2
3
namespace Victoire\Bundle\I18nBundle\Manager;
4
5
use Doctrine\Orm\EntityManager;
6
use Victoire\Bundle\BusinessPageBundle\Entity\BusinessTemplate;
7
use Victoire\Bundle\CoreBundle\Entity\View;
8
use Victoire\Bundle\PageBundle\Entity\BasePage;
9
10
/**
11
 * Page helper
12
 * ref: victoire_i18n.view_translation_manager.
13
 */
14
class ViewTranslationManager
15
{
16
    protected $parameterConverter;
17
    protected $businessEntityHelper;
18
    protected $em;
19
    protected $viewReferenceBuilder;
20
    protected $viewReferenceHelper;
21
    protected $viewReferenceProvider;
22
23
    /**
24
     * Constructor.
25
     *
26
     * @param EntityManager $entityManager
27
     */
28
    public function __construct(EntityManager $entityManager)
0 ignored issues
show
Bug introduced by
You have injected the EntityManager via parameter $entityManager. This is generally not recommended as it might get closed and become unusable. Instead, it is recommended to inject the ManagerRegistry and retrieve the EntityManager via getManager() each time you need it.

The EntityManager might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:

function someFunction(ManagerRegistry $registry) {
    $em = $registry->getManager();
    $em->getConnection()->beginTransaction();
    try {
        // Do something.
        $em->getConnection()->commit();
    } catch (\Exception $ex) {
        $em->getConnection()->rollback();
        $em->close();

        throw $ex;
    }
}

If that code throws an exception and the EntityManager is closed. Any other code which depends on the same instance of the EntityManager during this request will fail.

On the other hand, if you instead inject the ManagerRegistry, the getManager() method guarantees that you will always get a usable manager instance.

Loading history...
29
    {
30
        $this->em = $entityManager;
31
    }
32
33
    /**
34
     * @param View $view, the view to translatate
0 ignored issues
show
Bug introduced by
There is no parameter named $view,. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
35
     * @param $templatename the new name of the view
36
     * @param $loopindex the current loop of iteration in recursion
37
     * @param $locale the target locale to translate view
38
     *
39
     * this methods allow you to add a translation to any view
40
     * recursively to its subview
41
     */
42
    public function addTranslation(View $view, $viewName = null, $locale)
43
    {
44
        $template = null;
45
        if ($view->getTemplate()) {
46
            $template = $view->getTemplate();
47
            if ($template->getI18n()->getTranslation($locale)) {
0 ignored issues
show
Bug introduced by
The method getI18n cannot be called on $template (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
48
                $template = $template->getI18n()->getTranslation($locale);
0 ignored issues
show
Bug introduced by
The method getI18n cannot be called on $template (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
49
            } else {
50
                $templateName = $template->getName().'-'.$locale;
0 ignored issues
show
Bug introduced by
The method getName cannot be called on $template (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
51
                $this->em->refresh($view);
52
                $template = $this->addTranslation($template, $templateName, $locale);
0 ignored issues
show
Documentation introduced by
$template is of type string, but the function expects a object<Victoire\Bundle\CoreBundle\Entity\View>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
53
            }
54
        }
55
        $view->setLocale($locale);
56
        $view->setTemplate($template);
57
        $clonedView = $this->cloneView($view, $viewName, $locale);
0 ignored issues
show
Unused Code introduced by
The call to ViewTranslationManager::cloneView() has too many arguments starting with $locale.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
58
        if ($clonedView instanceof BasePage && $view->getTemplate()) {
59
            $template->addPage($clonedView);
60
        }
61
        $i18n = $view->getI18n();
62
        $i18n->setTranslation($locale, $clonedView);
0 ignored issues
show
Bug introduced by
The method setTranslation cannot be called on $i18n (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
63
        $this->em->persist($clonedView);
64
        $this->em->refresh($view);
65
        $this->em->flush();
66
67
        return $clonedView;
68
    }
69
70
    /**
71
     * @param View $view
72
     * @param $templateName the future name of the clone
73
     *
74
     * this methods allows you to clone a view and its widgets and also the widgetmap
75
     */
76
    public function cloneView(View $view, $templateName = null)
77
    {
78
        $clonedView = clone $view;
79
        $this->em->refresh($view);
80
        $widgetMapClone = $clonedView->getWidgetMap(false);
81
        $arrayMapOfWidgetMap = [];
82
        if (null !== $templateName) {
83
            $clonedView->setName($templateName);
84
        }
85
86
        $clonedView->setId(null);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a object<Victoire\Bundle\CoreBundle\Entity\id>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
87
        $this->em->persist($clonedView);
88
89
        if (!$clonedView instanceof BusinessTemplate) {
90
            $widgetLayoutSlots = [];
91
            $newWidgets = [];
92
            foreach ($clonedView->getWidgets() as $widgetKey => $widgetVal) {
0 ignored issues
show
Bug introduced by
The expression $clonedView->getWidgets() of type string is not traversable.
Loading history...
93
                $clonedWidget = clone $widgetVal;
94
                $clonedWidget->setId(null);
95
                $clonedWidget->setView($clonedView);
96
                $this->em->persist($clonedWidget);
97
                $newWidgets[] = $clonedWidget;
98
                $arrayMapOfWidgetMap[$widgetVal->getId()] = $clonedWidget;
99
                if ($widgetVal instanceof WidgetLayout) {
0 ignored issues
show
Bug introduced by
The class Victoire\Bundle\I18nBundle\Manager\WidgetLayout does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
100
                    $id = $widgetVal->getId();
101
                    $widgetLayoutSlots[$id] = $clonedWidget;
102
                }
103
            }
104
            $clonedView->setWidgets($newWidgets);
0 ignored issues
show
Documentation introduced by
$newWidgets is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
105
            $this->em->persist($clonedView);
106
            $this->em->flush();
107
            $widgetSlotMap = [];
108
            foreach ($widgetLayoutSlots as $_id => $_widget) {
109
                foreach ($clonedView->getWidgets() as $_clonedWidget) {
0 ignored issues
show
Bug introduced by
The expression $clonedView->getWidgets() of type string is not traversable.
Loading history...
110
                    if (preg_match('/^'.$_id.'_(.)/', $_clonedWidget->getSlot(), $matches)) {
111
                        $newSlot = $_widget->getId().'_'.$matches[1];
112
                        $oldSlot = $_clonedWidget->getSlot();
113
                        $_clonedWidget->setSlot($newSlot);
114
                        $widgetSlotMap[$oldSlot] = $newSlot;
115
                    }
116
                }
117
            }
118
119
            $this->em->flush();
120
            foreach ($widgetMapClone as $wigetSlotCloneKey => $widgetSlotCloneVal) {
121
                foreach ($widgetSlotCloneVal as $widgetMapItemKey => $widgetMapItemVal) {
122
                    if (isset($arrayMapOfWidgetMap[$widgetMapItemVal['widgetId']])) {
123
                        $widgetId = $arrayMapOfWidgetMap[$widgetMapItemVal['widgetId']]->getId();
124
                        $widgetMapItemVal['widgetId'] = $widgetId;
125
                        if (array_key_exists($wigetSlotCloneKey, $widgetSlotMap)) {
126
                            $wigetSlotCloneKey = $widgetSlotMap[$wigetSlotCloneKey];
127
                        }
128
                        $widgetMapClone[$wigetSlotCloneKey][$widgetMapItemKey] = $widgetMapItemVal;
129
                    }
130
                }
131
            }
132
133
            $clonedView->setSlots([]);
0 ignored issues
show
Documentation introduced by
array() is of type array, but the function expects a object<Victoire\Bundle\CoreBundle\Entity\unknown>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
134
            $clonedView->setWidgetMap($widgetMapClone);
0 ignored issues
show
Documentation introduced by
$widgetMapClone is of type array, but the function expects a object<Victoire\Bundle\C...undle\Entity\widgetMap>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
135
            $this->em->persist($clonedView);
136
            $this->em->flush();
137
        }
138
139
        return $clonedView;
140
    }
141
}
142