Completed
Pull Request — master (#394)
by
unknown
09:40
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 View Code Duplication
    protected function applyRoleRenamed(RoleRenamed $roleRenamed)
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...
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
        $this->repository->save($document->withBody($json));
70
    }
71
72
    /**
73
     * @param ConstraintUpdated $constraintUpdated
74
     */
75 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...
76
        ConstraintUpdated $constraintUpdated
77
    ) {
78
        $document = $this->loadDocumentFromRepositoryByUuid(
79
            $constraintUpdated->getUuid()->toNative()
80
        );
81
82
        $json = $document->getBody();
83
        $json->constraint = $constraintUpdated->getQuery()->toNative();
84
85
        $this->repository->save($document->withBody($json));
86
    }
87
88
    /**
89
     * @param ConstraintRemoved $constraintRemoved
90
     */
91 View Code Duplication
    protected function applyConstraintRemoved(
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...
92
        ConstraintRemoved $constraintRemoved
93
    ) {
94
        $document = $this->loadDocumentFromRepositoryByUuid(
95
            $constraintRemoved->getUuid()->toNative()
96
        );
97
98
        $json = $document->getBody();
99
        unset($json->constraint);
100
101
        $this->repository->save($document->withBody($json));
102
    }
103
104
    /**
105
     * @param PermissionAdded $permissionAdded
106
     */
107
    public function applyPermissionAdded(PermissionAdded $permissionAdded)
108
    {
109
        $document = $this->loadDocumentFromRepositoryByUuid(
110
            $permissionAdded->getUuid()->toNative()
111
        );
112
113
        $permission = $permissionAdded->getPermission();
114
115
        $json = $document->getBody();
116
117
        $permissions = property_exists($json, 'permissions') ? $json->permissions : [];
118
        array_push($permissions, $permission->getName());
119
120
        $json->permissions = array_unique($permissions);
121
122
        $this->repository->save($document->withBody($json));
123
    }
124
125
    /**
126
     * @param PermissionRemoved $permissionRemoved
127
     */
128
    public function applyPermissionRemoved(PermissionRemoved $permissionRemoved)
129
    {
130
        $document = $this->loadDocumentFromRepositoryByUuid(
131
            $permissionRemoved->getUuid()->toNative()
132
        );
133
134
        $permission = $permissionRemoved->getPermission();
135
        $permissionName = $permission->getName();
136
137
        $json = $document->getBody();
138
        $json->permissions = array_values(
139
            array_filter(
140
                $json->permissions,
141
                function ($item) use ($permissionName) {
142
                    return $item !== $permissionName;
143
                }
144
            )
145
        );
146
147
        $this->repository->save($document->withBody($json));
148
    }
149
}
150