Skip to content

Commit 0f41e1a

Browse files
committed
IBX-11553: Made response taggers more verbose instead of silently failing
1 parent 80ed217 commit 0f41e1a

4 files changed

Lines changed: 72 additions & 6 deletions

File tree

phpstan-baseline.neon

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,6 +1152,12 @@ parameters:
11521152
count: 1
11531153
path: src/lib/ResponseTagger/Value/ContentInfoTagger.php
11541154

1155+
-
1156+
message: '#^Variable \$logger on left side of \?\? always exists and is not nullable\.$#'
1157+
identifier: nullCoalesce.variable
1158+
count: 1
1159+
path: src/lib/ResponseTagger/Value/ContentInfoTagger.php
1160+
11551161
-
11561162
message: '#^Method Ibexa\\HttpCache\\ResponseTagger\\Value\\LocationTagger\:\:tag\(\) has no return type specified\.$#'
11571163
identifier: missingType.return

src/bundle/Resources/config/view_cache.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,16 @@ services:
8383
Ibexa\HttpCache\ResponseTagger\Value\ContentInfoTagger:
8484
class: Ibexa\HttpCache\ResponseTagger\Value\ContentInfoTagger
8585
parent: ibexa.http_cache.view_cache.response_tagger.abstract_value
86+
arguments:
87+
$debug: '%kernel.debug%'
8688
tags:
8789
- {name: ibexa.cache.http.response.tagger}
8890

8991
Ibexa\HttpCache\ResponseTagger\Value\LocationTagger:
9092
class: Ibexa\HttpCache\ResponseTagger\Value\LocationTagger
9193
parent: ibexa.http_cache.view_cache.response_tagger.abstract_value
94+
arguments:
95+
$debug: '%kernel.debug%'
9296
tags:
9397
- {name: ibexa.cache.http.response.tagger}
9498

src/lib/ResponseTagger/Value/ContentInfoTagger.php

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,57 @@
44
* @copyright Copyright (C) Ibexa AS. All rights reserved.
55
* @license For full copyright and license information view LICENSE file distributed with this source code.
66
*/
7+
declare(strict_types=1);
78

89
namespace Ibexa\HttpCache\ResponseTagger\Value;
910

11+
use FOS\HttpCache\ResponseTagger as FosResponseTagger;
1012
use Ibexa\Contracts\Core\Repository\Values\Content\ContentInfo;
1113
use Ibexa\Contracts\HttpCache\Handler\ContentTagInterface;
14+
use Psr\Log\LoggerInterface;
15+
use Psr\Log\NullLogger;
16+
use RuntimeException;
1217

1318
class ContentInfoTagger extends AbstractValueTagger
1419
{
20+
public function __construct(
21+
FosResponseTagger $responseTagger,
22+
private LoggerInterface $logger = new NullLogger(),
23+
private readonly bool $debug = false
24+
) {
25+
parent::__construct($responseTagger);
26+
27+
$this->logger = $logger ?? new NullLogger();
28+
}
29+
1530
public function tag($value)
1631
{
1732
if (!$value instanceof ContentInfo) {
33+
$message = sprintf(
34+
'%s expects value of type %s, got %s.',
35+
self::class,
36+
ContentInfo::class,
37+
get_debug_type($value),
38+
);
39+
40+
if ($this->debug) {
41+
throw new RuntimeException($message);
42+
}
43+
44+
$this->logger->warning($message);
45+
1846
return $this;
1947
}
2048

2149
$this->responseTagger->addTags([
22-
ContentTagInterface::CONTENT_PREFIX . $value->id,
50+
ContentTagInterface::CONTENT_PREFIX . $value->getId(),
2351
ContentTagInterface::CONTENT_TYPE_PREFIX . $value->contentTypeId,
2452
]);
2553

2654
if ($value->mainLocationId) {
27-
$this->responseTagger->addTags([ContentTagInterface::LOCATION_PREFIX . $value->mainLocationId]);
55+
$this->responseTagger->addTags([
56+
ContentTagInterface::LOCATION_PREFIX . $value->getMainLocationId(),
57+
]);
2858
}
2959
}
3060
}

src/lib/ResponseTagger/Value/LocationTagger.php

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,57 @@
44
* @copyright Copyright (C) Ibexa AS. All rights reserved.
55
* @license For full copyright and license information view LICENSE file distributed with this source code.
66
*/
7+
declare(strict_types=1);
78

89
namespace Ibexa\HttpCache\ResponseTagger\Value;
910

11+
use FOS\HttpCache\ResponseTagger as FosResponseTagger;
1012
use Ibexa\Contracts\Core\Repository\Values\Content\Location;
1113
use Ibexa\Contracts\HttpCache\Handler\ContentTagInterface;
14+
use Psr\Log\LoggerInterface;
15+
use Psr\Log\NullLogger;
16+
use RuntimeException;
1217

1318
class LocationTagger extends AbstractValueTagger
1419
{
20+
public function __construct(
21+
FosResponseTagger $responseTagger,
22+
private LoggerInterface $logger = new NullLogger(),
23+
private readonly bool $debug = false
24+
) {
25+
parent::__construct($responseTagger);
26+
}
27+
1528
public function tag($value)
1629
{
1730
if (!$value instanceof Location) {
31+
$message = sprintf(
32+
'%s expects value of type %s, got %s.',
33+
self::class,
34+
Location::class,
35+
get_debug_type($value),
36+
);
37+
38+
if ($this->debug) {
39+
throw new RuntimeException($message);
40+
}
41+
42+
$this->logger->warning($message);
43+
1844
return $this;
1945
}
2046

21-
if ($value->id !== $value->contentInfo->mainLocationId) {
22-
$this->responseTagger->addTags([ContentTagInterface::LOCATION_PREFIX . $value->id]);
47+
if ($value->id !== $value->getContentInfo()->getMainLocationId()) {
48+
$this->responseTagger->addTags([ContentTagInterface::LOCATION_PREFIX . $value->getId()]);
2349
}
2450

2551
$this->responseTagger->addTags([ContentTagInterface::PARENT_LOCATION_PREFIX . $value->parentLocationId]);
2652
$this->responseTagger->addTags(
2753
array_map(
28-
static function (string $pathItem): string {
54+
static function ($pathItem): string {
2955
return ContentTagInterface::PATH_PREFIX . $pathItem;
3056
},
31-
$value->path
57+
$value->getPath()
3258
)
3359
);
3460
}

0 commit comments

Comments
 (0)