UpdateForecastWidgetOptions   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 69
Duplicated Lines 34.78 %

Coupling/Cohesion

Components 0
Dependencies 8

Importance

Changes 0
Metric Value
wmc 12
lcom 0
cbo 8
dl 24
loc 69
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 16 2
D processWidget() 24 41 10

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
namespace Oro\Bundle\MagentoBundle\Migrations\Data\ORM;
4
5
use Doctrine\ORM\EntityRepository;
6
7
use Doctrine\Common\DataFixtures\AbstractFixture;
8
use Doctrine\Common\Persistence\ObjectManager;
9
10
use Oro\Bundle\DashboardBundle\Entity\Widget;
11
use Oro\Bundle\UserBundle\Entity\User;
12
use Oro\Bundle\OrganizationBundle\Entity\BusinessUnit;
13
14
class UpdateForecastWidgetOptions extends AbstractFixture
15
{
16
    /**
17
     * {@inheritdoc}
18
     */
19
    public function load(ObjectManager $manager)
20
    {
21
        /** @var EntityRepository $repository */
22
        $repository = $manager->getRepository('Oro\Bundle\DashboardBundle\Entity\Widget');
23
24
        $qb = $repository->createQueryBuilder('w');
25
        $qb->andWhere('w.name = :name');
26
        $qb->setParameter('name', 'forecast_of_opportunities');
27
        $widgets = $qb->getQuery()->getResult();
28
        foreach ($widgets as $widget) {
29
            $this->processWidget($widget);
30
        }
31
32
33
        $manager->flush();
34
    }
35
36
    /**
37
     * Update old forecast_of_opportunities configuration
38
     *
39
     * @param Widget $widget
40
     */
41
    protected function processWidget(Widget $widget)
42
    {
43
        $needUpdate = false;
44
        $options    = $widget->getOptions();
45
        $owners     = [
46
            'roles'         => [],
47
            'users'         => [],
48
            'businessUnits' => []
49
        ];
50
51 View Code Duplication
        if (!empty($options['businessUnits'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
52
            if (is_array($options['businessUnits'])) {
53
                /** @var BusinessUnit $businessUnit */
54
                foreach ($options['businessUnits'] as $businessUnit) {
55
                    if ($businessUnit instanceof BusinessUnit) {
56
                        $owners['businessUnits'][] = $businessUnit->getId();
57
                        $needUpdate                = true;
58
                    }
59
                }
60
            }
61
            unset($options['businessUnits']);
62
        }
63
64 View Code Duplication
        if (!empty($options['owners'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
65
            if (is_array($options['owners'])) {
66
                /** @var User $user */
67
                foreach ($options['owners'] as $user) {
68
                    if ($user instanceof User) {
69
                        $owners['users'][] = $user->getId();
70
                        $needUpdate        = true;
71
                    }
72
                }
73
            }
74
            unset($options['owners']);
75
        }
76
77
        if ($needUpdate) {
78
            $options['owners'] = $owners;
79
            $widget->setOptions($options);
80
        }
81
    }
82
}
83