Completed
Push — master ( 91fdab...75a7b9 )
by
unknown
13:37
created

Helper/NodeAdmin/NodeVersionLockHelper.php (2 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\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 BaseUser $user
39
     * @param NodeTranslation $nodeTranslation
40
     * @param bool $isPublicNodeVersion
41
     * @return bool
42
     */
43
    public function isNodeVersionLocked(BaseUser $user, NodeTranslation $nodeTranslation, $isPublicNodeVersion)
44
    {
45
        if ($this->container->getParameter('kunstmaan_node.lock_enabled')) {
46
            $this->removeExpiredLocks($nodeTranslation);
47
            $this->createNodeVersionLock($user, $nodeTranslation, $isPublicNodeVersion); // refresh lock
48
            $locks = $this->getNodeVersionLocksByNodeTranslation($nodeTranslation, $isPublicNodeVersion, $user);
49
            return count($locks) ? true : false;
50
        }
51
        return false;
52
    }
53
54
    /**
55
     * @param NodeTranslation $nodeTranslation
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
     * @return array
59
     */
60
    public function getUsersWithNodeVersionLock(NodeTranslation $nodeTranslation, $isPublicNodeVersion, BaseUser $userToExclude = null)
61
    {
62
        return  array_reduce(
63
            $this->getNodeVersionLocksByNodeTranslation($nodeTranslation, $isPublicNodeVersion, $userToExclude),
64
            function ($return, NodeVersionLock $item) {
65
                $return[] = $item->getOwner();
66
                return $return;
67
            },
68
            []
69
        );
70
    }
71
72
    /**
73
     * @param NodeTranslation $nodeTranslation
74
     */
75
    protected function removeExpiredLocks(NodeTranslation $nodeTranslation)
76
    {
77
        $threshold = $this->container->getParameter('kunstmaan_node.lock_threshold');
78
        $locks = $this->objectManager->getRepository('KunstmaanNodeBundle:NodeVersionLock')->getExpiredLocks($nodeTranslation, $threshold);
79
        foreach ($locks as $lock) {
80
            $this->objectManager->remove($lock);
81
        }
82
    }
83
84
    /**
85
     * When editing the node, create a new node translation lock.
86
     *
87
     * @param BaseUser $user
88
     * @param NodeTranslation $nodeTranslation
89
     * @param bool $isPublicVersion
90
     */
91
    protected function createNodeVersionLock(BaseUser $user, NodeTranslation $nodeTranslation, $isPublicVersion)
92
    {
93
        $lock = $this->objectManager->getRepository('KunstmaanNodeBundle:NodeVersionLock')->findOneBy([
94
            'owner' => $user->getUsername(),
95
            'nodeTranslation' => $nodeTranslation,
96
            'publicVersion' => $isPublicVersion
97
        ]);
98
        if (! $lock) {
99
            $lock = new NodeVersionLock();
100
        }
101
        $lock->setOwner($user->getUsername());
102
        $lock->setNodeTranslation($nodeTranslation);
103
        $lock->setPublicVersion($isPublicVersion);
104
        $lock->setCreatedAt(new \DateTime());
105
106
        $this->objectManager->persist($lock);
107
        $this->objectManager->flush();
108
    }
109
110
    /**
111
     * When editing a node, check if there is a lock for this node translation.
112
     *
113
     * @param NodeTranslation $nodeTranslation
114
     * @param bool $isPublicVersion
115
     * @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...
116
     * @return NodeVersionLock[]
117
     */
118
    protected function getNodeVersionLocksByNodeTranslation(NodeTranslation $nodeTranslation, $isPublicVersion, BaseUser $userToExclude = null)
119
    {
120
        $threshold = $this->container->getParameter('kunstmaan_node.lock_threshold');
121
        /** @var NodeVersionLockRepository $objectRepository */
122
        $objectRepository = $this->objectManager->getRepository('KunstmaanNodeBundle:NodeVersionLock');
123
        return $objectRepository->getLocksForNodeTranslation($nodeTranslation, $isPublicVersion, $threshold, $userToExclude);
124
    }
125
}
126