Completed
Pull Request — master (#394)
by Luc
08:30 queued 01:19
created

Projector::applyConstraintCreated()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
dl 12
loc 12
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace CultuurNet\UDB3\Role\ReadModel\Detail;
4
5
use CultuurNet\UDB3\Role\Events\ConstraintAdded;
6
use CultuurNet\UDB3\Role\Events\ConstraintRemoved;
7
use CultuurNet\UDB3\Role\Events\ConstraintUpdated;
8
use CultuurNet\UDB3\Role\Events\PermissionAdded;
9
use CultuurNet\UDB3\Role\Events\PermissionRemoved;
10
use CultuurNet\UDB3\Role\Events\RoleCreated;
11
use CultuurNet\UDB3\Role\Events\RoleDeleted;
12
use CultuurNet\UDB3\Role\Events\RoleRenamed;
13
use CultuurNet\UDB3\Role\ReadModel\RoleProjector;
14
15
class Projector extends RoleProjector
16
{
17
    /**
18
     * @param RoleCreated $roleCreated
19
     */
20
    protected function applyRoleCreated(RoleCreated $roleCreated)
21
    {
22
        $this->saveNewDocument(
23
            $roleCreated->getUuid()->toNative(),
24
            function (\stdClass $json) use ($roleCreated) {
25
                $json->uuid = $roleCreated->getUuid()->toNative();
26
                $json->name = $roleCreated->getName()->toNative();
27
                $json->permissions = [];
28
                return $json;
29
            }
30
        );
31
    }
32
33
    /**
34
     * @param RoleRenamed $roleRenamed
35
     */
36
    protected function applyRoleRenamed(RoleRenamed $roleRenamed)
37
    {
38
        $document = $this->loadDocumentFromRepositoryByUuid(
39
            $roleRenamed->getUuid()->toNative()
40
        );
41
42
        $json = $document->getBody();
43
        $json->name = $roleRenamed->getName()->toNative();
44
45
        $this->repository->save($document->withBody($json));
46
    }
47
48
    /**
49
     * @param RoleDeleted $roleDeleted
50
     */
51
    protected function applyRoleDeleted(RoleDeleted $roleDeleted)
52
    {
53
        $this->repository->remove($roleDeleted->getUuid());
54
    }
55
56
    /**
57
     * @param ConstraintAdded $constraintAdded
58
     */
59 View Code Duplication
    protected function applyConstraintAdded(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
60
        ConstraintAdded $constraintAdded
61
    ) {
62
        $document = $this->loadDocumentFromRepositoryByUuid(
63
            $constraintAdded->getUuid()->toNative()
64
        );
65
66
        $json = $document->getBody();
67
        $json->constraint = $constraintAdded->getQuery()->toNative();
68
69
        $json->constraints = new \stdClass();
70
        $json->constraints->{$constraintAdded->getSapiVersion()->toNative()} = $constraintAdded->getQuery()->toNative();
71
72
        $this->repository->save($document->withBody($json));
73
    }
74
75
    /**
76
     * @param ConstraintUpdated $constraintUpdated
77
     */
78 View Code Duplication
    protected function applyConstraintUpdated(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
79
        ConstraintUpdated $constraintUpdated
80
    ) {
81
        $document = $this->loadDocumentFromRepositoryByUuid(
82
            $constraintUpdated->getUuid()->toNative()
83
        );
84
85
        $json = $document->getBody();
86
        $json->constraint = $constraintUpdated->getQuery()->toNative();
87
        $json->constraints->{$constraintUpdated->getSapiVersion()->toNative()} = $constraintUpdated->getQuery()->toNative();
88
89
        $this->repository->save($document->withBody($json));
90
    }
91
92
    /**
93
     * @param ConstraintRemoved $constraintRemoved
94
     */
95
    protected function applyConstraintRemoved(
96
        ConstraintRemoved $constraintRemoved
97
    ) {
98
        $document = $this->loadDocumentFromRepositoryByUuid(
99
            $constraintRemoved->getUuid()->toNative()
100
        );
101
102
        $json = $document->getBody();
103
        $json->constraint = null;
104
        $json->constraints->{$constraintRemoved->getSapiVersion()->toNative()} = null;
105
106
        $this->repository->save($document->withBody($json));
107
    }
108
109
    /**
110
     * @param PermissionAdded $permissionAdded
111
     */
112
    public function applyPermissionAdded(PermissionAdded $permissionAdded)
113
    {
114
        $document = $this->loadDocumentFromRepositoryByUuid(
115
            $permissionAdded->getUuid()->toNative()
116
        );
117
118
        $permission = $permissionAdded->getPermission();
119
120
        $json = $document->getBody();
121
122
        $permissions = property_exists($json, 'permissions') ? $json->permissions : [];
123
        array_push($permissions, $permission->getName());
124
125
        $json->permissions = array_unique($permissions);
126
127
        $this->repository->save($document->withBody($json));
128
    }
129
130
    /**
131
     * @param PermissionRemoved $permissionRemoved
132
     */
133
    public function applyPermissionRemoved(PermissionRemoved $permissionRemoved)
134
    {
135
        $document = $this->loadDocumentFromRepositoryByUuid(
136
            $permissionRemoved->getUuid()->toNative()
137
        );
138
139
        $permission = $permissionRemoved->getPermission();
140
        $permissionName = $permission->getName();
141
142
        $json = $document->getBody();
143
        $json->permissions = array_values(
144
            array_filter(
145
                $json->permissions,
146
                function ($item) use ($permissionName) {
147
                    return $item !== $permissionName;
148
                }
149
            )
150
        );
151
152
        $this->repository->save($document->withBody($json));
153
    }
154
}
155