Completed
Push — master ( 43fb10...664b94 )
by
unknown
12:57
created

UpdateCallAccessLevels::getDependencies()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace Oro\Bridge\CrmCall\Migrations\Data\ORM;
4
5
use Doctrine\Common\DataFixtures\AbstractFixture;
6
use Doctrine\Common\Persistence\ObjectManager;
7
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
8
9
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
10
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
11
use Symfony\Component\Security\Acl\Model\SecurityIdentityInterface;
12
use Symfony\Component\Yaml\Yaml;
13
14
use Oro\Bundle\UserBundle\Entity\Role;
15
use Oro\Bundle\SecurityBundle\Acl\Persistence\AclManager;
16
17
class UpdateCallAccessLevels extends AbstractFixture implements DependentFixtureInterface, ContainerAwareInterface
18
{
19
    use ContainerAwareTrait;
20
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function getDependencies()
25
    {
26
        return [
27
            'OroCRM\Bundle\DemoDataBundle\Migrations\Data\ORM\LoadRolesData'
28
        ];
29
    }
30
31
    /**
32
     * Update call access levels
33
     *
34
     * @param ObjectManager $manager
35
     */
36
    public function load(ObjectManager $manager)
37
    {
38
        if ($this->container->hasParameter('installed') && $this->container->getParameter('installed')) {
39
            return;
40
        }
41
42
        /** @var AclManager $aclManager */
43
        $aclManager = $this->container->get('oro_security.acl.manager');
44
45
        $fileName = $this->container
46
            ->get('kernel')
47
            ->locateResource('@OroCRMCallBridgeBundle/Migrations/Data/ORM/CrmRoles/roles.yml');
48
49
        $fileName  = str_replace('/', DIRECTORY_SEPARATOR, $fileName);
50
        $rolesData = Yaml::parse(file_get_contents($fileName));
51
52
        foreach ($rolesData as $roleName => $roleConfigData) {
0 ignored issues
show
Bug introduced by
The expression $rolesData of type string|array|object<stdClass> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
53
            if (!array_key_exists('bap_role', $roleConfigData)) {
54
                continue;
55
            }
56
57
            $role = $manager->getRepository('OroUserBundle:Role')
58
                            ->findOneBy(['role' => $roleConfigData['bap_role']]);
59
60 View Code Duplication
            if ($aclManager->isAclEnabled()) {
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...
61
                $sid = $aclManager->getSid($role);
62
                foreach ($roleConfigData['permissions'] as $permission => $acls) {
63
                    $this->processPermission($aclManager, $sid, $permission, $acls);
64
                }
65
            }
66
        }
67
68
        $aclManager->flush();
69
        $manager->flush();
70
    }
71
72
    /**
73
     * @param AclManager $aclManager
74
     * @param SecurityIdentityInterface $sid
75
     * @param string $permission
76
     * @param array $acls
77
     */
78 View Code Duplication
    protected function processPermission(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
79
        AclManager $aclManager,
80
        SecurityIdentityInterface $sid,
81
        $permission,
82
        array $acls
83
    ) {
84
        $oid = $aclManager->getOid(str_replace('|', ':', $permission));
85
86
        $extension = $aclManager->getExtensionSelector()->select($oid);
87
        $maskBuilders = $extension->getAllMaskBuilders();
88
89
        foreach ($maskBuilders as $maskBuilder) {
90
            $mask = $maskBuilder->reset()->get();
91
92
            foreach ($acls as $acl) {
93
                if ($maskBuilder->hasMask('MASK_' . $acl)) {
94
                    $mask = $maskBuilder->add($acl)->get();
95
                }
96
            }
97
98
            $aclManager->setPermission($sid, $oid, $mask);
99
        }
100
    }
101
}
102