Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
35 / 35 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
SeedPublicationCommand | |
100.00% |
35 / 35 |
|
100.00% |
4 / 4 |
11 | |
100.00% |
1 / 1 |
safeHandle | |
100.00% |
18 / 18 |
|
100.00% |
1 / 1 |
4 | |||
getPublicationTypeSelection | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
3 | |||
getPublicationTypes | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
pluralize | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace Hyde\Publications\Commands; |
6 | |
7 | use Hyde\Publications\Actions\SeedsPublicationFiles; |
8 | use Hyde\Publications\Models\PublicationType; |
9 | use Hyde\Publications\Publications; |
10 | use Illuminate\Support\Collection; |
11 | use InvalidArgumentException; |
12 | use LaravelZero\Framework\Commands\Command; |
13 | |
14 | use function microtime; |
15 | use function round; |
16 | use function sprintf; |
17 | |
18 | /** |
19 | * Hyde command to seed publication files for a publication type. |
20 | * |
21 | * @see \Hyde\Publications\Actions\SeedsPublicationFiles |
22 | * @see \Hyde\Publications\Testing\Feature\SeedPublicationCommandTest |
23 | */ |
24 | class SeedPublicationCommand extends ValidatingCommand |
25 | { |
26 | /** @var string */ |
27 | protected $signature = 'seed:publications |
28 | {publicationType? : The name of the publication type to create publications for} |
29 | {number? : The number of publications to generate}'; |
30 | |
31 | /** @var string */ |
32 | protected $description = 'Generate random publications for a publication type'; |
33 | |
34 | public function safeHandle(): int |
35 | { |
36 | $this->title('Seeding new publications!'); |
37 | |
38 | $publicationType = $this->getPublicationTypeSelection($this->getPublicationTypes()); |
39 | $number = (int) ($this->argument('number') ?? $this->askWithValidation( |
40 | 'number', |
41 | 'How many publications would you like to generate', |
42 | ['required', 'integer', 'between:1,100000'], 1)); |
43 | |
44 | if ($number >= 10000) { |
45 | $this->warn('Warning: Generating a large number of publications may take a while. <fg=gray>Expected time: '.($number / 1000).' seconds.</>'); |
46 | if (! $this->confirm('Are you sure you want to continue?')) { |
47 | return parent::USER_EXIT; |
48 | } |
49 | } |
50 | |
51 | $timeStart = microtime(true); |
52 | $seeder = new SeedsPublicationFiles($publicationType, $number); |
53 | $seeder->create(); |
54 | |
55 | $ms = round((microtime(true) - $timeStart) * 1000); |
56 | $each = round($ms / $number, 2); |
57 | $this->info(sprintf("<comment>$number</comment> publication{$this->pluralize($number)} for <comment>$publicationType->name</comment> created! <fg=gray>Took {$ms}ms%s", |
58 | ($number > 1) ? " ({$each}ms/each)</>" : '')); |
59 | |
60 | return Command::SUCCESS; |
61 | } |
62 | |
63 | /** |
64 | * @param \Illuminate\Support\Collection<string, \Hyde\Publications\Models\PublicationType> $publicationTypes |
65 | * @return \Hyde\Publications\Models\PublicationType |
66 | */ |
67 | protected function getPublicationTypeSelection(Collection $publicationTypes): PublicationType |
68 | { |
69 | $publicationType = $this->argument('publicationType') ?? $publicationTypes->keys()->get( |
70 | (int) $this->choice( |
71 | 'Which publication type would you like to seed?', |
72 | $publicationTypes->keys()->toArray() |
73 | ) |
74 | ); |
75 | |
76 | if ($publicationTypes->has($publicationType)) { |
77 | if ($this->argument('number')) { |
78 | $this->line("<info>Creating</info> [<comment>{$this->argument('number')}</comment>] <info>random publications for type</info> [<comment>$publicationType</comment>]"); |
79 | } else { |
80 | $this->line("<info>Creating random publications for type</info> [<comment>$publicationType</comment>]"); |
81 | } |
82 | |
83 | return $publicationTypes->get($publicationType); |
84 | } |
85 | |
86 | throw new InvalidArgumentException("Unable to locate publication type [$publicationType]"); |
87 | } |
88 | |
89 | /** |
90 | * @return \Illuminate\Support\Collection<string, PublicationType> |
91 | * |
92 | * @throws \InvalidArgumentException |
93 | */ |
94 | protected function getPublicationTypes(): Collection |
95 | { |
96 | $publicationTypes = Publications::getPublicationTypes(); |
97 | if ($publicationTypes->isEmpty()) { |
98 | throw new InvalidArgumentException('Unable to locate any publication types. Did you create any?'); |
99 | } |
100 | |
101 | return $publicationTypes; |
102 | } |
103 | |
104 | protected function pluralize(int $count): string |
105 | { |
106 | return ($count === 1) ? '' : 's'; |
107 | } |
108 | } |