diff --git a/src/FileGenerators/SyncClassGenerator.php b/src/FileGenerators/SyncClassGenerator.php index e833595..7ae6fb1 100644 --- a/src/FileGenerators/SyncClassGenerator.php +++ b/src/FileGenerators/SyncClassGenerator.php @@ -4,6 +4,7 @@ use ApiClients\Foundation\Hydrator\CommandBus\Command\BuildAsyncFromSyncCommand; use ApiClients\Tools\ResourceGenerator\FileGeneratorInterface; +use Doctrine\Common\Inflector\Inflector; use PhpParser\BuilderFactory; use PhpParser\Node; @@ -18,12 +19,14 @@ public function generate(): Node { $classChunks = explode('\\', $this->yaml['class']); $className = array_pop($classChunks); + $interfaceName = $className . 'Interface'; $namespace = $this->yaml['src']['namespace'] . '\\' . static::NAMESPACE; if (count($classChunks) > 0) { $namespace .= '\\' . implode('\\', $classChunks); $namespace = str_replace('\\\\', '\\', $namespace); } $baseClass = $this->yaml['src']['namespace'] . '\\' . $this->yaml['class']; + $interfaceFQName = $this->yaml['src']['namespace'] . '\\' . $this->yaml['class'] . 'Interface'; $factory = new BuilderFactory(); @@ -40,16 +43,45 @@ public function generate(): Node 'wait', [ new Node\Expr\MethodCall( - new Node\Expr\Variable('this'), - 'handleCommand', + new Node\Expr\MethodCall( + new Node\Expr\Variable('this'), + 'handleCommand', + [ + new Node\Expr\New_( + new Node\Name('BuildAsyncFromSyncCommand'), + [ + new Node\Expr\ClassConstFetch( + new Node\Name('self'), + 'HYDRATE_CLASS' + ), + new Node\Expr\Variable('this'), + ] + ), + ] + ), + 'then', [ - new Node\Expr\New_( - new Node\Name('BuildAsyncFromSyncCommand'), + new Node\Expr\Closure( [ - new Node\Scalar\String_($this->yaml['class']), - new Node\Expr\Variable('this'), + 'params' => [ + new Node\Param( + Inflector::camelize($className), + null, + $interfaceName + ) + ], + 'stmts' => [ + new Node\Stmt\Return_( + new Node\Expr\MethodCall( + new Node\Expr\Variable( + Inflector::camelize($className) + ), + 'refresh' + ) + ) + ], ] - ), + ) ] ), ] @@ -60,6 +92,7 @@ public function generate(): Node return $factory->namespace($namespace) ->addStmt($factory->use(BuildAsyncFromSyncCommand::class)) ->addStmt($factory->use($baseClass)->as('Base' . $className)) + ->addStmt($factory->use($interfaceFQName)) ->addStmt($class) ->getNode() ; diff --git a/tests/expected-app/src/Resources/Sync/Project.php b/tests/expected-app/src/Resources/Sync/Project.php index ac01078..f72506a 100644 --- a/tests/expected-app/src/Resources/Sync/Project.php +++ b/tests/expected-app/src/Resources/Sync/Project.php @@ -4,11 +4,14 @@ use ApiClients\Foundation\Hydrator\CommandBus\Command\BuildAsyncFromSyncCommand; use Example\Client\Resource\Project as BaseProject; +use Example\Client\Resource\ProjectInterface; class Project extends BaseProject { public function refresh() : Project { - return $this->wait($this->handleCommand(new BuildAsyncFromSyncCommand('Project', $this))); + return $this->wait($this->handleCommand(new BuildAsyncFromSyncCommand(self::HYDRATE_CLASS, $this))->then(function (ProjectInterface $project) { + return $project->refresh(); + })); } } diff --git a/tests/expected-app/src/Resources/Sync/Project/Build.php b/tests/expected-app/src/Resources/Sync/Project/Build.php index a393ef0..1dd9e33 100644 --- a/tests/expected-app/src/Resources/Sync/Project/Build.php +++ b/tests/expected-app/src/Resources/Sync/Project/Build.php @@ -4,11 +4,14 @@ use ApiClients\Foundation\Hydrator\CommandBus\Command\BuildAsyncFromSyncCommand; use Example\Client\Resource\Project\Build as BaseBuild; +use Example\Client\Resource\Project\BuildInterface; class Build extends BaseBuild { public function refresh() : Build { - return $this->wait($this->handleCommand(new BuildAsyncFromSyncCommand('Project\\Build', $this))); + return $this->wait($this->handleCommand(new BuildAsyncFromSyncCommand(self::HYDRATE_CLASS, $this))->then(function (BuildInterface $build) { + return $build->refresh(); + })); } } diff --git a/tests/expected-app/src/Resources/Sync/Project/Config.php b/tests/expected-app/src/Resources/Sync/Project/Config.php index 36e909b..f40ad16 100644 --- a/tests/expected-app/src/Resources/Sync/Project/Config.php +++ b/tests/expected-app/src/Resources/Sync/Project/Config.php @@ -4,11 +4,14 @@ use ApiClients\Foundation\Hydrator\CommandBus\Command\BuildAsyncFromSyncCommand; use Example\Client\Resource\Project\Config as BaseConfig; +use Example\Client\Resource\Project\ConfigInterface; class Config extends BaseConfig { public function refresh() : Config { - return $this->wait($this->handleCommand(new BuildAsyncFromSyncCommand('Project\\Config', $this))); + return $this->wait($this->handleCommand(new BuildAsyncFromSyncCommand(self::HYDRATE_CLASS, $this))->then(function (ConfigInterface $config) { + return $config->refresh(); + })); } }