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

CronUpdateNodeCommand   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 98
Duplicated Lines 16.33 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 0
Metric Value
wmc 11
lcom 2
cbo 4
dl 16
loc 98
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 16 16 3
A configure() 0 8 1
B execute() 0 43 7

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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)
0 ignored issues
show
Duplication introduced by
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
}
117