Completed
Pull Request — master (#1)
by
unknown
13:02
created

UserObject::setData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
namespace Sulu\Bundle\ElasticsearchActivityLogBundle\Document;
4
5
use Ferrandini\Urlizer;
6
use ONGR\ElasticsearchBundle\Annotation\Object;
7
use ONGR\ElasticsearchBundle\Annotation\Property;
8
use Sulu\Component\Security\Authentication\UserInterface;
9
10
/**
11
 * @Object
12
 */
13
class UserObject
14
{
15
    /**
16
     * @var int
17
     *
18
     * @Property(type="integer")
19
     */
20
    public $id;
21
22
    /**
23
     * @var string
24
     *
25
     * @Property(
26
     *     type="string",
27
     *     options={
28
     *         "fields":{
29
     *            "raw":{"type":"string", "index":"not_analyzed"},
30
     *            "folded":{"type":"string", "analyzer":"folding"},
31
     *            "value":{"type":"string"}
32
     *         }
33
     *     }
34
     * )
35
     */
36
    public $username;
37
38
    /**
39
     * Set data.
40
     *
41
     * @param UserInterface $user
42
     *
43
     * @return $this
44
     */
45
    public function setData(UserInterface $user)
46
    {
47
        $this->id = $user->getId();
48
        $this->username = $user->getUsername();
49
50
        return $this;
51
    }
52
53
    /**
54
     * Return username slug.
55
     *
56
     * @param string $fallback
57
     *
58
     * @return string
59
     */
60
    public function getSlug($fallback = 'user')
61
    {
62
        return Urlizer::urlize($this->username) ?: $fallback;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function __toString()
69
    {
70
        return $this->username;
71
    }
72
}
73