UpdateForecastWidgetOptions::load()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 1
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