Repository navigation
Expand file tree
/
Copy pathAbstractCredential.php
More file actions
53 lines (41 loc) · 1.83 KB
/
Copy pathAbstractCredential.php
File metadata and controls
53 lines (41 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
declare(strict_types=1);
namespace MsgPhp\User\Entity\Features;
use MsgPhp\User\CredentialInterface;
use MsgPhp\User\Event\Domain\ChangeCredentialEvent;
/**
* @author Roland Franssen <franssen.roland@gmail.com>
*
* @internal
*/
trait AbstractCredential
{
/** @var CredentialInterface */
private $credential;
public function getCredential(): CredentialInterface
{
return $this->credential;
}
private function handleChangeCredentialEvent(ChangeCredentialEvent $event): bool
{
$handled = false;
foreach ($event->fields as $field => $value) {
$readIsProp = $writeIsProp = false;
if (!method_exists($this, $writeMethod = 'change'.($ucField = ucfirst($field))) && !method_exists($this, $writeMethod = 'set'.$ucField) && !($writeIsProp = property_exists($this, $field))) {
throw new \LogicException(sprintf('Unable to handle event "%s", method "%s::change/set%s()" nor property "$%s" does not exists.', \get_class($event), \get_class($this), $ucField, $field));
}
if (!method_exists($this, $readMethod = 'get'.$ucField) && !method_exists($this, $readMethod = 'is'.$ucField) && !method_exists($this, $readMethod = 'has'.$ucField) && !($readIsProp = property_exists($this, $field))) {
throw new \LogicException(sprintf('Unable to handle event "%s", method "%s::get/is/has%s()" nor property "$%s" does not exists.', \get_class($event), \get_class($this), $ucField, $field));
}
if ($value !== ($readIsProp ? $this->$field : $this->$readMethod())) {
if ($writeIsProp) {
$this->$field = $value;
} else {
$this->$writeMethod($value);
}
$handled = true;
}
}
return $handled;
}
}