Completed
Push — develop ( b80884...23366e )
by
unknown
10:52
created

process()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 8.7972
c 0
b 0
f 0
cc 4
eloc 14
nc 4
nop 4
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @license MIT
7
 * @copyright  2013 - 2016 Cross Solution <http://cross-solution.de>
8
 */
9
  
10
/** */
11
namespace Core\Repository\DoctrineMongoODM\Event;
12
13
use Core\Entity\PermissionsAwareInterface;
14
use Doctrine\Common\Collections\Collection;
15
use Doctrine\Common\EventSubscriber;
16
use Doctrine\ODM\MongoDB\DocumentManager;
17
use Doctrine\ODM\MongoDB\Event\OnFlushEventArgs;
18
use Doctrine\ODM\MongoDB\Events;
19
use Doctrine\ODM\MongoDB\UnitOfWork;
20
use Zend\Stdlib\ArrayUtils;
21
22
/**
23
 * Boilerplate for Files permissions update subscriber.
24
 *
25
 * updates the permissions of embedded files if the permissions on the parent document
26
 * had changed.
27
 *
28
 * @author Mathias Gelhausen <[email protected]>
29
 * @author Miroslav Fedeleš <[email protected]>
30
 * @since 0.26
31
 */
32
abstract class AbstractUpdateFilesPermissionsSubscriber implements EventSubscriber
33
{
34
    /**
35
     * List of properties where embedded files can be retreived.
36
     *
37
     * Note: entries will be prepended with "get"
38
     *
39
     * @var string[]
40
     */
41
    protected $filesProperties = [];
42
43
    /**
44
     * The document class that should be watched.
45
     *
46
     * @var string
47
     */
48
    protected $targetDocument;
49
50
51
    public function getSubscribedEvents()
52
    {
53
        return [ Events::onFlush ];
54
    }
55
56
    /**
57
     * Updates permissions on embedded files.
58
     *
59
     * @param OnFlushEventArgs $args
60
     */
61
    public function onFlush(OnFlushEventArgs $args)
62
    {
63
        $dm = $args->getDocumentManager();
64
        $uow = $dm->getUnitOfWork();
65
66
        $filter = function($element) {
67
            return $element instanceOf $this->targetDocument
68
                   && $element instanceOf PermissionsAwareInterface
69
                   && $element->getPermissions()->hasChanged();
70
        };
71
72
        $inserts = array_filter($uow->getScheduledDocumentInsertions(), $filter);
73
        $updates = array_filter($uow->getScheduledDocumentUpdates(), $filter);
74
75
        $this->process($inserts, $dm, $uow, true);
76
        $this->process($updates, $dm, $uow);
77
    }
78
79
    /**
80
     * @param PermissionsAwareInterface[] $documents
81
     * @param DocumentManager $dm
82
     * @param UnitOfWork $uow
83
     * @param bool $insert
84
     */
85
    protected function process($documents, DocumentManager $dm, UnitOfWork $uow, $insert = false)
86
    {
87
        foreach ($documents as $document) {
88
            $perms = $document->getPermissions();
89
            $files = $this->getFiles($document);
0 ignored issues
show
Documentation introduced by
$document is of type object<Core\Entity\PermissionsAwareInterface>, but the function expects a object<Core\Entity\EntityInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
90
91
            foreach ($files as $file) {
92
                $filePermissions = $file
93
                    ->getPermissions()
94
                    ->clear()
95
                    ->inherit($perms);
96
97
                if ($insert) {
98
                    $dm->persist($filePermissions);
99
                }
100
                
101
                $uow->computeChangeSet(
102
                    $dm->getClassMetadata(get_class($file)),
103
                    $file
104
                );
105
            }
106
        }
107
    }
108
109
    /**
110
     *
111
     *
112
     * @param \Core\Entity\EntityInterface $document
113
     *
114
     * @return PermissionsAwareInterface[]
115
     */
116
    protected function getFiles($document)
117
    {
118
        $files = [];
119
120
        foreach ($this->filesProperties as $prop) {
121
            $getter = "get$prop";
122
            $file   = $document->$getter();
123
            if ($file instanceof Collection) {
124
                $files = array_merge($files, ArrayUtils::iteratorToArray($file));
125
126
            } else {
127
                $files[] = $file;
128
            }
129
        }
130
131
        return array_filter($files, function($i) { return $i instanceOf PermissionsAwareInterface; });
132
    }
133
}