Completed
Push — 3.x ( 839353...0c5d0b )
by Grégoire
02:14
created

ObjectAclManipulator   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 8
c 3
b 0
f 1
lcom 0
cbo 7
dl 0
loc 63
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B batchConfigureAcls() 0 57 8
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\DoctrineMongoDBAdminBundle\Util;
13
14
use Doctrine\ODM\MongoDB\DocumentManager;
15
use Sonata\AdminBundle\Admin\AdminInterface;
16
use Sonata\AdminBundle\Exception\ModelManagerException;
17
use Sonata\AdminBundle\Security\Handler\AclSecurityHandlerInterface;
18
use Sonata\AdminBundle\Util\ObjectAclManipulator as BaseObjectAclManipulator;
19
use Symfony\Component\Console\Output\OutputInterface;
20
use Symfony\Component\Security\Acl\Domain\ObjectIdentity;
21
use Symfony\Component\Security\Acl\Domain\UserSecurityIdentity;
22
23
class ObjectAclManipulator extends BaseObjectAclManipulator
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function batchConfigureAcls(OutputInterface $output, AdminInterface $admin, UserSecurityIdentity $securityIdentity = null)
29
    {
30
        $securityHandler = $admin->getSecurityHandler();
31
        if (!$securityHandler instanceof AclSecurityHandlerInterface) {
32
            $output->writeln('Admin class is not configured to use ACL : <info>ignoring</info>');
33
34
            return;
35
        }
36
37
        $output->writeln(sprintf(' > generate ACLs for %s', $admin->getCode()));
38
        $objectOwnersMsg = is_null($securityIdentity) ? '' : ' and set the object owner';
39
40
        /** @var DocumentManager $om */
41
        $om = $admin->getModelManager()->getDocumentManager();
42
        $qb = $om->createQueryBuilder($admin->getClass());
43
44
        $count = 0;
45
        $countUpdated = 0;
46
        $countAdded = 0;
47
48
        try {
49
            $batchSize = 20;
50
            $batchSizeOutput = 200;
51
            $objectIds = array();
52
53
            foreach ($qb->getQuery()->iterate() as $row) {
54
                $objectIds[] = ObjectIdentity::fromDomainObject($row);
55
                $objectIdIterator = new \ArrayIterator($objectIds);
56
57
                // detach from Doctrine, so that it can be Garbage-Collected immediately
58
                $om->detach($row);
59
60
                ++$count;
61
62
                if (($count % $batchSize) == 0) {
63
                    list($batchAdded, $batchUpdated) = $this->configureAcls($output, $admin, $objectIdIterator, $securityIdentity);
64
                    $countAdded += $batchAdded;
65
                    $countUpdated += $batchUpdated;
66
                    $objectIds = array();
67
                }
68
69
                if (($count % $batchSizeOutput) == 0) {
70
                    $output->writeln(sprintf('   - generated class ACEs%s for %s objects (added %s, updated %s)', $objectOwnersMsg, $count, $countAdded, $countUpdated));
71
                }
72
            }
73
74
            if (count($objectIds) > 0) {
75
                list($batchAdded, $batchUpdated) = $this->configureAcls($output, $admin, $objectIdIterator, $securityIdentity);
0 ignored issues
show
Bug introduced by
The variable $objectIdIterator does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
76
                $countAdded += $batchAdded;
77
                $countUpdated += $batchUpdated;
78
            }
79
        } catch (\BadMethodCallException $e) {
80
            throw new ModelManagerException('', 0, $e);
81
        }
82
83
        $output->writeln(sprintf('   - [TOTAL] generated class ACEs%s for %s objects (added %s, updated %s)', $objectOwnersMsg, $count, $countAdded, $countUpdated));
84
    }
85
}
86