Completed
Push — master ( 91fdab...75a7b9 )
by
unknown
13:37
created

Kunstmaan/AdminBundle/Command/ApplyAclCommand.php (2 issues)

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
namespace Kunstmaan\AdminBundle\Command;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use Kunstmaan\AdminBundle\Entity\AclChangeset;
7
use Kunstmaan\AdminBundle\Helper\Security\Acl\Permission\PermissionAdmin;
8
use Kunstmaan\AdminBundle\Repository\AclChangesetRepository;
9
use Kunstmaan\UtilitiesBundle\Helper\Shell\Shell;
10
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Output\OutputInterface;
13
use Kunstmaan\AdminBundle\Service\AclManager;
14
15
/**
16
 * Symfony CLI command to apply the {@link AclChangeSet} with status {@link AclChangeSet::STATUS_NEW} to their entities
17
 *
18
 * @final since 5.1
19
 * NEXT_MAJOR extend from `Command` and remove `$this->getContainer` usages
20
 */
21
class ApplyAclCommand extends ContainerAwareCommand
22
{
23
24
    /**
25
     * @var EntityManager
26
     */
27
    private $em = null;
28
29
    /**
30
     * @var Shell
31
     */
32
    private $shellHelper = null;
33
34
    /** @var AclManager */
35
    private $aclManager = null;
36
37
    public function __construct(/*AclManager*/ $aclManager = null, EntityManagerInterface $em = null, Shell $shellHelper = null)
38
    {
39
        parent::__construct();
40
41
        if (!$aclManager instanceof AclManager) {
42
            @trigger_error(sprintf('Passing a command name as the first argument of "%s" is deprecated since version symfony 3.4 and will be removed in symfony 4.0. If the command was registered by convention, make it a service instead. ', __METHOD__), E_USER_DEPRECATED);
43
44
            $this->setName(null === $aclManager ? 'kuma:acl:apply' : $aclManager);
45
46
            return;
47
        }
48
49
        $this->aclManager = $aclManager;
50
        $this->em = $em;
0 ignored issues
show
Documentation Bug introduced by
It seems like $em can also be of type object<Doctrine\ORM\EntityManagerInterface>. However, the property $em is declared as type object<Kunstmaan\AdminBu...\Command\EntityManager>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
51
        $this->shellHelper = $shellHelper;
52
    }
53
54
    /**
55
     * Configures the command.
56
     */
57
    protected function configure()
58
    {
59
        parent::configure();
60
61
        $this->setName('kuma:acl:apply')
62
             ->setDescription('Apply ACL changeset.')
63
             ->setHelp("The <info>kuma:acl:apply</info> can be used to apply an ACL changeset recursively, changesets are fetched from the database.");
64
    }
65
66
    /**
67
     * Apply the {@link AclChangeSet} with status {@link AclChangeSet::STATUS_NEW} to their entities
68
     *
69
     * @param InputInterface  $input  The input
70
     * @param OutputInterface $output The output
71
     *
72
     * @return int
0 ignored issues
show
Should the return type not be integer|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
73
     */
74
    protected function execute(InputInterface $input, OutputInterface $output)
75
    {
76
        if (null === $this->aclManager) {
77
            $this->aclManager = $this->getContainer()->get('kunstmaan_admin.acl.manager');
78
        }
79
        if (null === $this->em) {
80
            $this->em = $this->getContainer()->get('doctrine.orm.entity_manager');
81
        }
82
        if (null === $this->shellHelper) {
83
            $this->shellHelper = $this->getContainer()->get('kunstmaan_utilities.shell');
84
        }
85
86
        // Check if another ACL apply process is currently running & do nothing if it is
87
        if ($this->isRunning()) {
88
            return;
89
        }
90
91
       $this->aclManager->applyAclChangesets();
92
    }
93
94
    /**
95
     * @return boolean
96
     */
97
    private function isRunning()
98
    {
99
        // Check if we have records in running state, if so read PID & check if process is active
100
        /* @var AclChangeset $runningAclChangeset */
101
        $runningAclChangeset = $this->em->getRepository('KunstmaanAdminBundle:AclChangeset')->findRunningChangeset();
102
        if (!is_null($runningAclChangeset)) {
103
            // Found running process, check if PID is still running
104
            if (!$this->shellHelper->isRunning($runningAclChangeset->getPid())) {
105
                // PID not running, process probably failed...
106
                $runningAclChangeset->setStatus(AclChangeset::STATUS_FAILED);
107
                $this->em->persist($runningAclChangeset);
108
                $this->em->flush($runningAclChangeset);
109
            }
110
        }
111
112
        return false;
113
    }
114
115
}
116