<commit_before><?php

namespace Kunstmaan\AdminNodeBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

use Kunstmaan\AdminNodeBundle\Entity\AclChangeset;

/**
 */
class ApplyAclCommand extends ContainerAwareCommand
{

    /**
     * @var EntityManager
     */
    private $em = null;

    /**
     * @var \Kunstmaan\AdminNodeBundle\Helper\ShellHelper
     */
    private $shellHelper = null;

    /**
     * @var Node
     */
    private $rootNode;

    protected function configure()
    {
        parent::configure();

        $this->setName('kuma:acl:apply')
            ->setDescription('Apply ACL changeset.')
            ->setHelp("The <info>kuma:acl:apply</info> can be used to apply an ACL changeset recursively, changesets are fetched from the database.")
        ;
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $this->em = $this->getContainer()->get('doctrine.orm.entity_manager');
        $this->shellHelper = $this->getContainer()->get('kunstmaan.shell_helper');
        $this->permissionAdmin = $this->getContainer()->get('kunstmaan_admin.permissionadmin');

        // Check if another ACL apply process is currently running & do nothing if it is
        if ($this->isRunning()) return;

        $aclRepo = $this->em->getRepository('KunstmaanAdminNodeBundle:AclChangeset');
        $hasPending = true;
        do {
            /**
             * @var AclChangeset $changeset
             */
            $changeset = $aclRepo->findNewChangeset();
            if (is_null($changeset)) {
                break;
            }
            $changeset->setPid(getmypid());
            $changeset->setStatus(AclChangeset::STATUS_RUNNING);
            $this->em->persist($changeset);
            $this->em->flush();

            $this->permissionAdmin->applyAclChangeset($changeset->getNode(), $changeset->getChangeset());

            $changeset->setStatus(AclChangeset::STATUS_FINISHED);
            $this->em->persist($changeset);
            $this->em->flush();

            $hasPending = $aclRepo->hasPendingChangesets();
        } while ($hasPending);
    }

    private function isRunning()
    {
        // Check if we have records in running state, if so read PID & check if process is active
        /**
         * @var AclChangeset
         */
        $runningAclChangeset = $this->em->getRepository('KunstmaanAdminNodeBundle:AclChangeset')->findRunningChangeset();
        if (!is_null($runningAclChangeset)) {
            // Found running process, check if PID is still running
            if (!$this->shellHelper->isProcessRunning($runningAclChangeset->getPid())) {
                // PID not running, process probably failed...
                $runningAclChangeset->setStatus(AclChangeset::STATUS_FAILED);
                $this->em->persist($runningAclChangeset);
                $this->em->flush();
            }
        }

        return false;
    }

}
<commit_msg>type hints added<commit_after><?php

namespace Kunstmaan\AdminNodeBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

use Kunstmaan\AdminNodeBundle\Entity\AclChangeset;
use Kunstmaan\AdminBundle\Permission\PermissionAdmin;

/**
 */
class ApplyAclCommand extends ContainerAwareCommand
{

    /**
     * @var EntityManager
     */
    private $em = null;

    /**
     * @var \Kunstmaan\AdminNodeBundle\Helper\ShellHelper
     */
    private $shellHelper = null;

    /**
     * @var Node
     */
    private $rootNode;

    protected function configure()
    {
        parent::configure();

        $this->setName('kuma:acl:apply')
            ->setDescription('Apply ACL changeset.')
            ->setHelp("The <info>kuma:acl:apply</info> can be used to apply an ACL changeset recursively, changesets are fetched from the database.")
        ;
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $this->em = $this->getContainer()->get('doctrine.orm.entity_manager');
        $this->shellHelper = $this->getContainer()->get('kunstmaan.shell_helper');
        /* @var PermissionAdmin $permissionAdmin */
        $permissionAdmin = $this->getContainer()->get('kunstmaan_admin.permissionadmin');

        // Check if another ACL apply process is currently running & do nothing if it is
        if ($this->isRunning()) return;
        $aclRepo = $this->em->getRepository('KunstmaanAdminNodeBundle:AclChangeset');
        do {
            /**
             * @var AclChangeset $changeset
             */
            $changeset = $aclRepo->findNewChangeset();
            if (is_null($changeset)) {
                break;
            }
            $changeset->setPid(getmypid());
            $changeset->setStatus(AclChangeset::STATUS_RUNNING);
            $this->em->persist($changeset);
            $this->em->flush();

            $permissionAdmin->applyAclChangeset($changeset->getNode(), $changeset->getChangeset());

            $changeset->setStatus(AclChangeset::STATUS_FINISHED);
            $this->em->persist($changeset);
            $this->em->flush();

            $hasPending = $aclRepo->hasPendingChangesets();
        } while ($hasPending);
    }

    private function isRunning()
    {
        // Check if we have records in running state, if so read PID & check if process is active
        /**
         * @var AclChangeset
         */
        $runningAclChangeset = $this->em->getRepository('KunstmaanAdminNodeBundle:AclChangeset')->findRunningChangeset();
        if (!is_null($runningAclChangeset)) {
            // Found running process, check if PID is still running
            if (!$this->shellHelper->isProcessRunning($runningAclChangeset->getPid())) {
                // PID not running, process probably failed...
                $runningAclChangeset->setStatus(AclChangeset::STATUS_FAILED);
                $this->em->persist($runningAclChangeset);
                $this->em->flush();
            }
        }

        return false;
    }

}

{'fraction_non_alphanumeric': 0.12925497752087348, 'fraction_numerical': 0.0, 'mean_word_length': 2.4663327768503063, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 91, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2975736', 'n_tokens_mistral': 1745, 'n_tokens_neox': 1735, 'n_words': 319}