Skip to content

Commit 69f5b1c

Browse files
authored
Merge pull request #9 from TimoKoerber/essential-flag
Create a cleaner file with --essential flag
2 parents 87b4089 + 00cfb51 commit 69f5b1c

5 files changed

+77
-8
lines changed

README.md

+11-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ Now you're all set!
4141

4242
### Create operation files
4343
```shell
44-
php artisan operations:make <operation_name> // create operation file
44+
php artisan operations:make <operation_name> // create new operation file
45+
php artisan operations:make <operation_name> -e|--essential // create file without any attributes
4546
```
4647

4748
### Process operations
@@ -159,7 +160,6 @@ return new class extends OneTimeOperation
159160

160161
Provide your code in the `process()` method, for example:
161162

162-
163163
```php
164164
// operations/XXXX_XX_XX_XXXXXX_awesome_operation.php
165165

@@ -177,6 +177,15 @@ _(this is only recommended for small operations, since the processing of these o
177177

178178
**Hint:** If you use syncronous processing, the `$queue` attribute will be ignored (duh!).
179179

180+
### Create a cleaner operation file
181+
182+
If you don't need all the available attributes for your operation, you can create a *cleaner* operation file with the `--essential` or `-e` option:
183+
184+
```shell
185+
php artisan operations:make AwesomeOperation --essential
186+
php artisan operations:make AwesomeOperation -e
187+
```
188+
180189
### Processing the operations
181190

182191
![One-Time Operations for Laravel - Processing the operations](https://user-images.githubusercontent.com/65356688/224434129-43082402-6077-4043-8e97-c44786e60a59.png)

src/Commands/OneTimeOperationsMakeCommand.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@
77

88
class OneTimeOperationsMakeCommand extends OneTimeOperationsCommand
99
{
10-
protected $signature = 'operations:make {name : The name of the one-time operation}';
10+
protected $signature = 'operations:make
11+
{name : The name of the one-time operation}
12+
{--e|essential : Create file without any attributes}';
1113

1214
protected $description = 'Create a new one-time operation';
1315

1416
public function handle(): int
1517
{
1618
try {
17-
$file = OneTimeOperationCreator::createOperationFile($this->argument('name'));
19+
$file = OneTimeOperationCreator::createOperationFile($this->argument('name'), $this->option('essential'));
1820
$this->components->info(sprintf('One-time operation [%s] created successfully.', $file->getOperationName()));
1921

2022
return self::SUCCESS;

src/OneTimeOperationCreator.php

+13-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ class OneTimeOperationCreator
1515

1616
protected string $operationName = '';
1717

18+
protected bool $essential = false;
19+
1820
public function __construct()
1921
{
2022
$this->operationsDirectory = OneTimeOperationManager::getDirectoryPath();
@@ -23,10 +25,11 @@ public function __construct()
2325
/**
2426
* @throws \Throwable
2527
*/
26-
public static function createOperationFile(string $name): OneTimeOperationFile
28+
public static function createOperationFile(string $name, bool $essential = false): OneTimeOperationFile
2729
{
2830
$instance = new self();
2931
$instance->setProvidedName($name);
32+
$instance->setEssential($essential);
3033

3134
return OneTimeOperationFile::make($instance->createFile());
3235
}
@@ -54,6 +57,10 @@ protected function getPath(): string
5457

5558
protected function getStubFilepath(): string
5659
{
60+
if ($this->essential) {
61+
return File::get(__DIR__.'/../stubs/one-time-operation-essential.stub');
62+
}
63+
5764
return File::get(__DIR__.'/../stubs/one-time-operation.stub');
5865
}
5966

@@ -85,4 +92,9 @@ public function setProvidedName(string $providedName): void
8592
{
8693
$this->providedName = $providedName;
8794
}
95+
96+
public function setEssential(bool $essential): void
97+
{
98+
$this->essential = $essential;
99+
}
88100
}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
use TimoKoerber\LaravelOneTimeOperations\OneTimeOperation;
4+
5+
return new class extends OneTimeOperation
6+
{
7+
public function process(): void
8+
{
9+
//
10+
}
11+
};

tests/Feature/OneTimeOperationCommandTest.php

+38-3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@ public function test_make_command_with_attributes()
1818
$filepath = $this->filepath('2015_10_21_072800_awesome_operation.php');
1919
File::delete($filepath);
2020

21-
// no files, database entries or jobs
22-
$this->assertFileDoesNotExist($filepath);
23-
2421
// create operation file
2522
$this->artisan('operations:make AwesomeOperation')
2623
->assertSuccessful()
@@ -37,6 +34,44 @@ public function test_make_command_with_attributes()
3734
$this->assertStringContainsString('public function process(): void', $fileContent);
3835
}
3936

37+
public function test_make_command_without_attributes()
38+
{
39+
$filepath = $this->filepath('2015_10_21_072800_awesome_operation.php');
40+
File::delete($filepath);
41+
42+
// create operation file with essential flag
43+
$this->artisan('operations:make AwesomeOperation --essential')->assertSuccessful();
44+
45+
$fileContent = File::get($filepath);
46+
47+
// file should contain method
48+
$this->assertStringContainsString('public function process(): void', $fileContent);
49+
50+
// file should not contain attributes
51+
$this->assertStringNotContainsString('protected bool $async = true;', $fileContent);
52+
$this->assertStringNotContainsString('protected string $queue = \'default\';', $fileContent);
53+
$this->assertStringNotContainsString('protected ?string $tag = null;', $fileContent);
54+
}
55+
56+
public function test_make_command_without_attributes_shortcut()
57+
{
58+
$filepath = $this->filepath('2015_10_21_072800_awesome_operation.php');
59+
File::delete($filepath);
60+
61+
// create operation file with shortcut for essential flag
62+
$this->artisan('operations:make AwesomeOperation -e')->assertSuccessful();
63+
64+
$fileContent = File::get($filepath);
65+
66+
// file should contain method
67+
$this->assertStringContainsString('public function process(): void', $fileContent);
68+
69+
// file should not contain attributes
70+
$this->assertStringNotContainsString('protected bool $async = true;', $fileContent);
71+
$this->assertStringNotContainsString('protected string $queue = \'default\';', $fileContent);
72+
$this->assertStringNotContainsString('protected ?string $tag = null;', $fileContent);
73+
}
74+
4075
/** @test */
4176
public function test_the_whole_command_process()
4277
{

0 commit comments

Comments
 (0)