Completed
Push — master ( 664fb1...60ffba )
by
unknown
26:32
created

UpdateForecastWidgetOptions::processWidget()   D

Complexity

Conditions 10
Paths 18

Size

Total Lines 41
Code Lines 24

Duplication

Lines 24
Ratio 58.54 %

Importance

Changes 0
Metric Value
dl 24
loc 41
rs 4.8196
c 0
b 0
f 0
cc 10
eloc 24
nc 18
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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