Skip to content

Commit

Permalink
refactor: remove tightly coupled template parser from report client (#36
Browse files Browse the repository at this point in the history
)
  • Loading branch information
gearsdigital authored Oct 31, 2021
1 parent 9ce5787 commit 7939f94
Show file tree
Hide file tree
Showing 10 changed files with 16 additions and 22 deletions.
6 changes: 2 additions & 4 deletions lib/Client/BitbucketReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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";

Expand All @@ -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(),
Expand All @@ -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()];
Expand Down
6 changes: 2 additions & 4 deletions lib/Client/GithubReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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";

Expand All @@ -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(),
Expand All @@ -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()];
Expand Down
6 changes: 2 additions & 4 deletions lib/Client/GitlabReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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";

Expand All @@ -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(),
Expand All @@ -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()];
Expand Down
6 changes: 5 additions & 1 deletion lib/Report/ReportClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
class ReportClient
{
use ReportTemplateParser;

/**
* @var BitbucketReport|GithubReport|GitlabReport
*/
Expand All @@ -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);
}
}
2 changes: 1 addition & 1 deletion lib/Report/ReportInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
*/
interface ReportInterface
{
public function report(FormData $reportData): ReportResponse;
public function report(FormData $reportData, string $parsedTemplate): ReportResponse;
}
2 changes: 1 addition & 1 deletion tests/Client/BitbucketReportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion tests/Client/GithubReportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion tests/Client/GitlabReportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
4 changes: 1 addition & 3 deletions tests/Report/ReportClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
* @noinspection MethodShouldBeFinalInspection
*/

namespace KirbyReporter\Mixins;

use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
Expand Down Expand Up @@ -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');
}
}

2 changes: 0 additions & 2 deletions tests/Vendor/VendorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
* @noinspection MethodShouldBeFinalInspection
*/

namespace KirbyReporter\Mixins;

use KirbyReporter\Exception\OptionNotFoundException;
use KirbyReporter\Exception\UnsupportedPlatformException;
use KirbyReporter\Vendor\Vendor;
Expand Down

0 comments on commit 7939f94

Please sign in to comment.