From 7939f94fd85b401c23a5c0b7790e0f2f7187eeab Mon Sep 17 00:00:00 2001 From: Steffen Giers Date: Sun, 31 Oct 2021 22:13:59 +0100 Subject: [PATCH] refactor: remove tightly coupled template parser from report client (#36) --- lib/Client/BitbucketReport.php | 6 ++---- lib/Client/GithubReport.php | 6 ++---- lib/Client/GitlabReport.php | 6 ++---- lib/Report/ReportClient.php | 6 +++++- lib/Report/ReportInterface.php | 2 +- tests/Client/BitbucketReportTest.php | 2 +- tests/Client/GithubReportTest.php | 2 +- tests/Client/GitlabReportTest.php | 2 +- tests/Report/ReportClientTest.php | 4 +--- tests/Vendor/VendorTest.php | 2 -- 10 files changed, 16 insertions(+), 22 deletions(-) diff --git a/lib/Client/BitbucketReport.php b/lib/Client/BitbucketReport.php index 746c8dc..dded9a3 100644 --- a/lib/Client/BitbucketReport.php +++ b/lib/Client/BitbucketReport.php @@ -5,7 +5,6 @@ use KirbyReporter\Model\FormData; use KirbyReporter\Report\ReportInterface; use KirbyReporter\Report\ReportResponse; -use KirbyReporter\Report\ReportTemplateParser; use KirbyReporter\Traits\Expander; use KirbyReporter\Traits\Request; use KirbyReporter\Vendor\Vendor; @@ -14,7 +13,6 @@ class BitbucketReport implements ReportInterface { use Request; use Expander; - use ReportTemplateParser; private string $urlTemplate = "https://api.bitbucket.org/2.0/repositories/{user}/{repo}/issues"; @@ -25,7 +23,7 @@ public function __construct(Vendor $vendor) $this->vendor = $vendor; } - public final function report(FormData $reportData): ReportResponse + public final function report(FormData $reportData, string $parsedTemplate): ReportResponse { $url = $this->expandUrl($this->urlTemplate, [ "user" => $this->vendor->getOwner(), @@ -34,7 +32,7 @@ public final function report(FormData $reportData): ReportResponse $reportData = [ "title" => $reportData->getTitle(), - "description" => $this->parseTemplate($reportData->getFormFields()), + "description" => $parsedTemplate, ]; $header = ["Authorization" => "Basic ".$this->getBasicAuth()]; diff --git a/lib/Client/GithubReport.php b/lib/Client/GithubReport.php index 4c7ca42..2c8431a 100644 --- a/lib/Client/GithubReport.php +++ b/lib/Client/GithubReport.php @@ -5,7 +5,6 @@ use KirbyReporter\Model\FormData; use KirbyReporter\Report\ReportInterface; use KirbyReporter\Report\ReportResponse; -use KirbyReporter\Report\ReportTemplateParser; use KirbyReporter\Traits\Expander; use KirbyReporter\Traits\Request; use KirbyReporter\Vendor\Vendor; @@ -14,7 +13,6 @@ class GithubReport implements ReportInterface { use Request; use Expander; - use ReportTemplateParser; private string $urlTemplate = "https://api.github.com/repos/{user}/{repo}/issues"; @@ -25,7 +23,7 @@ public function __construct(Vendor $vendor) $this->vendor = $vendor; } - public final function report(FormData $reportData): ReportResponse + public final function report(FormData $reportData, string $parsedTemplate): ReportResponse { $url = $this->expandUrl($this->urlTemplate, [ "user" => $this->vendor->getOwner(), @@ -34,7 +32,7 @@ public final function report(FormData $reportData): ReportResponse $reportData = [ "title" => $reportData->getTitle(), - "body" => $this->parseTemplate($reportData->getFormFields()), + "body" => $parsedTemplate, ]; $header = ["Authorization" => "token ".$this->vendor->getToken()]; diff --git a/lib/Client/GitlabReport.php b/lib/Client/GitlabReport.php index 75e50eb..789b329 100644 --- a/lib/Client/GitlabReport.php +++ b/lib/Client/GitlabReport.php @@ -5,7 +5,6 @@ use KirbyReporter\Model\FormData; use KirbyReporter\Report\ReportInterface; use KirbyReporter\Report\ReportResponse; -use KirbyReporter\Report\ReportTemplateParser; use KirbyReporter\Traits\Expander; use KirbyReporter\Traits\Request; use KirbyReporter\Vendor\Vendor; @@ -14,7 +13,6 @@ class GitlabReport implements ReportInterface { use Request; use Expander; - use ReportTemplateParser; private string $urlTemplate = "https://gitlab.com/api/v4/projects/{user}%2F{repo}/issues"; @@ -25,7 +23,7 @@ public function __construct(Vendor $vendor) $this->vendor = $vendor; } - public final function report(FormData $reportData): ReportResponse + public final function report(FormData $reportData, string $parsedTemplate): ReportResponse { $url = $this->expandUrl($this->urlTemplate, [ "user" => $this->vendor->getOwner(), @@ -34,7 +32,7 @@ public final function report(FormData $reportData): ReportResponse $reportData = [ "title" => $reportData->getTitle(), - "description" => $this->parseTemplate($reportData->getFormFields()), + "description" => $parsedTemplate, ]; $header = ["Private-Token" => $this->vendor->getToken()]; diff --git a/lib/Report/ReportClient.php b/lib/Report/ReportClient.php index b7895a1..5a9bd3a 100644 --- a/lib/Report/ReportClient.php +++ b/lib/Report/ReportClient.php @@ -16,6 +16,8 @@ */ class ReportClient { + use ReportTemplateParser; + /** * @var BitbucketReport|GithubReport|GitlabReport */ @@ -38,6 +40,8 @@ public function __construct(Vendor $vendor) public final function createReport(FormData $formData): ReportResponse { - return $this->client->report($formData); + $parsedTemplate = $this->parseTemplate($formData->getFormFields()); + + return $this->client->report($formData, $parsedTemplate); } } diff --git a/lib/Report/ReportInterface.php b/lib/Report/ReportInterface.php index 452a942..e4e676d 100644 --- a/lib/Report/ReportInterface.php +++ b/lib/Report/ReportInterface.php @@ -10,5 +10,5 @@ */ interface ReportInterface { - public function report(FormData $reportData): ReportResponse; + public function report(FormData $reportData, string $parsedTemplate): ReportResponse; } diff --git a/tests/Client/BitbucketReportTest.php b/tests/Client/BitbucketReportTest.php index 2be42bc..9d189a7 100644 --- a/tests/Client/BitbucketReportTest.php +++ b/tests/Client/BitbucketReportTest.php @@ -50,7 +50,7 @@ public function test_should_send_form_data_to_bitbucket_api() 'description' => 'Test', ], ]); - $response = $reporter->report($formData); + $response = $reporter->report($formData, 'parsedTemplate'); // request $this->assertEquals("/2.0/repositories/test/test-repo/issues", $mock->getLastRequest()->getUri()->getPath()); diff --git a/tests/Client/GithubReportTest.php b/tests/Client/GithubReportTest.php index 5cdbaed..5be85ec 100644 --- a/tests/Client/GithubReportTest.php +++ b/tests/Client/GithubReportTest.php @@ -43,7 +43,7 @@ public function test_should_send_form_data_to_gitlab_api() 'description' => 'Test', ], ]); - $response = $reporter->report($formData); + $response = $reporter->report($formData, 'parsedTemplate'); // request $this->assertEquals("/repos/test/test-repo/issues", $mock->getLastRequest()->getUri()->getPath()); diff --git a/tests/Client/GitlabReportTest.php b/tests/Client/GitlabReportTest.php index 32f0336..e23c994 100644 --- a/tests/Client/GitlabReportTest.php +++ b/tests/Client/GitlabReportTest.php @@ -43,7 +43,7 @@ public function test_should_send_form_data_to_gitlab_api() 'description' => 'Test', ], ]); - $response = $reporter->report($formData); + $response = $reporter->report($formData, 'parsedTemplate'); // request $this->assertEquals("/api/v4/projects/test%2Ftest-repo/issues", $mock->getLastRequest()->getUri()->getPath()); diff --git a/tests/Report/ReportClientTest.php b/tests/Report/ReportClientTest.php index 076d7a3..77053f7 100644 --- a/tests/Report/ReportClientTest.php +++ b/tests/Report/ReportClientTest.php @@ -4,8 +4,6 @@ * @noinspection MethodShouldBeFinalInspection */ -namespace KirbyReporter\Mixins; - use GuzzleHttp\Client; use GuzzleHttp\Handler\MockHandler; use GuzzleHttp\HandlerStack; @@ -98,7 +96,7 @@ private function test_exeption(int $code, string $message) $this->expectExceptionMessage($message); $this->expectExceptionCode($code); - $reporter->report($formData); + $reporter->report($formData, 'parsedTemplate'); } } diff --git a/tests/Vendor/VendorTest.php b/tests/Vendor/VendorTest.php index 5dd377f..76b0d47 100644 --- a/tests/Vendor/VendorTest.php +++ b/tests/Vendor/VendorTest.php @@ -4,8 +4,6 @@ * @noinspection MethodShouldBeFinalInspection */ -namespace KirbyReporter\Mixins; - use KirbyReporter\Exception\OptionNotFoundException; use KirbyReporter\Exception\UnsupportedPlatformException; use KirbyReporter\Vendor\Vendor;