Skip to content

Commit

Permalink
chore: fix client complexity (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
rem42 authored May 15, 2022
1 parent 2183822 commit 5499c6c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
20 changes: 13 additions & 7 deletions src/Annotation/ExtractAnnotation.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,7 @@ private function extractChildValues(Scraper $scraper, array $vars): void
* @var string $value
*/
foreach ($vars as $property => $value) {
if (preg_match_all('#{(.*?)}#', $value, $matchs)) {
foreach ($matchs[1] as $match) {
$method = 'get' . ucfirst($match);
$requestValue = $this->request->{$method}();
$value = str_replace('{' . $match . '}', $requestValue, $value);
}
}
$value = $this->replaceVariableInValue($value);

if ('path' === $property) {
$this->handlePath($scraper, $value);
Expand All @@ -136,4 +130,16 @@ private function extractChildValues(Scraper $scraper, array $vars): void
$scraper->{$property} = $value;
}
}

public function replaceVariableInValue(string $value): string
{
if (preg_match_all('#{(.*?)}#', $value, $matchs)) {
foreach ($matchs[1] as $match) {
$method = 'get' . ucfirst($match);
$requestValue = $this->request->{$method}();
$value = str_replace('{' . $match . '}', $requestValue, $value);
}
}
return $value;
}
}
21 changes: 13 additions & 8 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,9 @@ public function send(ScraperRequest $request)
{
$this->request = $request;
$annotation = ExtractAnnotation::extract($this->request);
$options = $this->buildOptions();

$options = $this->buildOptions();

$throw = true;

if ($this->request instanceof RequestException) {
$throw = $this->request->isThrow();
}
$throw = $this->isThrow();

try {
$response = $this->httpClient->request(
Expand Down Expand Up @@ -88,7 +83,7 @@ private function getApiReflectionClass(): \ReflectionClass
/**
* @return array<string, array<int|string,mixed>|resource|string>
*/
public function buildOptions(): array
private function buildOptions(): array
{
$options = [];

Expand Down Expand Up @@ -117,4 +112,14 @@ public function buildOptions(): array
}
return $options;
}

private function isThrow(): bool
{
$throw = true;

if ($this->request instanceof RequestException) {
$throw = $this->request->isThrow();
}
return $throw;
}
}

0 comments on commit 5499c6c

Please sign in to comment.