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

FullName   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 57
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 1

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setFirstName() 0 5 1
A checkEmptyFirstName() 0 6 2
A setLastName() 0 4 1
A firstName() 0 6 2
A lastName() 0 6 2
A fullName() 0 8 2
A __toString() 0 4 1
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