Skip to content

Commit da02fa8

Browse files
authored
Merge pull request #238 from cosmastech/feature/fix-command-dependency-injection
Allow container to resolve command dependencies via handle()
2 parents 9ed6dba + 545dc54 commit da02fa8

File tree

4 files changed

+19
-45
lines changed

4 files changed

+19
-45
lines changed

src/Console/LiapUrlCommand.php

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,25 +49,18 @@ class LiapUrlCommand extends Command
4949
private Collection $urlCollection;
5050

5151
/**
52-
* @inheritDoc
52+
* Execute the console command.
53+
*
54+
* @param UrlGenerator $urlGenerator
55+
* @param Collection $urlCollection
56+
* @return int
5357
*/
54-
public function __construct(UrlGenerator $urlGenerator, Collection $urlCollection)
58+
public function handle(UrlGenerator $urlGenerator, Collection $urlCollection): int
5559
{
56-
parent::__construct();
57-
5860
$this->urlGenerator = $urlGenerator;
5961

6062
$this->urlCollection = $urlCollection;
61-
}
62-
6363

64-
/**
65-
* Execute the console command.
66-
*
67-
* @return int
68-
*/
69-
public function handle(): int
70-
{
7164
$this->generateUrls();
7265

7366
$this->table(self::TABLE_HEADERS, $this->urlCollection->toArray());

src/Console/RequestTestNotificationCommand.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,17 @@ class RequestTestNotificationCommand extends Command
3232
*/
3333
private ServiceBuilder $serviceBuilder;
3434

35-
/**
36-
* @param ServiceBuilder $serviceBuilder
37-
*/
38-
public function __construct(ServiceBuilder $serviceBuilder)
39-
{
40-
parent::__construct();
41-
42-
$this->serviceBuilder = $serviceBuilder;
43-
}
44-
45-
4635
/**
4736
* Execute the console command
4837
*
38+
* @param ServiceBuilder $serviceBuilder
4939
* @return int
5040
* @throws JsonException
5141
*/
52-
public function handle(): int
42+
public function handle(ServiceBuilder $serviceBuilder): int
5343
{
44+
$this->serviceBuilder = $serviceBuilder;
45+
5446
try {
5547
$response = $this->buildService()->request();
5648

tests/Doubles/LiapTestProvider.php

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@
22

33
namespace Imdhemy\Purchases\Tests\Doubles;
44

5-
use Illuminate\Foundation\Application;
65
use Illuminate\Support\ServiceProvider;
7-
use Imdhemy\Purchases\Console\LiapUrlCommand;
8-
use Imdhemy\Purchases\Console\RequestTestNotificationCommand;
9-
use Imdhemy\Purchases\Console\UrlGenerator;
106
use Imdhemy\Purchases\Contracts\UrlGenerator as UrlGeneratorContract;
117
use Imdhemy\Purchases\Services\AppStoreTestNotificationServiceBuilder;
128

@@ -32,21 +28,14 @@ public function boot(): void
3228
*/
3329
public function register(): void
3430
{
35-
$this->app->when(LiapUrlCommand::class)
36-
->needs(UrlGeneratorContract::class)
37-
->give(function (Application $app) {
38-
$concrete = $app->runningUnitTests() ? UrlGeneratorDouble::class : UrlGenerator::class;
31+
$this->app->bind(
32+
UrlGeneratorContract::class,
33+
UrlGeneratorDouble::class
34+
);
3935

40-
return $app->make($concrete);
41-
});
42-
43-
$this->app->when(RequestTestNotificationCommand::class)
44-
->needs(AppStoreTestNotificationServiceBuilder::class)
45-
->give(function (Application $app) {
46-
$concrete = $app->runningUnitTests() ?
47-
AppStoreTestNotificationServiceBuilderDouble::class : AppStoreTestNotificationServiceBuilder::class;
48-
49-
return $app->make($concrete);
50-
});
36+
$this->app->bind(
37+
AppStoreTestNotificationServiceBuilder::class,
38+
AppStoreTestNotificationServiceBuilderDouble::class
39+
);
5140
}
5241
}

tests/Handlers/AbstractNotificationHandlerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use Illuminate\Http\Request;
77
use Illuminate\Validation\Factory;
88
use Illuminate\Validation\ValidationException;
9-
use Imdhemy\Purchases\Contracts\UrlGenerator;
9+
use Imdhemy\Purchases\Console\UrlGenerator;
1010
use Imdhemy\Purchases\Handlers\AbstractNotificationHandler;
1111
use Imdhemy\Purchases\Handlers\HandlerHelpers;
1212
use Imdhemy\Purchases\Tests\TestCase;

0 commit comments

Comments
 (0)