UserAttributesTrait   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 15
c 1
b 0
f 0
dl 0
loc 47
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getAttributeValue() 0 3 2
A getAttributes() 0 3 1
A hasAttribute() 0 3 1
A setAttribute() 0 9 2
A delAttribute() 0 7 2
A setAttributes() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula - https://ziku.la/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Zikula\UsersBundle\Entity;
15
16
use Doctrine\Common\Collections\ArrayCollection;
17
use Doctrine\Common\Collections\Collection;
18
19
trait UserAttributesTrait
20
{
21
    private Collection $attributes;
22
23
    public function getAttributes(): Collection
24
    {
25
        return $this->attributes;
26
    }
27
28
    public function getAttributeValue(string $name): string
29
    {
30
        return $this->getAttributes()->offsetExists($name) ? $this->getAttributes()->get($name)->getValue() : '';
31
    }
32
33
    public function setAttributes(ArrayCollection $attributes): self
34
    {
35
        $this->attributes = $attributes;
36
37
        return $this;
38
    }
39
40
    /**
41
     * @param mixed $value
42
     */
43
    public function setAttribute(string $name, $value): self
44
    {
45
        if (isset($this->attributes[$name])) {
46
            $this->attributes[$name]->setValue($value);
47
        } else {
48
            $this->attributes[$name] = new UserAttribute($this, $name, $value);
49
        }
50
51
        return $this;
52
    }
53
54
    public function delAttribute(string $name): self
55
    {
56
        if (isset($this->attributes[$name])) {
57
            $this->attributes->remove($name);
58
        }
59
60
        return $this;
61
    }
62
63
    public function hasAttribute(string $name): bool
64
    {
65
        return $this->attributes->containsKey($name);
66
    }
67
}
68