Skip to content

Commit 57ce1db

Browse files
committed
Create model: SiteDetectorOptions
1 parent 631c55e commit 57ce1db

File tree

8 files changed

+207
-91
lines changed

8 files changed

+207
-91
lines changed

src/Command/BaseCommand.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Drall\Command;
44

5+
use Drall\Model\SiteDetectorOptions;
56
use Drall\Service\SiteDetector;
67
use Drall\Trait\SiteDetectorAwareTrait;
78
use Psr\Log\LoggerAwareTrait;
@@ -77,13 +78,23 @@ protected function preExecute(InputInterface $input, OutputInterface $output) {
7778
$this->setSiteDetector(new SiteDetector());
7879
}
7980

80-
if ($group = $this->getDrallGroup($input)) {
81+
$options = SiteDetectorOptions::fromInput($input);
82+
83+
if ($group = $options->getGroup()) {
8184
$this->logger->info('Using group: {group}', ['group' => $group]);
8285
}
8386

84-
if ($filter = $this->getDrallFilter($input)) {
87+
if ($filter = $options->getFilter()) {
8588
$this->logger->info('Using filter: {filter}', ['filter' => $filter]);
8689
}
90+
91+
if ($offset = $options->getOffset()) {
92+
$this->logger->info('Using offset: {offset}', ['offset' => $offset]);
93+
}
94+
95+
if ($limit = $options->getLimit()) {
96+
$this->logger->info('Using limit: {limit}', ['limit' => $limit]);
97+
}
8798
}
8899

89100
}

src/Command/ExecCommand.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Amp\Pipeline\Pipeline;
88
use Amp\Process\Process;
99
use Drall\Model\Placeholder;
10+
use Drall\Model\SiteDetectorOptions;
1011
use Symfony\Component\Console\Command\Command;
1112
use Symfony\Component\Console\Command\SignalableCommandInterface;
1213
use Symfony\Component\Console\Helper\ProgressBar;
@@ -209,19 +210,18 @@ protected function execute(InputInterface $input, OutputInterface $output): int
209210
return 1;
210211
}
211212

212-
$group = $this->getDrallGroup($input);
213-
$filter = $this->getDrallFilter($input);
214-
215213
if (!$placeholder = $this->getUniquePlaceholder($command)) {
216214
return 1;
217215
}
218216

217+
$sdOptions = SiteDetectorOptions::fromInput($input);
218+
219219
// Get all possible values for the placeholder.
220220
$values = match ($placeholder) {
221-
Placeholder::Directory => $this->siteDetector()->getSiteDirNames($group, $filter),
222-
Placeholder::Site => $this->siteDetector()->getSiteAliasNames($group, $filter),
223-
Placeholder::Key => $this->siteDetector()->getSiteKeys($group, $filter),
224-
Placeholder::UniqueKey => $this->siteDetector()->getSiteKeys($group, $filter, TRUE),
221+
Placeholder::Directory => $this->siteDetector()->getSiteDirNames($sdOptions),
222+
Placeholder::Site => $this->siteDetector()->getSiteAliasNames($sdOptions),
223+
Placeholder::Key => $this->siteDetector()->getSiteKeys($sdOptions),
224+
Placeholder::UniqueKey => $this->siteDetector()->getSiteKeys($sdOptions, TRUE),
225225
default => throw new \RuntimeException('Unrecognized placeholder: ' . $placeholder->value),
226226
};
227227

src/Command/SiteAliasesCommand.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Drall\Command;
44

5+
use Drall\Model\SiteDetectorOptions;
56
use Symfony\Component\Console\Attribute\AsCommand;
67
use Symfony\Component\Console\Input\InputInterface;
78
use Symfony\Component\Console\Output\OutputInterface;
@@ -17,17 +18,15 @@ protected function configure() {
1718
parent::configure();
1819
$this->addUsage('site:aliases');
1920
$this->addUsage('--group=GROUP site:aliases');
20-
$this->addUsage('--filter=FILTER site:aliases');
21+
$this->addUsage('--filter=FILTER site:aliases');
2122
}
2223

2324
protected function execute(InputInterface $input, OutputInterface $output): int {
2425
$this->preExecute($input, $output);
2526

27+
$sdOptions = SiteDetectorOptions::fromInput($input);
2628
$aliases = $this->siteDetector()
27-
->getSiteAliases(
28-
$this->getDrallGroup($input),
29-
$this->getDrallFilter($input),
30-
);
29+
->getSiteAliases($sdOptions);
3130

3231
if (count($aliases) === 0) {
3332
$this->logger->warning('No site aliases found.');

src/Command/SiteDirectoriesCommand.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Drall\Command;
44

5+
use Drall\Model\SiteDetectorOptions;
56
use Symfony\Component\Console\Attribute\AsCommand;
67
use Symfony\Component\Console\Input\InputInterface;
78
use Symfony\Component\Console\Output\OutputInterface;
@@ -22,11 +23,9 @@ protected function configure() {
2223
protected function execute(InputInterface $input, OutputInterface $output): int {
2324
$this->preExecute($input, $output);
2425

26+
$sdOptions = SiteDetectorOptions::fromInput($input);
2527
$dirNames = $this->siteDetector()
26-
->getSiteDirNames(
27-
$this->getDrallGroup($input),
28-
$this->getDrallFilter($input),
29-
);
28+
->getSiteDirNames($sdOptions);
3029

3130
if (count($dirNames) === 0) {
3231
$this->logger->warning('No Drupal sites found.');

src/Command/SiteKeysCommand.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Drall\Command;
44

5+
use Drall\Model\SiteDetectorOptions;
56
use Symfony\Component\Console\Attribute\AsCommand;
67
use Symfony\Component\Console\Input\InputInterface;
78
use Symfony\Component\Console\Output\OutputInterface;
@@ -23,11 +24,9 @@ protected function configure() {
2324
protected function execute(InputInterface $input, OutputInterface $output): int {
2425
$this->preExecute($input, $output);
2526

27+
$sdOptions = SiteDetectorOptions::fromInput($input);
2628
$keys = $this->siteDetector()
27-
->getSiteKeys(
28-
$this->getDrallGroup($input),
29-
$this->getDrallFilter($input),
30-
);
29+
->getSiteKeys($sdOptions);
3130

3231
if (count($keys) === 0) {
3332
$this->logger->warning('No Drupal sites found.');

src/Model/SiteDetectorOptions.php

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
namespace Drall\Model;
4+
5+
use Symfony\Component\Console\Input\InputInterface;
6+
7+
class SiteDetectorOptions {
8+
9+
private ?string $filter = NULL;
10+
11+
private ?string $group = NULL;
12+
13+
private ?int $limit = NULL;
14+
15+
private ?int $offset = NULL;
16+
17+
public function __construct() {}
18+
19+
public static function fromInput(InputInterface $input): SiteDetectorOptions {
20+
$options = new SiteDetectorOptions();
21+
22+
if (
23+
$input->hasOption('group') &&
24+
$group = $input->getOption('group') ?? getenv('DRALL_GROUP')
25+
) {
26+
$options->setGroup($group);
27+
}
28+
29+
if (
30+
$input->hasOption('filter') &&
31+
$filter = $input->getOption('filter')
32+
) {
33+
$options->setFilter($filter);
34+
}
35+
36+
if (
37+
$input->hasOption('offset') &&
38+
$offset = $input->getOption('offset')
39+
) {
40+
$options->setOffset($offset);
41+
}
42+
43+
if (
44+
$input->hasOption('limit') &&
45+
$limit = $input->getOption('limit')
46+
) {
47+
$options->setLimit($limit);
48+
}
49+
50+
return $options;
51+
}
52+
53+
public function getFilter(): ?string {
54+
return $this->filter;
55+
}
56+
57+
public function setFilter(string $filter): static {
58+
$this->filter = $filter;
59+
return $this;
60+
}
61+
62+
public function getGroup(): ?string {
63+
return $this->group;
64+
}
65+
66+
public function setGroup(string $group): static {
67+
$this->group = $group;
68+
return $this;
69+
}
70+
71+
public function getOffset(): ?int {
72+
return $this->offset;
73+
}
74+
75+
public function setOffset(int $offset): static {
76+
$this->offset = $offset;
77+
return $this;
78+
}
79+
80+
public function getLimit(): ?int {
81+
return $this->limit;
82+
}
83+
84+
public function setLimit(int $limit): static {
85+
if ($limit < 1) {
86+
throw new \ValueError('Limit must be a positive integer');
87+
}
88+
89+
$this->limit = $limit;
90+
return $this;
91+
}
92+
93+
}

0 commit comments

Comments
 (0)