repository = $repository; $this->generator = $generator; } public function getSecretForUser(User $user) { $secret = $this->repository->findOneBy(['creator' => $user], ['created' => 'DESC']); if ($secret) { return $secret; } $token = $this->generator->generateString(64, Generator::CHAR_ALNUM | Generator::CHAR_SYMBOLS); $secret = new Secret($user, $token); $this->repository->save($secret); return $secret; } public function offsetExists($offset) { return null !== $this->repository->find($offset); } public function offsetGet($offset) { $secret = $this->repository->find($offset); if (!$secret instanceof Secret) { throw new \RuntimeException('Undefined index: ' . $offset); } return $secret->getToken(); } public function offsetSet($offset, $value) { throw new \LogicException('This ArrayAccess is non mutable.'); } public function offsetUnset($offset) { throw new \LogicException('This ArrayAccess is non mutable.'); } }