From 135b9a681349d6f2ad8e93a783a374e73cf1b016 Mon Sep 17 00:00:00 2001 From: Phil Young Date: Tue, 15 Jun 2021 11:02:17 +0100 Subject: [PATCH 01/12] Update A Docblock PHPStorm pointed out that this was missing --- src/Endpoints/CustomHostnames.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Endpoints/CustomHostnames.php b/src/Endpoints/CustomHostnames.php index a7f09b09..8e6e3d8b 100644 --- a/src/Endpoints/CustomHostnames.php +++ b/src/Endpoints/CustomHostnames.php @@ -14,7 +14,7 @@ class CustomHostnames implements API { use BodyAccessorTrait; - + private $adapter; public function __construct(Adapter $adapter) @@ -125,6 +125,8 @@ public function getHostname(string $zoneID, string $hostnameID) * @param string $hostnameID * @param string $sslMethod * @param string $sslType + * @param array $sslSettings + * @param string $customOriginServer * @return \stdClass */ public function updateHostname(string $zoneID, string $hostnameID, string $sslMethod = '', string $sslType = '', array $sslSettings = [], string $customOriginServer = ''): \stdClass From da3d10c4ee19484e21ef828e983f7def3a38915b Mon Sep 17 00:00:00 2001 From: Phil Young Date: Tue, 15 Jun 2021 11:10:23 +0100 Subject: [PATCH 02/12] Add More Properties To Custom Hostname Updating These are documented but not provided by the SDK --- src/Endpoints/CustomHostnames.php | 18 ++++++- tests/Endpoints/CustomHostnamesTest.php | 64 ++++++++++++++++++++++++- 2 files changed, 79 insertions(+), 3 deletions(-) diff --git a/src/Endpoints/CustomHostnames.php b/src/Endpoints/CustomHostnames.php index 8e6e3d8b..7e01cf2f 100644 --- a/src/Endpoints/CustomHostnames.php +++ b/src/Endpoints/CustomHostnames.php @@ -129,7 +129,7 @@ public function getHostname(string $zoneID, string $hostnameID) * @param string $customOriginServer * @return \stdClass */ - public function updateHostname(string $zoneID, string $hostnameID, string $sslMethod = '', string $sslType = '', array $sslSettings = [], string $customOriginServer = ''): \stdClass + public function updateHostname(string $zoneID, string $hostnameID, string $sslMethod = '', string $sslType = '', array $sslSettings = [], string $customOriginServer = '', ?bool $wildcard = null, string $bundleMethod = '', string $customKey = '', string $customCertificate = ''): \stdClass { $query = []; @@ -145,6 +145,22 @@ public function updateHostname(string $zoneID, string $hostnameID, string $sslMe $query['settings'] = $sslSettings; } + if (is_null($wildcard) === false) { + $query['wildcard'] = $wildcard; + } + + if (empty($bundleMethod) === false) { + $query['bundle_method'] = $bundleMethod; + } + + if (empty($customKey) === false) { + $query['custom_key'] = $customKey; + } + + if (empty($customCertificate) === false) { + $query['custom_certificate'] = $customCertificate; + } + if (!empty($query)) { $options = [ 'ssl' => $query diff --git a/tests/Endpoints/CustomHostnamesTest.php b/tests/Endpoints/CustomHostnamesTest.php index 7298d869..d8be09a8 100644 --- a/tests/Endpoints/CustomHostnamesTest.php +++ b/tests/Endpoints/CustomHostnamesTest.php @@ -108,6 +108,61 @@ public function testUpdateHostname() $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); $mock->method('patch')->willReturn($response); + $customKey = <<expects($this->once()) ->method('patch') ->with( @@ -121,7 +176,12 @@ public function testUpdateHostname() 'http2' => 'on', 'http3' => 'on', 'min_tls_version' => '1.2' - ] + ], + 'bundle_method' => 'optimal', + 'custom_key' => $customKey, + 'custom_certificate' => $customCertificate, + 'wildcard' => true, + ] ]) ); @@ -132,7 +192,7 @@ public function testUpdateHostname() 'http3' => 'on', 'min_tls_version' => '1.2' ]; - $result = $zones->updateHostname('023e105f4ecef8ad9ca31a8372d0c353', '0d89c70d-ad9f-4843-b99f-6cc0252067e9', 'http', 'dv', $sslSettings, 'origin.example.com'); + $result = $zones->updateHostname('023e105f4ecef8ad9ca31a8372d0c353', '0d89c70d-ad9f-4843-b99f-6cc0252067e9', 'http', 'dv', $sslSettings, 'origin.example.com', true, 'optimal', $customKey, $customCertificate); $this->assertObjectHasAttribute('id', $result); $this->assertObjectHasAttribute('hostname', $result); From f8124e76cbd885847d4fe25a751234b2e0cb57d3 Mon Sep 17 00:00:00 2001 From: Phil Young Date: Tue, 15 Jun 2021 11:12:37 +0100 Subject: [PATCH 03/12] Shorten Longer Lines The properties I've added have made these some pretty long lines --- src/Endpoints/CustomHostnames.php | 13 +++++++++++-- tests/Endpoints/CustomHostnamesTest.php | 13 ++++++++++++- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/Endpoints/CustomHostnames.php b/src/Endpoints/CustomHostnames.php index 7e01cf2f..219cfbba 100644 --- a/src/Endpoints/CustomHostnames.php +++ b/src/Endpoints/CustomHostnames.php @@ -129,8 +129,17 @@ public function getHostname(string $zoneID, string $hostnameID) * @param string $customOriginServer * @return \stdClass */ - public function updateHostname(string $zoneID, string $hostnameID, string $sslMethod = '', string $sslType = '', array $sslSettings = [], string $customOriginServer = '', ?bool $wildcard = null, string $bundleMethod = '', string $customKey = '', string $customCertificate = ''): \stdClass - { + public function updateHostname( + string $zoneID, + string $hostnameID, + string $sslMethod = '', + string $sslType = '', + array $sslSettings = [], + string $customOriginServer = '', + ?bool $wildcard = null, + string $bundleMethod = '', + string $customKey = '', + string $customCertificate = ''): \stdClass { $query = []; if (!empty($sslMethod)) { diff --git a/tests/Endpoints/CustomHostnamesTest.php b/tests/Endpoints/CustomHostnamesTest.php index d8be09a8..2c324945 100644 --- a/tests/Endpoints/CustomHostnamesTest.php +++ b/tests/Endpoints/CustomHostnamesTest.php @@ -192,7 +192,18 @@ public function testUpdateHostname() 'http3' => 'on', 'min_tls_version' => '1.2' ]; - $result = $zones->updateHostname('023e105f4ecef8ad9ca31a8372d0c353', '0d89c70d-ad9f-4843-b99f-6cc0252067e9', 'http', 'dv', $sslSettings, 'origin.example.com', true, 'optimal', $customKey, $customCertificate); + $result = $zones->updateHostname( + '023e105f4ecef8ad9ca31a8372d0c353', + '0d89c70d-ad9f-4843-b99f-6cc0252067e9', + 'http', + 'dv', + $sslSettings, + 'origin.example.com', + true, + 'optimal', + $customKey, + $customCertificate + ); $this->assertObjectHasAttribute('id', $result); $this->assertObjectHasAttribute('hostname', $result); From aea57e4994c34fd9753c7b5c9827053e246bdf4e Mon Sep 17 00:00:00 2001 From: Phil Young Date: Tue, 15 Jun 2021 11:12:57 +0100 Subject: [PATCH 04/12] Update A Docblock I missed adding the new properties in earlier --- src/Endpoints/CustomHostnames.php | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/Endpoints/CustomHostnames.php b/src/Endpoints/CustomHostnames.php index 219cfbba..db2a55dd 100644 --- a/src/Endpoints/CustomHostnames.php +++ b/src/Endpoints/CustomHostnames.php @@ -121,12 +121,16 @@ public function getHostname(string $zoneID, string $hostnameID) /** * @SuppressWarnings(PHPMD.BooleanArgumentFlag) * - * @param string $zoneID - * @param string $hostnameID - * @param string $sslMethod - * @param string $sslType - * @param array $sslSettings - * @param string $customOriginServer + * @param string $zoneID + * @param string $hostnameID + * @param string $sslMethod + * @param string $sslType + * @param array $sslSettings + * @param string $customOriginServer + * @param bool|null $wildcard + * @param string $bundleMethod + * @param string $customKey + * @param string $customCertificate * @return \stdClass */ public function updateHostname( From 63b11374f256b778434c8e1b2a0f027c9b87ceb8 Mon Sep 17 00:00:00 2001 From: Phil Young Date: Tue, 22 Jun 2021 11:13:53 +0100 Subject: [PATCH 05/12] Move SSL Setting To An Array This will match the add function and help with PHPMD errors --- src/Endpoints/CustomHostnames.php | 15 ++- tests/Endpoints/CustomHostnamesTest.php | 131 +++++++++++++----------- 2 files changed, 79 insertions(+), 67 deletions(-) diff --git a/src/Endpoints/CustomHostnames.php b/src/Endpoints/CustomHostnames.php index 1a90d0f3..507d41d3 100644 --- a/src/Endpoints/CustomHostnames.php +++ b/src/Endpoints/CustomHostnames.php @@ -129,8 +129,7 @@ public function getHostname(string $zoneID, string $hostnameID) * @param string $customOriginServer * @param bool|null $wildcard * @param string $bundleMethod - * @param string $customKey - * @param string $customCertificate + * @param array $customSsl * @return \stdClass */ public function updateHostname( @@ -142,8 +141,8 @@ public function updateHostname( string $customOriginServer = '', ?bool $wildcard = null, string $bundleMethod = '', - string $customKey = '', - string $customCertificate = ''): \stdClass { + array $customSsl = [] + ): \stdClass { $query = []; if (!empty($sslMethod)) { @@ -166,12 +165,12 @@ public function updateHostname( $query['bundle_method'] = $bundleMethod; } - if (empty($customKey) === false) { - $query['custom_key'] = $customKey; + if (empty($customSsl['key']) === false) { + $query['custom_key'] = $customSsl['key']; } - if (empty($customCertificate) === false) { - $query['custom_certificate'] = $customCertificate; + if (empty($customSsl['certificate']) === false) { + $query['custom_certificate'] = $customSsl['certificate']; } if (!empty($query)) { diff --git a/tests/Endpoints/CustomHostnamesTest.php b/tests/Endpoints/CustomHostnamesTest.php index ad752e60..b542c83e 100644 --- a/tests/Endpoints/CustomHostnamesTest.php +++ b/tests/Endpoints/CustomHostnamesTest.php @@ -105,64 +105,11 @@ public function testUpdateHostname() { $response = $this->getPsr7JsonResponseForFixture('Endpoints/updateHostname.json'); + $customSsl = $this->getCustomSsl(); + $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); $mock->method('patch')->willReturn($response); - $customKey = <<expects($this->once()) ->method('patch') ->with( @@ -178,8 +125,8 @@ public function testUpdateHostname() 'min_tls_version' => '1.2' ], 'bundle_method' => 'optimal', - 'custom_key' => $customKey, - 'custom_certificate' => $customCertificate, + 'custom_key' => $customSsl['key'], + 'custom_certificate' => $customSsl['certificate'], 'wildcard' => true, ] @@ -201,8 +148,10 @@ public function testUpdateHostname() 'origin.example.com', true, 'optimal', - $customKey, - $customCertificate + [ + 'key' => $customSsl['key'], + 'certificate' => $customSsl['certificate'], + ] ); $this->assertObjectHasAttribute('id', $result); @@ -249,4 +198,68 @@ public function testGetHostnameFallbackOrigin() $this->assertObjectHasAttribute('origin', $result); $this->assertObjectHasAttribute('status', $result); } + + private function getCustomSsl(): array + { + $customKey = << $customKey, + 'certificate' => $customCertificate, + ]; + } } From aa43a1ea3643adad418f03309e888c0c119151f4 Mon Sep 17 00:00:00 2001 From: Phil Young Date: Tue, 22 Jun 2021 11:16:25 +0100 Subject: [PATCH 06/12] Move To Shorthand False Checks This seems to be the preferred way to do them --- src/Endpoints/CustomHostnames.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Endpoints/CustomHostnames.php b/src/Endpoints/CustomHostnames.php index 507d41d3..f1027fc0 100644 --- a/src/Endpoints/CustomHostnames.php +++ b/src/Endpoints/CustomHostnames.php @@ -157,19 +157,19 @@ public function updateHostname( $query['settings'] = $sslSettings; } - if (is_null($wildcard) === false) { + if (!is_null($wildcard)) { $query['wildcard'] = $wildcard; } - if (empty($bundleMethod) === false) { + if (!empty($bundleMethod)) { $query['bundle_method'] = $bundleMethod; } - if (empty($customSsl['key']) === false) { + if (!empty($customSsl['key'])) { $query['custom_key'] = $customSsl['key']; } - if (empty($customSsl['certificate']) === false) { + if (!empty($customSsl['certificate'])) { $query['custom_certificate'] = $customSsl['certificate']; } From c66022e7bfb116a954f1685ef25476253d32b37a Mon Sep 17 00:00:00 2001 From: Phil Young Date: Tue, 22 Jun 2021 11:27:41 +0100 Subject: [PATCH 07/12] Add A Default Variable Value This is always set now --- src/Endpoints/CustomHostnames.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Endpoints/CustomHostnames.php b/src/Endpoints/CustomHostnames.php index f1027fc0..22c16166 100644 --- a/src/Endpoints/CustomHostnames.php +++ b/src/Endpoints/CustomHostnames.php @@ -144,6 +144,7 @@ public function updateHostname( array $customSsl = [] ): \stdClass { $query = []; + $options = []; if (!empty($sslMethod)) { $query['method'] = $sslMethod; From 3b167bab0461dd576d05bccb6f32cd7e25f03796 Mon Sep 17 00:00:00 2001 From: Phil Young Date: Tue, 22 Jun 2021 11:28:46 +0100 Subject: [PATCH 08/12] Add PHPMD Suppressions I'm unsure as to how best to refactor this in the way that the SDK is written. I'm going to commit this and ask on the PR how the maintainers want it --- src/Endpoints/CustomHostnames.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Endpoints/CustomHostnames.php b/src/Endpoints/CustomHostnames.php index 22c16166..9e01fa26 100644 --- a/src/Endpoints/CustomHostnames.php +++ b/src/Endpoints/CustomHostnames.php @@ -120,6 +120,8 @@ public function getHostname(string $zoneID, string $hostnameID) /** * @SuppressWarnings(PHPMD.BooleanArgumentFlag) + * @SuppressWarnings(PHPMD.CyclomaticComplexity) + * @SuppressWarnings(PHPMD.NPathComplexity) * * @param string $zoneID * @param string $hostnameID From e7cc9e067f79e998f534a7971ca9e1211c26881f Mon Sep 17 00:00:00 2001 From: Phil Young Date: Tue, 22 Jun 2021 11:39:12 +0100 Subject: [PATCH 09/12] Fix A PHP 7.0 Backwards Incompatibility Too new, that was --- src/Endpoints/CustomHostnames.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Endpoints/CustomHostnames.php b/src/Endpoints/CustomHostnames.php index 592ed718..7a0d2de3 100644 --- a/src/Endpoints/CustomHostnames.php +++ b/src/Endpoints/CustomHostnames.php @@ -164,7 +164,7 @@ public function updateHostname( string $sslType = '', array $sslSettings = [], string $customOriginServer = '', - ?bool $wildcard = null, + bool $wildcard = null, string $bundleMethod = '', array $customSsl = [] ): \stdClass { From f88a145785a88250d33efe6d6258b1c523a23bda Mon Sep 17 00:00:00 2001 From: Phil Young Date: Tue, 22 Jun 2021 12:05:30 +0100 Subject: [PATCH 10/12] Update tests/Endpoints/CustomHostnamesTest.php --- tests/Endpoints/CustomHostnamesTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/Endpoints/CustomHostnamesTest.php b/tests/Endpoints/CustomHostnamesTest.php index 90dda064..f8cf990b 100644 --- a/tests/Endpoints/CustomHostnamesTest.php +++ b/tests/Endpoints/CustomHostnamesTest.php @@ -155,6 +155,7 @@ public function testUpdateHostname() 'http3' => 'on', 'min_tls_version' => '1.2' ]; + $result = $zones->updateHostname( '023e105f4ecef8ad9ca31a8372d0c353', '0d89c70d-ad9f-4843-b99f-6cc0252067e9', From 1b80a3bae7c0f52cec834d69421df29688f9c4d8 Mon Sep 17 00:00:00 2001 From: Phil Young Date: Tue, 22 Jun 2021 12:21:59 +0100 Subject: [PATCH 11/12] Update tests/Endpoints/CustomHostnamesTest.php --- tests/Endpoints/CustomHostnamesTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/Endpoints/CustomHostnamesTest.php b/tests/Endpoints/CustomHostnamesTest.php index f8cf990b..90dda064 100644 --- a/tests/Endpoints/CustomHostnamesTest.php +++ b/tests/Endpoints/CustomHostnamesTest.php @@ -155,7 +155,6 @@ public function testUpdateHostname() 'http3' => 'on', 'min_tls_version' => '1.2' ]; - $result = $zones->updateHostname( '023e105f4ecef8ad9ca31a8372d0c353', '0d89c70d-ad9f-4843-b99f-6cc0252067e9', From ba875c32cab7c7fdffa251a81af565771acec5ad Mon Sep 17 00:00:00 2001 From: Phil Young Date: Tue, 22 Jun 2021 16:21:55 +0100 Subject: [PATCH 12/12] Update tests/Endpoints/CustomHostnamesTest.php --- tests/Endpoints/CustomHostnamesTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/Endpoints/CustomHostnamesTest.php b/tests/Endpoints/CustomHostnamesTest.php index 90dda064..f8cf990b 100644 --- a/tests/Endpoints/CustomHostnamesTest.php +++ b/tests/Endpoints/CustomHostnamesTest.php @@ -155,6 +155,7 @@ public function testUpdateHostname() 'http3' => 'on', 'min_tls_version' => '1.2' ]; + $result = $zones->updateHostname( '023e105f4ecef8ad9ca31a8372d0c353', '0d89c70d-ad9f-4843-b99f-6cc0252067e9',