Completed
Push — develop ( e13576...00026f )
by
unknown
16:30 queued 08:33
created

process()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 13

Duplication

Lines 13
Ratio 61.9 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 13
loc 21
rs 9.0534
c 1
b 0
f 1
cc 4
eloc 13
nc 4
nop 3
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
 * @since 0.26
30
 */
31
abstract class AbstractUpdateFilesPermissionsSubscriber implements EventSubscriber
32
{
33
    /**
34
     * List of properties where embedded files can be retreived.
35
     *
36
     * Note: entries will be prepended with "get"
37
     *
38
     * @var string[]
39
     */
40
    protected $filesProperties = [];
41
42
    /**
43
     * The document class that should be watched.
44
     *
45
     * @var string
46
     */
47
    protected $targetDocument;
48
49
50
    public function getSubscribedEvents()
51
    {
52
        return [ Events::onFlush ];
53
    }
54
55
    /**
56
     * Updates permissions on embedded files.
57
     *
58
     * @param OnFlushEventArgs $args
59
     */
60
    public function onFlush(OnFlushEventArgs $args)
61
    {
62
        $dm = $args->getDocumentManager();
63
        $uow = $dm->getUnitOfWork();
64
65
        $filter = function($element) {
66
            return $element instanceOf $this->targetDocument
67
                   && $element instanceOf PermissionsAwareInterface
68
                   && $element->getPermissions()->hasChanged();
69
        };
70
71
        $inserts = array_filter($uow->getScheduledDocumentInsertions(), $filter);
72
        $updates = array_filter($uow->getScheduledDocumentUpdates(), $filter);
73
74
        $this->process($inserts);
75
        $this->process($updates, $dm, $uow);
76
    }
77
78
    /**
79
     *
80
     *
81
     * @param PermissionsAwareInterface[] $documents
82
     * @param null|DocumentManager $dm
83
     * @param null|UnitOfWork $uow
84
     */
85
    protected function process($documents, $dm = null, $uow = null)
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 View Code Duplication
            foreach ($files as $file) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
92
                $file
93
                    ->getPermissions()
94
                    ->clear()
95
                    ->inherit($perms);
96
97
                if ($dm) {
98
                    $uow->computeChangeSet(
0 ignored issues
show
Bug introduced by
It seems like $uow is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
99
                        $dm->getClassMetadata(get_class($file)),
100
                        $file
101
                    );
102
                }
103
            }
104
        }
105
    }
106
107
    /**
108
     *
109
     *
110
     * @param \Core\Entity\EntityInterface $document
111
     *
112
     * @return PermissionsAwareInterface[]
113
     */
114
    protected function getFiles($document)
115
    {
116
        $files = [];
117
118
        foreach ($this->filesProperties as $prop) {
119
            $getter = "get$prop";
120
            $file   = $document->$getter();
121
            if ($file instanceof Collection) {
122
                $files = array_merge($files, ArrayUtils::iteratorToArray($file));
123
124
            } else {
125
                $files[] = $file;
126
            }
127
        }
128
129
        return array_filter($files, function($i) { return $i instanceOf PermissionsAwareInterface; });
130
    }
131
}