Skip to content

Commit d3a8044

Browse files
authored
Merge pull request #10 from ruchit288/main
feat: Add pan:delete command with ID-specific deletion and test case.
2 parents 924245a + 0fecd38 commit d3a8044

File tree

5 files changed

+106
-0
lines changed

5 files changed

+106
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Pan\Adapters\Laravel\Console\Commands;
6+
7+
use Illuminate\Console\Command;
8+
use Pan\Contracts\AnalyticsRepository;
9+
10+
final class PanDeleteCommand extends Command
11+
{
12+
/**
13+
* The name and signature of the console command.
14+
*
15+
* @var string
16+
*/
17+
protected $signature = 'pan:delete {id : The ID of the analytic to delete}';
18+
19+
/**
20+
* The console command description.
21+
*
22+
* @var string
23+
*/
24+
protected $description = 'Delete analytics by ID.';
25+
26+
/**
27+
* Execute the console command.
28+
*/
29+
public function handle(AnalyticsRepository $repository): void
30+
{
31+
$id = (int) $this->argument('id');
32+
33+
if ($this->isInvalidId($id)) {
34+
$this->error('Analytic ID must be greater than 0.');
35+
36+
return;
37+
}
38+
39+
if ($repository->delete($id) !== 0) {
40+
$this->info('Analytic has been deleted.');
41+
} else {
42+
$this->error('Record not found or already deleted.');
43+
}
44+
}
45+
46+
/**
47+
* Check if the ID is invalid.
48+
*/
49+
private function isInvalidId(int $id): bool
50+
{
51+
return $id <= 0;
52+
}
53+
}

src/Adapters/Laravel/Providers/PanServiceProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Illuminate\Support\ServiceProvider;
1111
use Pan\Adapters\Laravel\Console\Commands\InstallPanCommand;
1212
use Pan\Adapters\Laravel\Console\Commands\PanCommand;
13+
use Pan\Adapters\Laravel\Console\Commands\PanDeleteCommand;
1314
use Pan\Adapters\Laravel\Console\Commands\PanFlushCommand;
1415
use Pan\Adapters\Laravel\Http\Controllers\EventController;
1516
use Pan\Adapters\Laravel\Http\Middleware\InjectJavascriptLibrary;
@@ -97,6 +98,7 @@ private function registerCommands(): void
9798
InstallPanCommand::class,
9899
PanCommand::class,
99100
PanFlushCommand::class,
101+
PanDeleteCommand::class,
100102
]);
101103
}
102104
}

src/Adapters/Laravel/Repositories/DatabaseAnalyticsRepository.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,12 @@ private function connection(): Connection
8585
{
8686
return $this->databaseManager->connection($this->config->getDatabaseConnection());
8787
}
88+
89+
/**
90+
* Delete a specific analytic by ID.
91+
*/
92+
public function delete(int $id): int
93+
{
94+
return DB::table('pan_analytics')->where('id', $id)->delete();
95+
}
8896
}

src/Contracts/AnalyticsRepository.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,9 @@ public function increment(string $name, EventType $event): void;
2828
* Flush all analytics.
2929
*/
3030
public function flush(): void;
31+
32+
/**
33+
* Delete a specific analytic by ID.
34+
*/
35+
public function delete(int $id): int;
3136
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
use Pan\Contracts\AnalyticsRepository;
4+
5+
beforeEach(function (): void {
6+
$this->repository = mock(AnalyticsRepository::class)->makePartial();
7+
app()->instance(AnalyticsRepository::class, $this->repository);
8+
});
9+
10+
it('deletes a specific analytic by ID', function (): void {
11+
$this->repository
12+
->expects('delete')
13+
->with(1)
14+
->once()
15+
->andReturn(1);
16+
17+
$this->artisan('pan:delete', ['id' => 1])
18+
->expectsOutput('Analytic has been deleted.')
19+
->assertExitCode(0);
20+
});
21+
22+
it('fails when no argument is provided', function (): void {
23+
$this->artisan('pan:delete')
24+
->expectsOutput('Not enough arguments (missing: "id").')
25+
->assertExitCode(1);
26+
});
27+
28+
it('handles non-existent analytic gracefully', function (): void {
29+
$this->repository
30+
->expects('delete')
31+
->with(26)
32+
->once()
33+
->andReturn(0);
34+
35+
$this->artisan('pan:delete', ['id' => 26])
36+
->expectsOutput('Record not found or already deleted.')
37+
->assertExitCode(0);
38+
});

0 commit comments

Comments
 (0)