Completed
Push — master ( ceb81d...34dbeb )
by Beñat
07:47
created

FullName::firstName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Kreta package.
5
 *
6
 * (c) Beñat Espiña <[email protected]>
7
 * (c) Gorka Laucirica <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
declare(strict_types=1);
14
15
namespace Kreta\IdentityAccess\Domain\Model\User;
16
17
class FullName
18
{
19
    private $firstName;
20
    private $lastName;
21
22
    public function __construct(string $firstName, string $lastName)
23
    {
24
        $this->setFirstName($firstName);
25
        $this->setLastName($lastName);
26
    }
27
28
    private function setFirstName(string $firstName)
29
    {
30
        $this->checkEmptyFirstName($firstName);
31
        $this->firstName = $firstName;
32
    }
33
34
    private function checkEmptyFirstName(string $firstName)
35
    {
36
        if ('' === $firstName) {
37
            throw new FirstNameEmptyException();
38
        }
39
    }
40
41
    private function setLastName(string $lastName)
42
    {
43
        $this->lastName = $lastName;
44
    }
45
46
    public function firstName() : string
47
    {
48
        // This ternary is a hack that avoids the
49
        // DoctrineORM limitation with nullable embeddables
50
        return null === $this->firstName ? '' : $this->firstName;
51
    }
52
53
    public function lastName() : string
54
    {
55
        // This ternary is a hack that avoids the
56
        // DoctrineORM limitation with nullable embeddables
57
        return null === $this->lastName ? '' : $this->lastName;
58
    }
59
60
    public function fullName() : string
61
    {
62
        if (!$this->lastName()) {
63
            return $this->firstName();
64
        }
65
66
        return $this->firstName() . ' ' . $this->lastName();
67
    }
68
69
    public function __toString() : string
70
    {
71
        return (string) $this->fullName();
72
    }
73
}
74