Skip to content

Commit

Permalink
Merge 8d34a61 into 2b48b96
Browse files Browse the repository at this point in the history
  • Loading branch information
eliashaeussler committed Mar 28, 2024
2 parents 2b48b96 + 8d34a61 commit 5fcf964
Show file tree
Hide file tree
Showing 11 changed files with 124 additions and 166 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"psr/log": "^2.0 || ^3.0",
"symfony/console": "^5.4 || ^6.0 || ^7.0",
"symfony/filesystem": "^5.4 || ^6.0 || ^7.0",
"symfony/options-resolver": "^5.4 || ^6.0 || ^7.0",
"symfony/yaml": "^5.4 || ^6.0 || ^7.0"
},
"require-dev": {
Expand Down
136 changes: 68 additions & 68 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ parameters:
path: src/Config/CacheWarmupConfig.php

-
message: "#^Property EliasHaeussler\\\\CacheWarmup\\\\Crawler\\\\AbstractConfigurableCrawler\\<TOptions of array\\<string, mixed\\>\\>\\:\\:\\$options \\(TOptions of array\\<string, mixed\\>\\) does not accept array\\<string, mixed\\>\\.$#"
message: "#^Property EliasHaeussler\\\\CacheWarmup\\\\Crawler\\\\AbstractConfigurableCrawler\\<TOptions of array\\<string, mixed\\>\\>\\:\\:\\$options \\(TOptions of array\\<string, mixed\\>\\) does not accept array\\.$#"
count: 1
path: src/Crawler/AbstractConfigurableCrawler.php

Expand Down
21 changes: 7 additions & 14 deletions src/Crawler/AbstractConfigurableCrawler.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@

namespace EliasHaeussler\CacheWarmup\Crawler;

use EliasHaeussler\CacheWarmup\Exception;

use function array_diff_key;
use Symfony\Component\OptionsResolver;

/**
* AbstractConfigurableCrawler.
Expand All @@ -37,10 +35,7 @@
*/
abstract class AbstractConfigurableCrawler implements ConfigurableCrawlerInterface
{
/**
* @var TOptions
*/
protected static array $defaultOptions = [];
protected OptionsResolver\OptionsResolver $optionsResolver;

/**
* @var TOptions
Expand All @@ -52,20 +47,18 @@ abstract class AbstractConfigurableCrawler implements ConfigurableCrawlerInterfa
*/
public function __construct(array $options = [])
{
$this->optionsResolver = new OptionsResolver\OptionsResolver();
$this->configureOptions($this->optionsResolver);
$this->setOptions($options);
}

abstract protected function configureOptions(OptionsResolver\OptionsResolver $optionsResolver): void;

/**
* @param array<string, mixed> $options
*/
public function setOptions(array $options): void
{
$invalidOptions = array_diff_key($options, static::$defaultOptions);

if ([] !== $invalidOptions) {
throw Exception\InvalidCrawlerOptionException::createForAll($this, array_keys($invalidOptions));
}

$this->options = [...static::$defaultOptions, ...$options];
$this->options = $this->optionsResolver->resolve($options);
}
}
8 changes: 0 additions & 8 deletions src/Crawler/ConcurrentCrawler.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,6 @@ final class ConcurrentCrawler extends AbstractConfigurableCrawler implements Log
{
use ConcurrentCrawlerTrait;

protected static array $defaultOptions = [
'concurrency' => 5,
'request_method' => 'HEAD',
'request_headers' => [],
'request_options' => [],
'client_config' => [],
];

private ?Log\LoggerInterface $logger = null;

/**
Expand Down
31 changes: 31 additions & 0 deletions src/Crawler/ConcurrentCrawlerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use GuzzleHttp\Pool;
use GuzzleHttp\Psr7;
use Psr\Http\Message;
use Symfony\Component\OptionsResolver;

use function sprintf;

Expand All @@ -41,6 +42,36 @@
*/
trait ConcurrentCrawlerTrait
{
protected function configureOptions(OptionsResolver\OptionsResolver $optionsResolver): void
{
$optionsResolver->define('concurrency')
->allowedTypes('int')
->required()
->default(5)
;

$optionsResolver->define('request_method')
->allowedTypes('string')
->required()
->default('HEAD')
;

$optionsResolver->define('request_headers')
->allowedTypes('array')
->default([])
;

$optionsResolver->define('request_options')
->allowedTypes('array')
->default([])
;

$optionsResolver->define('client_config')
->allowedTypes('array')
->default([])
;
}

/**
* @param list<Message\UriInterface> $urls
* @param list<Http\Message\Handler\ResponseHandlerInterface> $handlers
Expand Down
8 changes: 0 additions & 8 deletions src/Crawler/OutputtingCrawler.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,6 @@ final class OutputtingCrawler extends AbstractConfigurableCrawler implements Log
{
use ConcurrentCrawlerTrait;

protected static array $defaultOptions = [
'concurrency' => 5,
'request_method' => 'HEAD',
'request_headers' => [],
'request_options' => [],
'client_config' => [],
];

private Console\Output\OutputInterface $output;
private ?Log\LoggerInterface $logger = null;

Expand Down
25 changes: 0 additions & 25 deletions src/Exception/InvalidCrawlerOptionException.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@
use EliasHaeussler\CacheWarmup\Crawler;
use RuntimeException;

use function array_pop;
use function count;
use function get_debug_type;
use function implode;
use function sprintf;

/**
Expand All @@ -48,28 +45,6 @@ public static function create(Crawler\ConfigurableCrawlerInterface $crawler, str
);
}

/**
* @param list<string> $options
*/
public static function createForAll(Crawler\ConfigurableCrawlerInterface $crawler, array $options): self
{
if (1 === count($options)) {
return self::create($crawler, $options[0]);
}

$lastOption = array_pop($options);

return new self(
sprintf(
'The crawler options "%s" and "%s" are invalid or not supported by crawler "%s".',
implode('", "', $options),
$lastOption,
$crawler::class,
),
1659206995,
);
}

public static function forInvalidType(mixed $options): self
{
return new self(
Expand Down
7 changes: 2 additions & 5 deletions tests/src/Crawler/AbstractConfigurableCrawlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use EliasHaeussler\CacheWarmup as Src;
use EliasHaeussler\CacheWarmup\Tests;
use PHPUnit\Framework;
use Symfony\Component\OptionsResolver;

/**
* AbstractConfigurableCrawlerTest.
Expand Down Expand Up @@ -57,11 +58,7 @@ public function defaultOptionsAreUsedOnInitialObject(): void
#[Framework\Attributes\Test]
public function setOptionsThrowsExceptionIfInvalidOptionsAreGiven(): void
{
$this->expectException(Src\Exception\InvalidCrawlerOptionException::class);
$this->expectExceptionCode(1659206995);
$this->expectExceptionMessage(
'The crawler options "dummy" and "blub" are invalid or not supported by crawler "'.$this->subject::class.'".',
);
$this->expectException(OptionsResolver\Exception\UndefinedOptionsException::class);

$this->subject->setOptions([
'foo' => 'bar',
Expand Down
Loading

0 comments on commit 5fcf964

Please sign in to comment.