diff --git a/src/Cdn/Certificates/Provider/Fastly.php b/src/Cdn/Certificates/Provider/Fastly.php index c55e6f5..3deb7fd 100644 --- a/src/Cdn/Certificates/Provider/Fastly.php +++ b/src/Cdn/Certificates/Provider/Fastly.php @@ -115,7 +115,23 @@ public function getCertificateStatus(string $domain, ?string $domainType): strin public function isRenewRequired(string $domain, ?string $domainType): bool { - return $this->tls->isRenewRequired(Domain::validate($domain), $domainType); + $domain = Domain::validate($domain); + $domainInfo = $this->findDomain($domain); + + if ($domainInfo === null) { + return true; + } + + $serviceId = $domainInfo['service_id'] ?? null; + if (!\is_string($serviceId) || $serviceId === '') { + return false; + } + + if ($serviceId !== $this->serviceId) { + return true; + } + + return $this->tls->isRenewRequired($domain, $domainType); } public function deleteCertificate(string $domain, ?string $domainType = null): void diff --git a/tests/Cdn/Certificates/Provider/FastlyTest.php b/tests/Cdn/Certificates/Provider/FastlyTest.php index edc7d6f..ecbb0d6 100644 --- a/tests/Cdn/Certificates/Provider/FastlyTest.php +++ b/tests/Cdn/Certificates/Provider/FastlyTest.php @@ -103,6 +103,46 @@ public function testTlsFailureStopsBeforeVersionlessDomainReassignment(): void $this->assertStringStartsWith('https://api.fastly.com/tls/subscriptions?', $client->calls[1]['url']); } + public function testRenewIsRequiredWhenDomainBelongsToAnotherService(): void + { + $client = new TestClient([ + $this->json('{"data":[{"id":"domain_1","fqdn":"example.com","service_id":"old_service"}]}'), + ]); + + $renewRequired = (new Fastly('token', 'new_service', client: $client)) + ->isRenewRequired('example.com', null); + + $this->assertTrue($renewRequired); + $this->assertCount(1, $client->calls); + } + + public function testRenewIsNotRequiredForClassicDomain(): void + { + $client = new TestClient([ + $this->json('{"data":[{"id":"domain_1","fqdn":"example.com"}]}'), + ]); + + $renewRequired = (new Fastly('token', 'service_1', client: $client)) + ->isRenewRequired('example.com', null); + + $this->assertFalse($renewRequired); + $this->assertCount(1, $client->calls); + } + + public function testRenewDelegatesToTlsWhenDomainBelongsToService(): void + { + $client = new TestClient([ + $this->json('{"data":[{"id":"domain_1","fqdn":"example.com","service_id":"service_1"}]}'), + $this->json('{"data":[{"id":"sub_1","attributes":{"state":"issued"}}]}'), + ]); + + $renewRequired = (new Fastly('token', 'service_1', client: $client)) + ->isRenewRequired('example.com', null); + + $this->assertFalse($renewRequired); + $this->assertCount(2, $client->calls); + } + public function testDeleteRemovesVersionlessSubscriptionAndDomain(): void { $client = new TestClient([