Completed
Pull Request — 5.6 (#2830)
by Jeroen
14:14
created

Helper/NodeAdmin/NodeVersionLockHelper.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\Helper\NodeAdmin;
4
5
use Doctrine\Common\Persistence\ObjectManager;
6
use Kunstmaan\AdminBundle\Entity\BaseUser;
7
use Kunstmaan\NodeBundle\Entity\NodeTranslation;
8
use Kunstmaan\NodeBundle\Entity\NodeVersionLock;
9
use Kunstmaan\NodeBundle\Repository\NodeVersionLockRepository;
10
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
11
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
12
use Symfony\Component\DependencyInjection\ContainerInterface;
13
14
class NodeVersionLockHelper implements ContainerAwareInterface
15
{
16
    use ContainerAwareTrait;
17
18
    /**
19
     * @var ObjectManager
20
     */
21
    private $objectManager;
22
23
    public function __construct(ContainerInterface $container, ObjectManager $em)
24
    {
25
        $this->setContainer($container);
26
        $this->setObjectManager($em);
27
    }
28
29
    /**
30
     * @param ObjectManager $objectManager
31
     */
32
    public function setObjectManager($objectManager)
33
    {
34
        $this->objectManager = $objectManager;
35
    }
36
37
    /**
38
     * @param bool $isPublicNodeVersion
39
     *
40
     * @return bool
41
     */
42
    public function isNodeVersionLocked(BaseUser $user, NodeTranslation $nodeTranslation, $isPublicNodeVersion)
43
    {
44
        if ($this->container->getParameter('kunstmaan_node.lock_enabled')) {
45
            $this->removeExpiredLocks($nodeTranslation);
46
            $this->createNodeVersionLock($user, $nodeTranslation, $isPublicNodeVersion); // refresh lock
47
            $locks = $this->getNodeVersionLocksByNodeTranslation($nodeTranslation, $isPublicNodeVersion, $user);
48
49
            return \count($locks) ? true : false;
50
        }
51
52
        return false;
53
    }
54
55
    /**
56
     * @param BaseUser $userToExclude
0 ignored issues
show
Should the type for parameter $userToExclude not be null|BaseUser?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
57
     * @param bool     $isPublicNodeVersion
58
     *
59
     * @return array
60
     */
61
    public function getUsersWithNodeVersionLock(NodeTranslation $nodeTranslation, $isPublicNodeVersion, BaseUser $userToExclude = null)
62
    {
63
        return  array_reduce(
64
            $this->getNodeVersionLocksByNodeTranslation($nodeTranslation, $isPublicNodeVersion, $userToExclude),
65
            function ($return, NodeVersionLock $item) {
66
                $return[] = $item->getOwner();
67
68
                return $return;
69
            },
70
            []
71
        );
72
    }
73
74
    protected function removeExpiredLocks(NodeTranslation $nodeTranslation)
75
    {
76
        $threshold = $this->container->getParameter('kunstmaan_node.lock_threshold');
77
        $locks = $this->objectManager->getRepository(NodeVersionLock::class)->getExpiredLocks($nodeTranslation, $threshold);
78
        foreach ($locks as $lock) {
79
            $this->objectManager->remove($lock);
80
        }
81
    }
82
83
    /**
84
     * When editing the node, create a new node translation lock.
85
     *
86
     * @param bool $isPublicVersion
87
     */
88
    protected function createNodeVersionLock(BaseUser $user, NodeTranslation $nodeTranslation, $isPublicVersion)
89
    {
90
        $lock = $this->objectManager->getRepository(NodeVersionLock::class)->findOneBy([
91
            'owner' => $user->getUsername(),
92
            'nodeTranslation' => $nodeTranslation,
93
            'publicVersion' => $isPublicVersion,
94
        ]);
95
        if (!$lock) {
96
            $lock = new NodeVersionLock();
97
        }
98
        $lock->setOwner($user->getUsername());
99
        $lock->setNodeTranslation($nodeTranslation);
100
        $lock->setPublicVersion($isPublicVersion);
101
        $lock->setCreatedAt(new \DateTime());
102
103
        $this->objectManager->persist($lock);
104
        $this->objectManager->flush();
105
    }
106
107
    /**
108
     * When editing a node, check if there is a lock for this node translation.
109
     *
110
     * @param bool     $isPublicVersion
111
     * @param BaseUser $userToExclude
112
     *
113
     * @return NodeVersionLock[]
114
     */
115
    protected function getNodeVersionLocksByNodeTranslation(NodeTranslation $nodeTranslation, $isPublicVersion, BaseUser $userToExclude = null)
116
    {
117
        $threshold = $this->container->getParameter('kunstmaan_node.lock_threshold');
118
        /** @var NodeVersionLockRepository $objectRepository */
119
        $objectRepository = $this->objectManager->getRepository(NodeVersionLock::class);
120
121
        return $objectRepository->getLocksForNodeTranslation($nodeTranslation, $isPublicVersion, $threshold, $userToExclude);
122
    }
123
}
124