Completed
Push — master ( 06c1ce...67d37c )
by Jeroen
06:20
created

NodeBundle/Command/CronUpdateNodeCommand.php (3 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\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
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)
0 ignored issues
show
This method seems to be duplicated in 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...
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);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
47
48
            $this->setName(null === $em ? 'kuma:nodes:cron' : $em);
49
50
            return;
51
        }
52
53
        $this->em = $em;
0 ignored issues
show
Documentation Bug introduced by
$em is of type object<Doctrine\ORM\EntityManagerInterface>, but the property $em was declared to be of type object<Doctrine\ORM\EntityManager>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof 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 given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
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
        return 0;
117
    }
118
}
119