Passed
Push — master ( 432fe2...c3ceb1 )
by Misha
41s queued 10s
created

Email::__clone()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 6
rs 10
cc 2
nc 2
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Communibase\Entity;
6
7
use Communibase\DataBag;
8
9
/**
10
 * Communibase E-mail
11
 *
12
 * @author Kingsquare ([email protected])
13
 * @copyright Copyright (c) Kingsquare BV (http://www.kingsquare.nl)
14
 */
15
class Email
16
{
17
    /**
18
     * @var DataBag
19
     */
20
    protected $dataBag;
21
22
    protected function __construct(array $emailAddressData = [])
23
    {
24
        if (empty($emailAddressData['type'])) {
25
            $emailAddressData['type'] = 'private';
26
        }
27
        $this->dataBag = DataBag::create();
28
        $this->dataBag->addEntityData('email', $emailAddressData);
29
    }
30
31
    /**
32
     * @return static
33
     */
34
    public static function create()
35
    {
36
        return new static();
37
    }
38
39
    /**
40
     * @return static
41
     */
42
    public static function fromEmailAddressData(array $emailAddressData = null)
43
    {
44
        if ($emailAddressData === null) {
45
            $emailAddressData = [];
46
        }
47
        return new static($emailAddressData);
48
    }
49
50
    /**
51
     * @return static
52
     */
53
    public static function fromEmailAddress(string $emailAddress)
54
    {
55
        return static::fromEmailAddressData([
56
            'emailAddress' => $emailAddress,
57
        ]);
58
    }
59
60
    public function getEmailAddress(): string
61
    {
62
        return (string)$this->dataBag->get('email.emailAddress');
63
    }
64
65
    public function setEmailAddress(string $emailAddress): void
66
    {
67
        $this->dataBag->set('email.emailAddress', $emailAddress);
68
    }
69
70
    public function getType(): string
71
    {
72
        return (string)$this->dataBag->get('email.type');
73
    }
74
75
    public function setType(string $type): void
76
    {
77
        $this->dataBag->set('email.type', $type);
78
    }
79
80
    public function __clone()
81
    {
82
        $state = $this->getState();
83
        if ($state !== null) {
84
            unset($state['_id']);
85
            $this->dataBag->addEntityData('email', $state);
86
        }
87
    }
88
89
    public function getState(): ?array
90
    {
91
        if (empty($this->getEmailAddress())) {
92
            return null;
93
        }
94
        return $this->dataBag->getState('email');
95
    }
96
}
97