Skip to content

Commit

Permalink
refactor: remove StateDirectoryManager and methods
Browse files Browse the repository at this point in the history
  • Loading branch information
vinceAmstoutz committed Oct 9, 2024
1 parent cb03f73 commit 5d39b7b
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 67 deletions.
8 changes: 4 additions & 4 deletions src/Laravel/Console/Maker/AbstractMakeStateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
namespace ApiPlatform\Laravel\Console\Maker;

use ApiPlatform\Laravel\Console\Maker\Utils\AppServiceProviderTagger;
use ApiPlatform\Laravel\Console\Maker\Utils\StateDirectoryManager;
use ApiPlatform\Laravel\Console\Maker\Utils\StateTemplateGenerator;
use ApiPlatform\Laravel\Console\Maker\Utils\StateTypeEnum;
use ApiPlatform\Laravel\Console\Maker\Utils\SuccessMessageTrait;
Expand All @@ -30,7 +29,6 @@ public function __construct(
private readonly Filesystem $filesystem,
private readonly StateTemplateGenerator $stateTemplateGenerator,
private readonly AppServiceProviderTagger $appServiceProviderTagger,
private readonly StateDirectoryManager $stateDirectoryManager,
) {
parent::__construct();
}
Expand All @@ -41,10 +39,12 @@ public function __construct(
public function handle(): int
{
$stateName = $this->askForStateName();
$directoryPath = $this->stateDirectoryManager->ensureStateDirectoryExists();

$directoryPath = base_path('src/State/');
$this->filesystem->ensureDirectoryExists($directoryPath);

$filePath = $this->stateTemplateGenerator->getFilePath($directoryPath, $stateName);
if ($this->stateTemplateGenerator->isFileExists($filePath)) {
if ($this->filesystem->exists($filePath)) {
$this->error(\sprintf('[ERROR] The file "%s" can\'t be generated because it already exists.', $filePath));

return self::FAILURE;
Expand Down
27 changes: 6 additions & 21 deletions src/Laravel/Console/Maker/Utils/AppServiceProviderTagger.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,17 @@ public function __construct(private Filesystem $filesystem)
public function addTagToServiceProvider(string $providerName, StateTypeEnum $stateTypeEnum): void
{
$appServiceProviderPath = app_path(self::APP_SERVICE_PROVIDER_PATH);
$this->ensureServiceProviderExists($appServiceProviderPath);
if (!$this->filesystem->exists($appServiceProviderPath)) {
throw new \RuntimeException('The AppServiceProvider is missing!');
}

$serviceProviderContent = $this->filesystem->get($appServiceProviderPath);

$this->addUseStatement($serviceProviderContent, $this->getStateTypeStatement($stateTypeEnum));
$this->addUseStatement($serviceProviderContent, $this->geStateNamespace($providerName));
$this->addUseStatement($serviceProviderContent, \sprintf('use App\\State\\%s;', $providerName));
$this->addTag($serviceProviderContent, $providerName, $appServiceProviderPath, $stateTypeEnum);
}

private function ensureServiceProviderExists(string $path): void
{
if (!$this->filesystem->exists($path)) {
throw new \RuntimeException('The AppServiceProvider is missing!');
}
}

private function addUseStatement(string &$content, string $useStatement): void
{
if (!str_contains($content, $useStatement)) {
Expand All @@ -67,7 +62,7 @@ private function addUseStatement(string &$content, string $useStatement): void
private function addTag(string &$content, string $stateName, string $serviceProviderPath, StateTypeEnum $stateTypeEnum): void
{
$tagStatement = $this->formatTagStatement($stateName, $stateTypeEnum->name);
$tagStatement = \sprintf("\n\n\t\t\$this->app->tag(%s::class, %sInterface::class);", $stateName, $stateTypeEnum->name);
if (!str_contains($content, $tagStatement)) {
$content = preg_replace(
Expand All @@ -80,17 +75,7 @@ private function addTag(string &$content, string $stateName, string $serviceProv
}
}
private function formatTagStatement(string $stateName, string $interface): string
{
return \sprintf("\n\n\t\t\$this->app->tag(%s::class, %sInterface::class);", $stateName, $interface);
}
public function geStateNamespace(string $providerName): string
{
return \sprintf('use App\\State\\%s;', $providerName);
}
public function getStateTypeStatement(StateTypeEnum $stateTypeEnum): string
private function getStateTypeStatement(StateTypeEnum $stateTypeEnum): string
{
return match ($stateTypeEnum) {
StateTypeEnum::Provider => self::ITEM_PROVIDER_USE_STATEMENT,
Expand Down
31 changes: 0 additions & 31 deletions src/Laravel/Console/Maker/Utils/StateDirectoryManager.php

This file was deleted.

12 changes: 1 addition & 11 deletions src/Laravel/Console/Maker/Utils/StateTemplateGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@ public function getFilePath(string $directoryPath, string $stateFileName): strin
return $directoryPath.$stateFileName.'.php';
}

public function isFileExists(string $filePath): bool
{
return $this->filesystem->exists($filePath);
}

/**
* @throws FileNotFoundException
*/
Expand All @@ -40,7 +35,7 @@ public function generate(string $pathLink, string $stateClassName, StateTypeEnum
$namespace = 'App\\State';
$template = $this->loadTemplate($stateTypeEnum);

$content = $this->replacePlaceholders($template, [
$content = strtr($template, [
'{{ namespace }}' => $namespace,
'{{ class_name }}' => $stateClassName,
]);
Expand All @@ -62,9 +57,4 @@ private function loadTemplate(StateTypeEnum $stateTypeEnum): string

return $this->filesystem->get($templatePath);
}

private function replacePlaceholders(string $template, array $variables): string
{
return strtr($template, $variables);
}
}

0 comments on commit 5d39b7b

Please sign in to comment.