Completed
Push — master ( 70b047...5c79cc )
by Kristof
03:34
created

Projector::applyConstraintAdded()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
cc 2
nc 2
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
    protected function applyConstraintAdded(
60
        ConstraintAdded $constraintAdded
61
    ) {
62
        $document = $this->loadDocumentFromRepositoryByUuid(
63
            $constraintAdded->getUuid()->toNative()
64
        );
65
66
        $json = $document->getBody();
67
68
        if (empty($json->constraints)) {
69
            $json->constraints = new \stdClass();
70
        }
71
        $json->constraints->{$constraintAdded->getSapiVersion()->toNative()} = $constraintAdded->getQuery()->toNative();
72
73
        $this->repository->save($document->withBody($json));
74
    }
75
76
    /**
77
     * @param ConstraintUpdated $constraintUpdated
78
     */
79 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...
80
        ConstraintUpdated $constraintUpdated
81
    ) {
82
        $document = $this->loadDocumentFromRepositoryByUuid(
83
            $constraintUpdated->getUuid()->toNative()
84
        );
85
86
        $json = $document->getBody();
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 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...
96
        ConstraintRemoved $constraintRemoved
97
    ) {
98
        $document = $this->loadDocumentFromRepositoryByUuid(
99
            $constraintRemoved->getUuid()->toNative()
100
        );
101
102
        $json = $document->getBody();
103
        $json->constraints->{$constraintRemoved->getSapiVersion()->toNative()} = null;
104
105
        $this->repository->save($document->withBody($json));
106
    }
107
108
    /**
109
     * @param PermissionAdded $permissionAdded
110
     */
111
    public function applyPermissionAdded(PermissionAdded $permissionAdded)
112
    {
113
        $document = $this->loadDocumentFromRepositoryByUuid(
114
            $permissionAdded->getUuid()->toNative()
115
        );
116
117
        $permission = $permissionAdded->getPermission();
118
119
        $json = $document->getBody();
120
121
        $permissions = property_exists($json, 'permissions') ? $json->permissions : [];
122
        array_push($permissions, $permission->getName());
123
124
        $json->permissions = array_unique($permissions);
125
126
        $this->repository->save($document->withBody($json));
127
    }
128
129
    /**
130
     * @param PermissionRemoved $permissionRemoved
131
     */
132
    public function applyPermissionRemoved(PermissionRemoved $permissionRemoved)
133
    {
134
        $document = $this->loadDocumentFromRepositoryByUuid(
135
            $permissionRemoved->getUuid()->toNative()
136
        );
137
138
        $permission = $permissionRemoved->getPermission();
139
        $permissionName = $permission->getName();
140
141
        $json = $document->getBody();
142
        $json->permissions = array_values(
143
            array_filter(
144
                $json->permissions,
145
                function ($item) use ($permissionName) {
146
                    return $item !== $permissionName;
147
                }
148
            )
149
        );
150
151
        $this->repository->save($document->withBody($json));
152
    }
153
}
154