Skip to content

Commit f59d07c

Browse files
committed
Refactor URL revalidator method in NextSite entity.
1 parent f2c6fd3 commit f59d07c

File tree

5 files changed

+12
-46
lines changed

5 files changed

+12
-46
lines changed

modules/next/src/Entity/NextSite.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -265,22 +265,15 @@ public function getLiveUrlForEntity(EntityInterface $entity): ?Url {
265265
/**
266266
* {@inheritdoc}
267267
*/
268-
public function getRevalidateUrlForPath(string $path): ?Url {
268+
public function buildRevalidateUrl(array $query = []): ?Url {
269269
$revalidate_url = $this->getRevalidateUrl();
270-
271270
if (!$revalidate_url) {
272271
return NULL;
273272
}
274-
275-
$query = [
276-
'path' => $path,
277-
];
278-
279273
if ($secret = $this->getRevalidateSecret()) {
280274
$query['secret'] = $secret;
281275
}
282-
283-
return Url::fromUri($this->getRevalidateUrl(), [
276+
return Url::fromUri($revalidate_url, [
284277
'query' => $query,
285278
]);
286279
}

modules/next/src/Entity/NextSiteInterface.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,12 @@ public function setRevalidateSecret(string $revalidate_secret): self;
131131
/**
132132
* Returns the revalidate url for given path.
133133
*
134-
* @param string $path
135-
* The path.
134+
* @param array $query
135+
* The revalidate URL query parameters.
136136
*
137137
* @return \Drupal\Core\Url|null
138138
* The revalidate url.
139139
*/
140-
public function getRevalidateUrlForPath(string $path): ?Url;
140+
public function buildRevalidateUrl(array $query = []): ?Url;
141141

142142
}

modules/next/src/Plugin/Next/Revalidator/CacheTag.php

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44

55
use Drupal\Core\Entity\FieldableEntityInterface;
66
use Drupal\Core\Form\FormStateInterface;
7-
use Drupal\Core\Url;
87
use Drupal\Core\Utility\Error;
9-
use Drupal\next\Entity\NextSite;
108
use Drupal\next\Event\EntityActionEvent;
119
use Drupal\next\Plugin\ConfigurableRevalidatorBase;
1210
use Drupal\next\Plugin\RevalidatorInterface;
@@ -72,7 +70,7 @@ public function revalidate(EntityActionEvent $event): bool {
7270
foreach ($sites as $site) {
7371
try {
7472
$tags_string = implode(',', $tags);
75-
$revalidate_url = $this->getRevalidateUrlForTags($site, $tags_string);
73+
$revalidate_url = $site->buildRevalidateUrl(['tags' => $tags_string]);
7674

7775
if (!$revalidate_url) {
7876
throw new \Exception('No revalidate url set.');
@@ -110,30 +108,4 @@ public function revalidate(EntityActionEvent $event): bool {
110108
return $revalidated;
111109
}
112110

113-
/**
114-
* Returns the revalidate url for given cache tags.
115-
*
116-
* @param NextSite $site
117-
* @param string $cache_tags
118-
* The cache tags as string.
119-
*
120-
* @return \Drupal\Core\Url|null
121-
* The revalidate url.
122-
*/
123-
protected function getRevalidateUrlForTags(NextSite $site, string $cache_tags): ?Url {
124-
$revalidate_url = $site->getRevalidateUrl();
125-
if (!$revalidate_url) {
126-
return NULL;
127-
}
128-
$query = [
129-
'tags' => $cache_tags,
130-
];
131-
if ($secret = $site->getRevalidateSecret()) {
132-
$query['secret'] = $secret;
133-
}
134-
return Url::fromUri($site->getRevalidateUrl(), [
135-
'query' => $query,
136-
]);
137-
}
138-
139111
}

modules/next/src/Plugin/Next/Revalidator/Path.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,10 @@ public function revalidate(EntityActionEvent $event): bool {
8484
}
8585

8686
foreach ($paths as $path) {
87+
/** @var \Drupal\next\Entity\NextSite $site */
8788
foreach ($sites as $site) {
8889
try {
89-
$revalidate_url = $site->getRevalidateUrlForPath($path);
90+
$revalidate_url = $site->buildRevalidateUrl(['path' => $path]);
9091

9192
if (!$revalidate_url) {
9293
throw new \Exception('No revalidate url set.');

modules/next/tests/src/Kernel/Entity/NextSiteTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public function test() {
117117
}
118118

119119
/**
120-
* @covers ::getRevalidateUrlForPath
120+
* @covers ::buildRevalidateUrl
121121
*/
122122
public function testGetRevalidateUrlForPath() {
123123
$marketing = NextSite::create([
@@ -129,13 +129,13 @@ public function testGetRevalidateUrlForPath() {
129129
]);
130130
$marketing->save();
131131

132-
$this->assertNull($marketing->getRevalidateUrlForPath('/foo'));
132+
$this->assertNull($marketing->buildRevalidateUrl(['path' => '/foo']));
133133

134134
$marketing->setRevalidateUrl('http://example.com/api/revalidate');
135-
$this->assertSame('http://example.com/api/revalidate?path=/foo', $marketing->getRevalidateUrlForPath('/foo')->toString());
135+
$this->assertSame('http://example.com/api/revalidate?path=/foo', $marketing->buildRevalidateUrl(['path' => '/foo'])->toString());
136136

137137
$marketing->setRevalidateSecret('12345');
138-
$this->assertSame('http://example.com/api/revalidate?path=/foo&secret=12345', $marketing->getRevalidateUrlForPath('/foo')->toString());
138+
$this->assertSame('http://example.com/api/revalidate?path=/foo&secret=12345', $marketing->buildRevalidateUrl(['path' => '/foo'])->toString());
139139
}
140140

141141
}

0 commit comments

Comments
 (0)