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