Completed
Pull Request — 5.0 (#2104)
by Kevin
13:45 queued 04:21
created

Kunstmaan/AdminBundle/Command/ApplyAclCommand.php (1 issue)

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\EntityManager;
6
7
use Kunstmaan\AdminBundle\Entity\AclChangeset;
8
use Kunstmaan\AdminBundle\Helper\Security\Acl\Permission\PermissionAdmin;
9
use Kunstmaan\AdminBundle\Repository\AclChangesetRepository;
10
11
use Kunstmaan\UtilitiesBundle\Helper\Shell\Shell;
12
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Output\OutputInterface;
15
16
/**
17
 * Symfony CLI command to apply the {@link AclChangeSet} with status {@link AclChangeSet::STATUS_NEW} to their entities
18
 */
19
class ApplyAclCommand extends ContainerAwareCommand
20
{
21
22
    /**
23
     * @var EntityManager
24
     */
25
    private $em = null;
26
27
    /**
28
     * @var Shell
29
     */
30
    private $shellHelper = null;
31
32
    /**
33
     * Configures the command.
34
     */
35
    protected function configure()
36
    {
37
        parent::configure();
38
39
        $this->setName('kuma:acl:apply')
40
             ->setDescription('Apply ACL changeset.')
41
             ->setHelp("The <info>kuma:acl:apply</info> can be used to apply an ACL changeset recursively, changesets are fetched from the database.");
42
    }
43
44
    /**
45
     * Apply the {@link AclChangeSet} with status {@link AclChangeSet::STATUS_NEW} to their entities
46
     *
47
     * @param InputInterface  $input  The input
48
     * @param OutputInterface $output The output
49
     *
50
     * @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...
51
     */
52
    protected function execute(InputInterface $input, OutputInterface $output)
53
    {
54
        $this->em = $this->getContainer()->get('doctrine.orm.entity_manager');
55
        $this->shellHelper = $this->getContainer()->get('kunstmaan_utilities.shell');
56
        /* @var PermissionAdmin $permissionAdmin */
57
        $permissionAdmin = $this->getContainer()->get('kunstmaan_admin.permissionadmin');
58
59
        // Check if another ACL apply process is currently running & do nothing if it is
60
        if ($this->isRunning()) {
61
            return;
62
        }
63
        /* @var AclChangesetRepository $aclRepo */
64
        $aclRepo = $this->em->getRepository('KunstmaanAdminBundle:AclChangeset');
65
        do {
66
            /* @var AclChangeset $changeset */
67
            $changeset = $aclRepo->findNewChangeset();
68
            if (is_null($changeset)) {
69
                break;
70
            }
71
            $changeset->setPid(getmypid());
72
            $changeset->setStatus(AclChangeset::STATUS_RUNNING);
73
            $this->em->persist($changeset);
74
            $this->em->flush($changeset);
75
76
            $entity = $this->em->getRepository($changeset->getRefEntityName())->find($changeset->getRefId());
77
            $permissionAdmin->applyAclChangeset($entity, $changeset->getChangeset());
78
79
            $changeset->setStatus(AclChangeset::STATUS_FINISHED);
80
            $this->em->persist($changeset);
81
            $this->em->flush($changeset);
82
83
            $hasPending = $aclRepo->hasPendingChangesets();
84
        } while ($hasPending);
85
    }
86
87
    /**
88
     * @return boolean
89
     */
90
    private function isRunning()
91
    {
92
        // Check if we have records in running state, if so read PID & check if process is active
93
        /* @var AclChangeset $runningAclChangeset */
94
        $runningAclChangeset = $this->em->getRepository('KunstmaanAdminBundle:AclChangeset')->findRunningChangeset();
95
        if (!is_null($runningAclChangeset)) {
96
            // Found running process, check if PID is still running
97
            if (!$this->shellHelper->isRunning($runningAclChangeset->getPid())) {
98
                // PID not running, process probably failed...
99
                $runningAclChangeset->setStatus(AclChangeset::STATUS_FAILED);
100
                $this->em->persist($runningAclChangeset);
101
                $this->em->flush($runningAclChangeset);
102
            }
103
        }
104
105
        return false;
106
    }
107
108
}
109