Completed
Pull Request — master (#2)
by Vojtěch
10:05
created

UserStorageProxy::setAuthenticated()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SixtyEightPublishers\User\DoctrineIdentity;
6
7
use Nette;
8
use Doctrine;
9
10
final class UserStorageProxy implements Nette\Security\IUserStorage
11
{
12
	use Nette\SmartObject;
13
14
	/** @var \Nette\Security\IUserStorage  */
15
	private $userStorage;
16
17
	/** @var \Doctrine\ORM\EntityManagerInterface  */
18
	private $em;
19
20
	/**
21
	 * @param \Nette\Security\IUserStorage         $userStorage
22
	 * @param \Doctrine\ORM\EntityManagerInterface $em
23
	 */
24
	public function __construct(Nette\Security\IUserStorage $userStorage, Doctrine\ORM\EntityManagerInterface $em)
25
	{
26
		$this->userStorage = $userStorage;
27
		$this->em = $em;
28
	}
29
30
	/**
31
	 * For compatibility with default implementation Nette\Http\UserStorage
32
	 *
33
	 * @param string $namespace
34
	 *
35
	 * @return \SixtyEightPublishers\User\DoctrineIdentity\UserStorageProxy
36
	 * @throws \SixtyEightPublishers\User\DoctrineIdentity\Exception\UnimplementedMethodException
37
	 */
38
	public function setNamespace(string $namespace): self
39
	{
40
		if (!is_callable([ $this->userStorage, 'setNamespace' ])) {
41
			throw Exception\UnimplementedMethodException::unimplementedMethod(get_class($this->userStorage), 'setNamespace');
42
		}
43
44
		/** @noinspection PhpUndefinedMethodInspection */
45
		$this->userStorage->setNamespace($namespace);
46
47
		return $this;
48
	}
49
50
51
	/**
52
	 * For compatibility with default implementation Nette\Http\UserStorage
53
	 *
54
	 * @return string
55
	 * @throws \SixtyEightPublishers\User\DoctrineIdentity\Exception\UnimplementedMethodException
56
	 */
57
	public function getNamespace(): string
58
	{
59
		if (!is_callable([ $this->userStorage, 'getNamespace' ])) {
60
			throw Exception\UnimplementedMethodException::unimplementedMethod(get_class($this->userStorage), 'getNamespace');
61
		}
62
63
		/** @noinspection PhpUndefinedMethodInspection */
64
		return $this->userStorage->getNamespace();
65
	}
66
67
	/************ interface \Nette\Security\IUserStorage ************/
68
69
	/**
70
	 * {@inheritdoc}
71
	 */
72
	public function setAuthenticated($state): self
73
	{
74
		$this->userStorage->setAuthenticated($state);
75
76
		return $this;
77
	}
78
79
	/**
80
	 * {@inheritdoc}
81
	 */
82
	public function isAuthenticated(): bool
83
	{
84
		return $this->userStorage->isAuthenticated();
85
	}
86
87
	/**
88
	 * {@inheritdoc}
89
	 */
90
	public function setIdentity(?Nette\Security\IIdentity $identity = NULL): self
91
	{
92
		if (NULL !== $identity) {
93
			/** @noinspection PhpDeprecationInspection */
94
			$className = Doctrine\Common\Util\ClassUtils::getClass($identity);
95
			$metadataFactory = $this->em->getMetadataFactory();
96
97
			if ($metadataFactory->hasMetadataFor($className)) {
98
				$identity = new IdentityReference(
99
					$className,
100
					$metadataFactory->getMetadataFor($className)->getIdentifierValues($identity)
101
				);
102
			}
103
		}
104
105
		$this->userStorage->setIdentity($identity);
106
107
		return $this;
108
	}
109
110
	/**
111
	 * {@inheritdoc}
112
	 */
113
	public function getIdentity(): ?Nette\Security\IIdentity
114
	{
115
		$identity = $this->userStorage->getIdentity();
116
117
		if ($identity instanceof IdentityReference) {
118
			$identity = $this->em->getReference($identity->getClassName(), $identity->getId());
119
		}
120
121
		return $identity;
122
	}
123
124
	/**
125
	 * {@inheritdoc}
126
	 */
127
	public function setExpiration($time, $flags = 0): self
128
	{
129
		$this->userStorage->setExpiration($time, $flags);
130
131
		return $this;
132
	}
133
134
	/**
135
	 * {@inheritdoc}
136
	 */
137
	public function getLogoutReason(): ?int
138
	{
139
		return $this->userStorage->getLogoutReason();
140
	}
141
}
142