Passed
Push — master ( e5b7ab...6d2a74 )
by Mathias
11:01
created

FileMetadataTrait::restoreAnonymousUser()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 5
rs 10
cc 2
nc 2
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Core\Entity;
6
7
use Auth\Entity\AnonymousUser;
8
use Auth\Entity\UserInterface;
9
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
10
11
trait FileMetadataTrait
12
{
13
    /**
14
     * @ODM\Field(type="string", nullable=true)
15
     */
16
    protected ?string $contentType = null;
17
18
    /**
19
     * owner of an attachment. Typically this is the candidate who applies for a job offer.
20
     *
21
     * @ODM\ReferenceOne(targetDocument="Auth\Entity\User", storeAs="id", cascade={"persist"})
22
     */
23
    protected ?UserInterface $user = null;
24
25
    /**
26
     * @ODM\Field(type="string", nullable=true)
27
     */
28
    protected ?string $userToken;
29
30
    protected ?AnonymousUser $anonymousUser;
31
32
    /**
33
     * @ODM\EmbedOne(targetDocument="Core\Entity\Permissions")
34
     */
35
    protected ?PermissionsInterface $permissions = null;
36
37
    /**
38
     * @ODM\Field(type="string", nullable=true)
39
     */
40
    protected ?string $name = null;
41
42
    /**
43
     * @return string
44
     */
45
    public function getResourceId()
46
    {
47
        return 'Entity/File';
48
    }
49
50
    /**
51
     * @return string|null
52
     */
53
    public function getContentType(): ?string
54
    {
55
        return $this->contentType;
56
    }
57
58
    /**
59
     * @param string|null $contentType
60
     * @return $this
61
     */
62
    public function setContentType(?string $contentType)
63
    {
64
        $this->contentType = $contentType;
65
        return $this;
66
    }
67
68
    /**
69
     * @return UserInterface|null
70
     */
71
    public function getUser(): ?UserInterface
72
    {
73
        if (!$this->user && $this->userToken) {
74
            $this->user = new AnonymousUser($this->userToken);
75
        }
76
77
        return $this->user;
78
    }
79
80
    public function setUser(?UserInterface $user): self
81
    {
82
        if ($this->user) {
83
            $this->getPermissions()->revoke($this->user, Permissions::PERMISSION_ALL, false);
84
        }
85
        $this->user = $user;
86
        $this->getPermissions()->grant($user, Permissions::PERMISSION_ALL);
87
88
        if ($user instanceof AnonymousUser) {
89
            $this->userToken = $user->getToken();
90
        } elseif ($this->userToken) {
91
            $this->userToken = null;
92
        }
93
94
        return $this;
95
    }
96
97
    /**
98
     *
99
     * @ODM\PrePersist
100
     * @ODM\PreUpdate
101
     * @ODM\PreFlush
102
     */
103
    public function preventPersistingAnonymousUser(): void
104
    {
105
        if ($this->user instanceof AnonymousUser) {
106
            $this->anonymousUser = $this->user;
107
            $this->user = null;
108
        }
109
    }
110
111
    /**
112
     *
113
     * @ODM\PostPersist
114
     * @ODM\PostUpdate
115
     */
116
    public function restoreAnonymousUser(): void
117
    {
118
        if ($this->anonymousUser) {
119
            $this->user = $this->anonymousUser;
120
            $this->anonymousUser = null;
121
        }
122
    }
123
124
    /**
125
     * @return PermissionsInterface|null
126
     */
127
    public function getPermissions(): ?PermissionsInterface
128
    {
129
        if (!$this->permissions) {
130
            $perms = new Permissions();
131
            $user = $this->getUser();
132
            if ($user instanceof UserInterface) {
0 ignored issues
show
introduced by
$user is always a sub-type of Auth\Entity\UserInterface.
Loading history...
133
                $perms->grant($user, PermissionsInterface::PERMISSION_ALL);
134
            }
135
            $this->setPermissions($perms);
136
        }
137
        return $this->permissions;
138
    }
139
140
    public function setPermissions(PermissionsInterface $permissions)
141
    {
142
        $this->permissions = $permissions;
143
        return $this;
144
    }
145
146
    /**
147
     * @return string|null
148
     */
149
    public function getName(): ?string
150
    {
151
        return $this->name;
152
    }
153
154
    public function setName(?string $name)
155
    {
156
        $this->name = $name;
157
        return $this;
158
    }
159
}