Skip to content

Commit

Permalink
chore: upgrade extract annotation (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
rem42 authored May 15, 2022
1 parent bfb6420 commit 2183822
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 58 deletions.
73 changes: 44 additions & 29 deletions src/Annotation/ExtractAnnotation.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,34 +58,10 @@ private function extractAnnotation(Scraper $annotation): void
{
$scraper = new Scraper();

$vars = get_object_vars($this->scraperAnnotation);
// Initializing class properties
foreach ($vars as $property => $value) {
$scraper->{$property} = $value;
}
$this->initDefaultValues($scraper);

$vars = get_object_vars($annotation);

/**
* @var string $property
* @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);
}
}

if ('path' === $property) {
$this->handlePath($scraper, $value);
continue;
}

$scraper->{$property} = $value;
}
$this->extractChildValues($scraper, $vars);

$this->scraperAnnotation = $scraper;
}
Expand All @@ -98,11 +74,14 @@ private function handlePath(Scraper $scraper, ?string $path = null): void

if ('' !== $path && '/' === $path[0]) {
$scraper->path = $path;
} elseif (isset($scraper->path)) {
return;
}

if (isset($scraper->path)) {
$scraper->path = rtrim($scraper->path, '/') . '/' . ltrim($path, '/');
} else {
$scraper->path = $path;
return;
}
$scraper->path = $path;
}

private function getScraperAnnotation(): Scraper
Expand All @@ -121,4 +100,40 @@ private function getScraperAnnotation(): Scraper

return $this->scraperAnnotation;
}

private function initDefaultValues(Scraper $scraper): void
{
$vars = get_object_vars($this->scraperAnnotation);
// Initializing class properties
foreach ($vars as $property => $value) {
$scraper->{$property} = $value;
}
}

/**
* @param array<string, mixed> $vars
*/
private function extractChildValues(Scraper $scraper, array $vars): void
{
/**
* @var string $property
* @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);
}
}

if ('path' === $property) {
$this->handlePath($scraper, $value);
continue;
}

$scraper->{$property} = $value;
}
}
}
67 changes: 38 additions & 29 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,31 +34,7 @@ public function send(ScraperRequest $request)
$this->request = $request;
$annotation = ExtractAnnotation::extract($this->request);

$options = [];

if ($this->request instanceof RequestAuthBearer) {
$options['auth_bearer'] = $this->request->getBearer();
}

if ($this->request instanceof RequestAuthBasic && false !== $this->request->isAuthBasic()) {
$options['auth_basic'] = $this->request->getAuthBasic();
}

if ($this->request instanceof RequestHeaders) {
$options['headers'] = $this->request->getHeaders();
}

if ($this->request instanceof RequestQuery) {
$options['query'] = $this->request->getQuery();
}

if ($this->request instanceof RequestBody) {
$options['body'] = $this->request->getBody();
}

if ($this->request instanceof RequestBodyJson) {
$options['json'] = $this->request->getJson();
}
$options = $this->buildOptions();

$throw = true;

Expand All @@ -76,8 +52,8 @@ public function send(ScraperRequest $request)
if ($throw && ($response->getStatusCode() >= 300 || $response->getStatusCode() < 200)) {
throw new ScraperException($response->getContent(false));
}
} catch (\Throwable $serverExceptionInterface) {
throw new ScraperException('cannot get response from: ' . $annotation->url(), \is_int($serverExceptionInterface->getCode()) ? $serverExceptionInterface->getCode() : 0, $serverExceptionInterface);
} catch (\Throwable $throwable) {
throw new ScraperException('cannot get response from: ' . $annotation->url(), \is_int($throwable->getCode()) ? $throwable->getCode() : 0, $throwable);
}

$apiReflectionClass = $this->getApiReflectionClass();
Expand All @@ -97,15 +73,48 @@ public function send(ScraperRequest $request)
*/
private function getApiReflectionClass(): \ReflectionClass
{
$requestReflectionClass = new \ReflectionClass($this->request);
$class = new \ReflectionClass($this->request);

/** @var class-string<AbstractApi> $apiClass */
$apiClass = str_replace('Request', 'Api', $requestReflectionClass->getName());
$apiClass = str_replace('Request', 'Api', $class->getName());

if (!class_exists($apiClass)) {
throw new ScraperException('Api class for this request not exist: ' . $apiClass);
}

return new \ReflectionClass($apiClass);
}

/**
* @return array<string, array<int|string,mixed>|resource|string>
*/
public function buildOptions(): array
{
$options = [];

if ($this->request instanceof RequestAuthBearer) {
$options['auth_bearer'] = $this->request->getBearer();
}

if ($this->request instanceof RequestAuthBasic && false !== $this->request->isAuthBasic()) {
$options['auth_basic'] = $this->request->getAuthBasic();
}

if ($this->request instanceof RequestHeaders) {
$options['headers'] = $this->request->getHeaders();
}

if ($this->request instanceof RequestQuery) {
$options['query'] = $this->request->getQuery();
}

if ($this->request instanceof RequestBody) {
$options['body'] = $this->request->getBody();
}

if ($this->request instanceof RequestBodyJson) {
$options['json'] = $this->request->getJson();
}
return $options;
}
}

0 comments on commit 2183822

Please sign in to comment.