From 5499c6c4d20991a320c1c2e514f61df2603be0ad Mon Sep 17 00:00:00 2001 From: rem42 Date: Sun, 15 May 2022 23:05:50 +0200 Subject: [PATCH] chore: fix client complexity (#18) --- src/Annotation/ExtractAnnotation.php | 20 +++++++++++++------- src/Client.php | 21 +++++++++++++-------- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/src/Annotation/ExtractAnnotation.php b/src/Annotation/ExtractAnnotation.php index dc1a5f9..bb32ec4 100644 --- a/src/Annotation/ExtractAnnotation.php +++ b/src/Annotation/ExtractAnnotation.php @@ -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); @@ -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; + } } diff --git a/src/Client.php b/src/Client.php index f1c2420..8e37a87 100644 --- a/src/Client.php +++ b/src/Client.php @@ -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( @@ -88,7 +83,7 @@ private function getApiReflectionClass(): \ReflectionClass /** * @return array|resource|string> */ - public function buildOptions(): array + private function buildOptions(): array { $options = []; @@ -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; + } }