Skip to content

Commit c2cdf5e

Browse files
committed
chore(refactor): register Context Factory instead of callable
Signed-off-by: Max <max@nextcloud.com>
1 parent 7e251bc commit c2cdf5e

2 files changed

Lines changed: 20 additions & 27 deletions

File tree

lib/Context/ContextManager.php

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,19 @@
1010
use OCA\Text\Event\RegisterContextEvent;
1111
use OCP\EventDispatcher\IEventDispatcher;
1212
use OCP\Files\NotFoundException;
13+
use OCP\Files\NotPermittedException;
14+
use OCP\IUserSession;
1315
use Psr\Log\LoggerInterface;
16+
use Psr\Container\ContainerInterface;
1417

1518
class ContextManager {
1619
/** @var array<string, callable> */
1720
private array $contexts = [];
1821
public function __construct(
22+
private readonly ContainerInterface $c,
1923
private readonly IEventDispatcher $eventDispatcher,
2024
private readonly LoggerInterface $logger,
25+
private readonly IUserSession $userSession,
2126
) {
2227
}
2328

@@ -33,24 +38,30 @@ private function getContexts(): array {
3338
return $this->contexts;
3439
}
3540

36-
public function registerContext(string $type, callable $createContext): void {
41+
public function registerContext(string $type, string $factoryClassName): void {
3742
$this->logger->debug('Registering context for type "' . $type . '".');
3843
if (array_key_exists($type, $this->contexts)) {
3944
$this->logger->warning('Context of type "' . $type . '" was already registered!');
4045
return;
4146
}
42-
$this->contexts[$type] = $createContext;
47+
$this->contexts[$type] = $factoryClassName;
4348
}
4449

4550
public function getContext(string $type, int $id, ?string $shareToken): IContext {
46-
$createContext = $this->getContexts()[$type];
47-
if (!is_callable($createContext)) {
51+
$factoryClassName = $this->getContexts()[$type];
52+
if ($factoryClassName === null) {
4853
throw new NotFoundException('Context of type "' . $type . '" was not registered!');
4954
}
50-
$context = $createContext($id, $type, $shareToken);
51-
if (!$context instanceof IContext) {
52-
throw new NotFoundException('Failed to create context of type ' . $type . '!');
55+
$factory = $this->c->get($factoryClassName);
56+
57+
if ($shareToken === null) {
58+
$user = $this->userSession->getUser();
59+
if ($user === null) {
60+
throw new NotPermittedException();
61+
}
62+
return $factory->buildForUser($user, $id);
63+
} else {
64+
return $factory->buildForShare($shareToken, $id);
5365
}
54-
return $context;
5566
}
5667
}

lib/Listeners/RegisterContextEventListener.php

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,11 @@
1212
use OCA\Text\Event\RegisterContextEvent;
1313
use OCP\EventDispatcher\Event;
1414
use OCP\EventDispatcher\IEventListener;
15-
use OCP\Files\NotPermittedException;
16-
use OCP\IUserSession;
1715
use Override;
1816

1917
/** @implements IEventListener<Event|RegisterContextEvent> */
2018
class RegisterContextEventListener implements IEventListener {
2119

22-
public function __construct(
23-
private readonly FileContextFactory $fileContextFactory,
24-
private readonly IUserSession $userSession,
25-
) {
26-
}
27-
2820
#[Override]
2921
public function handle(Event $event): void {
3022
if (!$event instanceof RegisterContextEvent) {
@@ -33,17 +25,7 @@ public function handle(Event $event): void {
3325

3426
$event->getContextManager()->registerContext(
3527
'file',
36-
function (int $id, string $type, ?string $shareToken) {
37-
if ($shareToken === null) {
38-
$user = $this->userSession->getUser();
39-
if ($user === null) {
40-
throw new NotPermittedException();
41-
}
42-
return $this->fileContextFactory->buildForUser($user, $id);
43-
} else {
44-
return $this->fileContextFactory->buildForShare($shareToken, $id);
45-
}
46-
}
28+
FileContextFactory::class,
4729
);
4830
}
4931
}

0 commit comments

Comments
 (0)