Completed
Pull Request — develop (#542)
by Mathias
09:01
created

UpdateFilesPermissionsSubscriber::onFlush()   B

Complexity

Conditions 8
Paths 11

Size

Total Lines 37
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 37
ccs 0
cts 27
cp 0
rs 8.4444
c 0
b 0
f 0
cc 8
nc 11
nop 1
crap 72
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de)
7
 * @license   MIT
8
 */
9
10
/** JobReferencesUpdateListener.php */
11
namespace Applications\Repository\Event;
12
13
use Doctrine\ODM\MongoDB\Event\LifecycleEventArgs;
14
use Doctrine\Common\EventSubscriber;
15
use Doctrine\ODM\MongoDB\Events;
16
use Doctrine\ODM\MongoDB\Event\OnFlushEventArgs;
17
use Applications\Entity\ApplicationInterface;
18
19
/**
20
 * class for updating file permissions
21
 */
22
class UpdateFilesPermissionsSubscriber implements EventSubscriber
23
{
24
    /**
25
     * Gets events
26
     *
27
     * @see \Doctrine\Common\EventSubscriber::getSubscribedEvents()
28
     */
29 3
    public function getSubscribedEvents()
30
    {
31 3
        return array(Events::onFlush);
32
    }
33
    
34
    /**
35
     * Updates fiile permissions on Flush
36
     *
37
     * @param OnFlushEventArgs $eventArgs
38
     * @return boolean
39
     */
40
    public function onFlush(OnFlushEventArgs $eventArgs)
41
    {
42
        $dm  = $eventArgs->getDocumentManager();
43
        $uow = $dm->getUnitOfWork();
44
        
45
        $filter = function ($element) {
46
            return $element instanceof ApplicationInterface
47
                   && $element->getPermissions()->hasChanged();
48
        };
49
        
50
        $inserts = array_filter($uow->getScheduledDocumentInsertions(), $filter);
51
        $updates = array_filter($uow->getScheduledDocumentUpdates(), $filter);
52
        
53
        foreach (array($inserts, $updates) as $isUpdate => $documents) {
54
            foreach ($documents as $document) { /* @var \Applications\Entity\Application $document */
55
                $permissions = $document->getPermissions();
56
               
57
                foreach ($document->getAttachments() as $attachment) {  /* @var \Applications\Entity\Attachment $attachment */
58
                    $attachment->getPermissions()
59
                               ->clear()
60
                               ->inherit($permissions);
61
                    if ($isUpdate) {
62
                        $uow->computeChangeSet(
63
                            $dm->getClassMetadata(get_class($attachment)),
64
                            $attachment
65
                        );
66
                    }
67
                }
68
                
69
                if ($image = $document->getContact()->getImage()) {
70
                    $image->getPermissions()
71
                          ->clear()
72
                          ->inherit($permissions);
73
                    if ($isUpdate) {
74
                        $uow->computeChangeSet(
75
                            $dm->getClassMetadata(get_class($image)),
76
                            $image
77
                        );
78
                    }
79
                }
80
            }
81
        }
82
    }
83
}
84