Skip to content

Commit 69d901e

Browse files
committed
refactor(dav): inject request and logger services
Signed-off-by: Jaggob <37583151+Jaggob@users.noreply.github.com>
1 parent b08b0da commit 69d901e

File tree

5 files changed

+39
-20
lines changed

5 files changed

+39
-20
lines changed

lib/DAV/Calendar.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use OCA\Deck\Db\Board;
1313
use OCA\Deck\Db\Card;
1414
use OCA\Deck\Db\Stack;
15+
use OCP\IRequest;
1516
use Sabre\CalDAV\CalendarQueryValidator;
1617
use Sabre\CalDAV\Xml\Property\SupportedCalendarComponentSet;
1718
use Sabre\DAV\Exception\Forbidden;
@@ -32,13 +33,16 @@ class Calendar extends ExternalCalendar {
3233
private $board;
3334
/** @var Stack|null */
3435
private $stack;
36+
/** @var IRequest|null */
37+
private $request;
3538

36-
public function __construct(string $principalUri, string $calendarUri, Board $board, DeckCalendarBackend $backend, ?Stack $stack = null) {
39+
public function __construct(string $principalUri, string $calendarUri, Board $board, DeckCalendarBackend $backend, ?Stack $stack = null, ?IRequest $request = null) {
3740
parent::__construct('deck', $calendarUri);
3841

3942
$this->backend = $backend;
4043
$this->board = $board;
4144
$this->stack = $stack;
45+
$this->request = $request;
4246

4347
$this->principalUri = $principalUri;
4448
}
@@ -432,27 +436,25 @@ private function buildPlaceholderCalendarObject(string $name) {
432436
}
433437

434438
private function shouldUsePlaceholderForMissingObject(): bool {
435-
if (!class_exists('\OC')) {
439+
if ($this->request === null) {
436440
return false;
437441
}
438442

439443
try {
440-
$request = \OC::$server->getRequest();
441-
$method = strtoupper((string)$request->getMethod());
444+
$method = strtoupper((string)$this->request->getMethod());
442445
return in_array($method, ['GET', 'HEAD', 'REPORT', 'PROPFIND'], true);
443446
} catch (\Throwable $e) {
444447
return false;
445448
}
446449
}
447450

448451
private function isDirectObjectReadRequest(): bool {
449-
if (!class_exists('\OC')) {
452+
if ($this->request === null) {
450453
return false;
451454
}
452455

453456
try {
454-
$request = \OC::$server->getRequest();
455-
$method = strtoupper((string)$request->getMethod());
457+
$method = strtoupper((string)$this->request->getMethod());
456458
return in_array($method, ['GET', 'HEAD'], true);
457459
} catch (\Throwable $e) {
458460
return false;

lib/DAV/CalendarPlugin.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use OCA\DAV\CalDAV\Integration\ICalendarProvider;
1212
use OCA\Deck\Db\Board;
1313
use OCA\Deck\Service\ConfigService;
14+
use OCP\IRequest;
1415
use Sabre\DAV\Exception\NotFound;
1516

1617
class CalendarPlugin implements ICalendarProvider {
@@ -21,10 +22,13 @@ class CalendarPlugin implements ICalendarProvider {
2122
private $configService;
2223
/** @var bool */
2324
private $calendarIntegrationEnabled;
25+
/** @var IRequest */
26+
private $request;
2427

25-
public function __construct(DeckCalendarBackend $backend, ConfigService $configService) {
28+
public function __construct(DeckCalendarBackend $backend, ConfigService $configService, IRequest $request) {
2629
$this->backend = $backend;
2730
$this->configService = $configService;
31+
$this->request = $request;
2832
$this->calendarIntegrationEnabled = $configService->get('calendar');
2933
}
3034

@@ -43,14 +47,14 @@ public function fetchAllForCalendarHome(string $principalUri): array {
4347
$calendars = [];
4448
foreach ($boards as $board) {
4549
foreach ($this->backend->getStacks($board->getId()) as $stack) {
46-
$calendars[] = new Calendar($principalUri, 'stack-' . $stack->getId(), $board, $this->backend, $stack);
50+
$calendars[] = new Calendar($principalUri, 'stack-' . $stack->getId(), $board, $this->backend, $stack, $this->request);
4751
}
4852
}
4953
return $calendars;
5054
}
5155

5256
return array_map(function (Board $board) use ($principalUri) {
53-
return new Calendar($principalUri, 'board-' . $board->getId(), $board, $this->backend);
57+
return new Calendar($principalUri, 'board-' . $board->getId(), $board, $this->backend, null, $this->request);
5458
}, $boards);
5559
}
5660

@@ -96,7 +100,7 @@ private function resolveCalendar(string $principalUri, string $calendarUri): ?Ex
96100
if ($board === null) {
97101
return null;
98102
}
99-
return new Calendar($principalUri, $normalizedCalendarUri, $board, $this->backend, $stack);
103+
return new Calendar($principalUri, $normalizedCalendarUri, $board, $this->backend, $stack, $this->request);
100104
}
101105

102106
if (!$perListMode && str_starts_with($normalizedCalendarUri, 'board-')) {
@@ -105,7 +109,7 @@ private function resolveCalendar(string $principalUri, string $calendarUri): ?Ex
105109
if ($board === null) {
106110
return null;
107111
}
108-
return new Calendar($principalUri, $normalizedCalendarUri, $board, $this->backend);
112+
return new Calendar($principalUri, $normalizedCalendarUri, $board, $this->backend, null, $this->request);
109113
}
110114
} catch (NotFound $e) {
111115
// We can just return null if we have no matching board/stack

lib/DAV/DeckCalendarBackend.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,12 @@ class DeckCalendarBackend {
4545
private $labelService;
4646
/** @var ConfigService */
4747
private $configService;
48+
/** @var LoggerInterface */
49+
private $logger;
4850

4951
public function __construct(
5052
BoardService $boardService, StackService $stackService, CardService $cardService, PermissionService $permissionService,
51-
BoardMapper $boardMapper, LabelService $labelService, ConfigService $configService,
53+
BoardMapper $boardMapper, LabelService $labelService, ConfigService $configService, LoggerInterface $logger,
5254
) {
5355
$this->boardService = $boardService;
5456
$this->stackService = $stackService;
@@ -57,6 +59,7 @@ public function __construct(
5759
$this->boardMapper = $boardMapper;
5860
$this->labelService = $labelService;
5961
$this->configService = $configService;
62+
$this->logger = $logger;
6063
}
6164

6265
public function getBoards(): array {
@@ -783,7 +786,7 @@ private function createLabelForCategory(int $boardId, string $title): ?\OCA\Deck
783786
return $this->labelService->create($title, '31CC7C', $boardId);
784787
} catch (\Throwable $e) {
785788
try {
786-
\OCP\Server::get(LoggerInterface::class)->debug('[deck-caldav] label-create-failed', [
789+
$this->logger->debug('[deck-caldav] label-create-failed', [
787790
'boardId' => $boardId,
788791
'title' => $title,
789792
'error' => $e->getMessage(),

tests/unit/DAV/CalendarPluginTest.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@
1010
use OCA\Deck\Db\Board;
1111
use OCA\Deck\Db\Stack;
1212
use OCA\Deck\Service\ConfigService;
13+
use OCP\IRequest;
1314
use Test\TestCase;
1415

1516
class CalendarPluginTest extends TestCase {
1617

1718
public function testHasCalendarInCalendarHomeNormalizesAppGeneratedBoardUri(): void {
1819
$backend = $this->createMock(DeckCalendarBackend::class);
1920
$configService = $this->createMock(ConfigService::class);
21+
$request = $this->createMock(IRequest::class);
2022

2123
$configService->method('get')->with('calendar')->willReturn(true);
2224
$configService->method('getCalDavListMode')->willReturn(ConfigService::SETTING_CALDAV_LIST_MODE_ROOT_TASKS);
@@ -29,7 +31,7 @@ public function testHasCalendarInCalendarHomeNormalizesAppGeneratedBoardUri(): v
2931
->method('getBoards')
3032
->willReturn([$board]);
3133

32-
$plugin = new CalendarPlugin($backend, $configService);
34+
$plugin = new CalendarPlugin($backend, $configService, $request);
3335

3436
$this->assertTrue(
3537
$plugin->hasCalendarInCalendarHome('principals/users/admin', 'app-generated--deck--board-2')
@@ -39,6 +41,7 @@ public function testHasCalendarInCalendarHomeNormalizesAppGeneratedBoardUri(): v
3941
public function testGetCalendarInCalendarHomeNormalizesAppGeneratedStackUri(): void {
4042
$backend = $this->createMock(DeckCalendarBackend::class);
4143
$configService = $this->createMock(ConfigService::class);
44+
$request = $this->createMock(IRequest::class);
4245

4346
$configService->method('get')->with('calendar')->willReturn(true);
4447
$configService->method('getCalDavListMode')->willReturn(ConfigService::SETTING_CALDAV_LIST_MODE_PER_LIST_CALENDAR);
@@ -61,7 +64,7 @@ public function testGetCalendarInCalendarHomeNormalizesAppGeneratedStackUri(): v
6164
->method('getBoards')
6265
->willReturn([$board]);
6366

64-
$plugin = new CalendarPlugin($backend, $configService);
67+
$plugin = new CalendarPlugin($backend, $configService, $request);
6568

6669
$calendar = $plugin->getCalendarInCalendarHome('principals/users/admin', 'app-generated--deck--stack-5');
6770

@@ -71,6 +74,7 @@ public function testGetCalendarInCalendarHomeNormalizesAppGeneratedStackUri(): v
7174
public function testHasCalendarInCalendarHomeReturnsFalseForDisabledBoardUri(): void {
7275
$backend = $this->createMock(DeckCalendarBackend::class);
7376
$configService = $this->createMock(ConfigService::class);
77+
$request = $this->createMock(IRequest::class);
7478

7579
$configService->method('get')->with('calendar')->willReturn(true);
7680
$configService->method('getCalDavListMode')->willReturn(ConfigService::SETTING_CALDAV_LIST_MODE_ROOT_TASKS);
@@ -83,7 +87,7 @@ public function testHasCalendarInCalendarHomeReturnsFalseForDisabledBoardUri():
8387
->method('getBoards')
8488
->willReturn([$board]);
8589

86-
$plugin = new CalendarPlugin($backend, $configService);
90+
$plugin = new CalendarPlugin($backend, $configService, $request);
8791

8892
$this->assertFalse(
8993
$plugin->hasCalendarInCalendarHome('principals/users/admin', 'app-generated--deck--board-2')
@@ -93,6 +97,7 @@ public function testHasCalendarInCalendarHomeReturnsFalseForDisabledBoardUri():
9397
public function testGetCalendarInCalendarHomeReturnsNullForDisabledStackBoard(): void {
9498
$backend = $this->createMock(DeckCalendarBackend::class);
9599
$configService = $this->createMock(ConfigService::class);
100+
$request = $this->createMock(IRequest::class);
96101

97102
$configService->method('get')->with('calendar')->willReturn(true);
98103
$configService->method('getCalDavListMode')->willReturn(ConfigService::SETTING_CALDAV_LIST_MODE_PER_LIST_CALENDAR);
@@ -113,7 +118,7 @@ public function testGetCalendarInCalendarHomeReturnsNullForDisabledStackBoard():
113118
->with(5)
114119
->willReturn($stack);
115120

116-
$plugin = new CalendarPlugin($backend, $configService);
121+
$plugin = new CalendarPlugin($backend, $configService, $request);
117122

118123
$this->assertNull(
119124
$plugin->getCalendarInCalendarHome('principals/users/admin', 'app-generated--deck--stack-5')

tests/unit/DAV/DeckCalendarBackendTest.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use OCA\Deck\Service\LabelService;
2020
use OCA\Deck\Service\PermissionService;
2121
use OCA\Deck\Service\StackService;
22+
use Psr\Log\LoggerInterface;
2223
use Test\TestCase;
2324

2425
class DeckCalendarBackendTest extends TestCase {
@@ -29,6 +30,7 @@ class DeckCalendarBackendTest extends TestCase {
2930
private BoardMapper $boardMapper;
3031
private LabelService $labelService;
3132
private PermissionService $permissionService;
33+
private LoggerInterface $logger;
3234

3335
public function setUp(): void {
3436
parent::setUp();
@@ -38,6 +40,7 @@ public function setUp(): void {
3840
$this->permissionService = $this->createMock(PermissionService::class);
3941
$this->boardMapper = $this->createMock(BoardMapper::class);
4042
$this->labelService = $this->createMock(LabelService::class);
43+
$this->logger = $this->createMock(LoggerInterface::class);
4144
$configService = $this->createMock(ConfigService::class);
4245
$configService->method('getCalDavListMode')
4346
->willReturn(ConfigService::SETTING_CALDAV_LIST_MODE_ROOT_TASKS);
@@ -49,7 +52,8 @@ public function setUp(): void {
4952
$this->permissionService,
5053
$this->boardMapper,
5154
$this->labelService,
52-
$configService
55+
$configService,
56+
$this->logger
5357
);
5458
}
5559

@@ -325,7 +329,8 @@ public function testCreateCalendarObjectMovesExistingCardToPreferredStackInSameB
325329
$permissionService,
326330
$this->boardMapper,
327331
$this->labelService,
328-
$configService
332+
$configService,
333+
$this->logger
329334
);
330335

331336
$existingCard = new Card();

0 commit comments

Comments
 (0)