Completed
Pull Request — 5.3 (#2504)
by Kristof
09:56
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\EntityManagerInterface;
6
use Kunstmaan\AdminBundle\Entity\AclChangeset;
7
use Kunstmaan\UtilitiesBundle\Helper\Shell\Shell;
8
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Kunstmaan\AdminBundle\Service\AclManager;
12
13
/**
14
 * Symfony CLI command to apply the {@link AclChangeSet} with status {@link AclChangeSet::STATUS_NEW} to their entities
15
 *
16
 * @final since 5.1
17
 * NEXT_MAJOR extend from `Command` and remove `$this->getContainer` usages
18
 */
19
class ApplyAclCommand extends ContainerAwareCommand
20
{
21
    /**
22
     * @var EntityManager
23
     */
24
    private $em = null;
25
26
    /**
27
     * @var Shell
28
     */
29
    private $shellHelper = null;
30
31
    /** @var AclManager */
32
    private $aclManager = null;
33
34 1
    public function __construct(/*AclManager*/ $aclManager = null, EntityManagerInterface $em = null, Shell $shellHelper = null)
35
    {
36 1
        parent::__construct();
37
38 1 View Code Duplication
        if (!$aclManager instanceof AclManager) {
0 ignored issues
show
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...
39 1
            @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);
40
41 1
            $this->setName(null === $aclManager ? 'kuma:acl:apply' : $aclManager);
42
43 1
            return;
44
        }
45
46
        $this->aclManager = $aclManager;
47
        $this->em = $em;
48
        $this->shellHelper = $shellHelper;
49
    }
50
51
    /**
52
     * Configures the command.
53
     */
54 1
    protected function configure()
55
    {
56 1
        parent::configure();
57
58 1
        $this->setName('kuma:acl:apply')
59 1
             ->setDescription('Apply ACL changeset.')
60 1
             ->setHelp('The <info>kuma:acl:apply</info> can be used to apply an ACL changeset recursively, changesets are fetched from the database.');
61 1
    }
62
63
    /**
64
     * Apply the {@link AclChangeSet} with status {@link AclChangeSet::STATUS_NEW} to their entities
65
     *
66
     * @param InputInterface  $input  The input
67
     * @param OutputInterface $output The output
68
     *
69
     * @return int
70
     */
71
    protected function execute(InputInterface $input, OutputInterface $output)
72
    {
73
        if (null === $this->aclManager) {
74
            $this->aclManager = $this->getContainer()->get('kunstmaan_admin.acl.manager');
75
        }
76
        if (null === $this->em) {
77
            $this->em = $this->getContainer()->get('doctrine.orm.entity_manager');
78
        }
79
        if (null === $this->shellHelper) {
80
            $this->shellHelper = $this->getContainer()->get('kunstmaan_utilities.shell');
81
        }
82
83
        // Check if another ACL apply process is currently running & do nothing if it is
84
        if ($this->isRunning()) {
85
            return;
86
        }
87
88
        $this->aclManager->applyAclChangesets();
89
    }
90
91
    /**
92
     * @return bool
93
     */
94
    private function isRunning()
95
    {
96
        // Check if we have records in running state, if so read PID & check if process is active
97
        /* @var AclChangeset $runningAclChangeset */
98
        $runningAclChangeset = $this->em->getRepository('KunstmaanAdminBundle:AclChangeset')->findRunningChangeset();
99
        if (!is_null($runningAclChangeset)) {
100
            // Found running process, check if PID is still running
101
            if (!$this->shellHelper->isRunning($runningAclChangeset->getPid())) {
102
                // PID not running, process probably failed...
103
                $runningAclChangeset->setStatus(AclChangeset::STATUS_FAILED);
104
                $this->em->persist($runningAclChangeset);
105
                $this->em->flush($runningAclChangeset);
106
            }
107
        }
108
109
        return false;
110
    }
111
}
112