Completed
Push — master ( 6d6774...64f3ed )
by Jeroen
11:23 queued 05:13
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 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('KunstmaanNodeBundle:NodeVersionLock')->getExpiredLocks($nodeTranslation, $threshold);
0 ignored issues
show
It seems like you code against a concrete implementation and not the interface Doctrine\Persistence\ObjectRepository as the method getExpiredLocks() does only exist in the following implementations of said interface: Kunstmaan\AdminListBundl...tyVersionLockRepository, Kunstmaan\NodeBundle\Rep...deVersionLockRepository.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
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('KunstmaanNodeBundle:NodeVersionLock')->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('KunstmaanNodeBundle:NodeVersionLock');
129
130
        return $objectRepository->getLocksForNodeTranslation($nodeTranslation, $isPublicVersion, $threshold, $userToExclude);
131
    }
132
}
133