Completed
Push — master ( 1de9b7...830752 )
by Kristof
38:46 queued 24:09
created

NodeBundle/Command/CronUpdateNodeCommand.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\NodeBundle\Command;
4
5
use Doctrine\ORM\EntityManager;
6
use Doctrine\ORM\EntityManagerInterface;
7
use Kunstmaan\NodeBundle\Entity\QueuedNodeTranslationAction;
8
use Kunstmaan\NodeBundle\Helper\NodeAdmin\NodeAdminPublisher;
9
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
13
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
14
15
/**
16
 * @final since 5.1
17
 * NEXT_MAJOR extend from `Command` and remove `$this->getContainer` usages
18
 */
19
class CronUpdateNodeCommand extends ContainerAwareCommand
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Bundle\Framework...d\ContainerAwareCommand has been deprecated with message: since Symfony 4.2, use {@see Command} instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
20
{
21
    /**
22
     * @var EntityManager
23
     */
24
    private $em;
25
26
    /**
27
     * @var TokenStorage
28
     */
29
    private $tokenStorage;
30
31
    /**
32
     * @var NodeAdminPublisher
33
     */
34
    private $nodePublisher;
35
36
    /**
37
     * @param EntityManagerInterface|null $em
38
     * @param TokenStorage|null           $tokenStorage
39
     * @param NodeAdminPublisher|null     $nodePublisher
40
     */
41 View Code Duplication
    public function __construct(/* EntityManagerInterface */ $em = null, /* TokenStorage */$tokenStorage = null, /* NodeAdminPublisher */ $nodePublisher = null)
42
    {
43
        parent::__construct();
44
45
        if (!$em instanceof EntityManagerInterface) {
46
            @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);
47
48
            $this->setName(null === $em ? 'kuma:nodes:cron' : $em);
49
50
            return;
51
        }
52
53
        $this->em = $em;
54
        $this->tokenStorage = $tokenStorage;
55
        $this->nodePublisher = $nodePublisher;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    protected function configure()
62
    {
63
        parent::configure();
64
65
        $this->setName('kuma:nodes:cron')
66
            ->setDescription('Do everything that needs to be run in a cron job.')
67
            ->setHelp('The <info>kuma:nodes:cron</info> will loop over all queued node translation action entries and update the nodetranslations if needed.');
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    protected function execute(InputInterface $input, OutputInterface $output)
74
    {
75
        if (null === $this->em) {
76
            $this->em = $this->getContainer()->get('doctrine.orm.entity_manager');
77
            $this->tokenStorage = $this->getContainer()->get('security.token_storage');
78
            $this->nodePublisher = $this->getContainer()->get('kunstmaan_node.admin_node.publisher');
79
        }
80
81
        $queuedNodeTranslationActions = $this->em->getRepository('KunstmaanNodeBundle:QueuedNodeTranslationAction')->findAll();
82
83
        if (count($queuedNodeTranslationActions)) {
84
            foreach ($queuedNodeTranslationActions as $queuedNodeTranslationAction) {
85
                $now = new \DateTime();
86
                if ($queuedNodeTranslationAction->getDate()->getTimestamp() < $now->getTimestamp()) {
87
                    $action = $queuedNodeTranslationAction->getAction();
88
                    {
89
                        // Set user security context
90
                        $user = $queuedNodeTranslationAction->getUser();
91
                        $runAsToken = new UsernamePasswordToken($user, null, 'foo', $user->getRoles());
92
                        $this->tokenStorage->setToken($runAsToken);
93
                    }
94
                    $nodeTranslation = $queuedNodeTranslationAction->getNodeTranslation();
95
                    switch ($action) {
96
                        case QueuedNodeTranslationAction::ACTION_PUBLISH:
97
                            $this->nodePublisher->publish($nodeTranslation, $user);
98
                            $output->writeln('Published the page ' . $nodeTranslation->getTitle());
99
100
                            break;
101
                        case QueuedNodeTranslationAction::ACTION_UNPUBLISH:
102
                            $this->nodePublisher->unPublish($nodeTranslation);
103
                            $output->writeln('Unpublished the page ' . $nodeTranslation->getTitle());
104
105
                            break;
106
                        default:
107
                            $output->writeln("Don't understand the action " . $action);
108
                    }
109
                }
110
            }
111
            $output->writeln('Done');
112
        } else {
113
            $output->writeln('No queued jobs');
114
        }
115
    }
116
}
117