Passed
Pull Request — master (#631)
by ANTHONIUS
08:16
created

FileMetadataTrait   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 12
eloc 26
dl 0
loc 98
rs 10
c 1
b 0
f 1

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getUser() 0 3 1
A setUser() 0 9 2
A getName() 0 3 1
A getPermissions() 0 10 3
A getContentType() 0 3 1
A setPermissions() 0 4 1
A setContentType() 0 4 1
A setName() 0 4 1
A getResourceId() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Core\Entity;
6
7
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\EmbedOne(targetDocument="Core\Entity\Permissions")
27
     */
28
    protected ?PermissionsInterface $permissions = null;
29
30
    /**
31
     * @ODM\Field(type="string", nullable=true)
32
     */
33
    protected ?string $name = null;
34
35
    /**
36
     * @return string
37
     */
38
    public function getResourceId()
39
    {
40
        return 'Entity/File';
41
    }
42
43
    /**
44
     * @return string|null
45
     */
46
    public function getContentType(): ?string
47
    {
48
        return $this->contentType;
49
    }
50
51
    public function setContentType(?string $contentType)
52
    {
53
        $this->contentType = $contentType;
54
        return $this;
55
    }
56
57
    /**
58
     * @return UserInterface|null
59
     */
60
    public function getUser(): ?UserInterface
61
    {
62
        return $this->user;
63
    }
64
65
    public function setUser(UserInterface $user)
66
    {
67
        if ($this->user) {
68
            $this->getPermissions()->revoke($this->user, Permissions::PERMISSION_ALL, false);
69
        }
70
        $this->user = $user;
71
        $this->getPermissions()->grant($user, Permissions::PERMISSION_ALL);
72
73
        return $this;
74
    }
75
76
    /**
77
     * @return PermissionsInterface|null
78
     */
79
    public function getPermissions(): ?PermissionsInterface
80
    {
81
        if (!$this->permissions) {
82
            $perms = new Permissions();
83
            if ($this->user instanceof UserInterface) {
84
                $perms->grant($this->user, PermissionsInterface::PERMISSION_ALL);
85
            }
86
            $this->setPermissions($perms);
87
        }
88
        return $this->permissions;
89
    }
90
91
    public function setPermissions(PermissionsInterface $permissions)
92
    {
93
        $this->permissions = $permissions;
94
        return $this;
95
    }
96
97
    /**
98
     * @return string|null
99
     */
100
    public function getName(): ?string
101
    {
102
        return $this->name;
103
    }
104
105
    public function setName(?string $name)
106
    {
107
        $this->name = $name;
108
        return $this;
109
    }
110
}