Completed
Push — master ( b87479...e865f1 )
by
unknown
8s
created

src/Util/ObjectAclManipulator.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\DoctrineMongoDBAdminBundle\Util;
15
16
use Doctrine\ODM\MongoDB\DocumentManager;
17
use Sonata\AdminBundle\Admin\AdminInterface;
18
use Sonata\AdminBundle\Exception\ModelManagerException;
19
use Sonata\AdminBundle\Security\Handler\AclSecurityHandlerInterface;
20
use Sonata\AdminBundle\Util\ObjectAclManipulator as BaseObjectAclManipulator;
21
use Symfony\Component\Console\Output\OutputInterface;
22
use Symfony\Component\Security\Acl\Domain\ObjectIdentity;
23
use Symfony\Component\Security\Acl\Domain\UserSecurityIdentity;
24
25
class ObjectAclManipulator extends BaseObjectAclManipulator
26
{
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function batchConfigureAcls(OutputInterface $output, AdminInterface $admin, UserSecurityIdentity $securityIdentity = null): void
31
    {
32
        $securityHandler = $admin->getSecurityHandler();
33
        if (!$securityHandler instanceof AclSecurityHandlerInterface) {
34
            $output->writeln('Admin class is not configured to use ACL : <info>ignoring</info>');
35
36
            return;
37
        }
38
39
        $output->writeln(sprintf(' > generate ACLs for %s', $admin->getCode()));
40
        $objectOwnersMsg = null === $securityIdentity ? '' : ' and set the object owner';
41
42
        /** @var DocumentManager $om */
43
        $om = $admin->getModelManager()->getDocumentManager();
44
        $qb = $om->createQueryBuilder($admin->getClass());
45
46
        $count = 0;
47
        $countUpdated = 0;
48
        $countAdded = 0;
49
50
        try {
51
            $batchSize = 20;
52
            $batchSizeOutput = 200;
53
            $objectIds = [];
54
55
            foreach ($qb->getQuery()->iterate() as $row) {
56
                $objectIds[] = ObjectIdentity::fromDomainObject($row);
57
                $objectIdIterator = new \ArrayIterator($objectIds);
58
59
                // detach from Doctrine, so that it can be Garbage-Collected immediately
60
                $om->detach($row);
61
62
                ++$count;
63
64
                if (0 == ($count % $batchSize)) {
65
                    list($batchAdded, $batchUpdated) = $this->configureAcls($output, $admin, $objectIdIterator, $securityIdentity);
66
                    $countAdded += $batchAdded;
67
                    $countUpdated += $batchUpdated;
68
                    $objectIds = [];
69
                }
70
71
                if (0 == ($count % $batchSizeOutput)) {
72
                    $output->writeln(sprintf(
73
                        '   - generated class ACEs%s for %s objects (added %s, updated %s)',
74
                        $objectOwnersMsg,
75
                        $count,
76
                        $countAdded,
77
                        $countUpdated
78
                    ));
79
                }
80
            }
81
82
            if (count($objectIds) > 0) {
83
                list($batchAdded, $batchUpdated) = $this->configureAcls($output, $admin, $objectIdIterator, $securityIdentity);
0 ignored issues
show
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...
84
                $countAdded += $batchAdded;
85
                $countUpdated += $batchUpdated;
86
            }
87
        } catch (\BadMethodCallException $e) {
88
            throw new ModelManagerException('', 0, $e);
89
        }
90
91
        $output->writeln(sprintf(
92
            '   - [TOTAL] generated class ACEs%s for %s objects (added %s, updated %s)',
93
            $objectOwnersMsg,
94
            $count,
95
            $countAdded,
96
            $countUpdated
97
        ));
98
    }
99
}
100