Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement CreateNewFileForRoadyProjectAction for Rig v2.0.0 #241

Open
sevidmusic opened this issue May 6, 2024 · 0 comments
Open

Implement CreateNewFileForRoadyProjectAction for Rig v2.0.0 #241

sevidmusic opened this issue May 6, 2024 · 0 comments
Assignees
Labels
rigv2.0.0 For Rig v2.0.0

Comments

@sevidmusic
Copy link
Owner

sevidmusic commented May 6, 2024

Implement CreateNewFileForRoadyProjectAction for Rig v2.0.0

class CreateNewFileForRoadyProjectAction extends Action
{

    public function __construct(
        private Arguments $arguments,
        private MessageLog $messageLog,
        private RoadyProjectPathInfo $roadyProjectPathInfo,
        private string $relativePathToNewFile,
        private Name $fileName,
        private string $content,
    ) {
        parent::__construct($this->arguments, $this->messageLog);
    }

    public function do(): CreateNewFileForRoadyProjectAction
    {
        $this->attemptToCreateNewFile();
        $this->messageLog()->addMessage(
            match($this->actionStatus()) {
                ActionStatus::FAILED =>
                    CLIColorizer::applyFAILEDColor(
                        'Failed to create new file ' .
                        $this->pathToNewFile()
                    ),
                ActionStatus::SUCCEEDED =>
                    CLIColorizer::applySUCCEEDEDColor(
                        'Created new file ' .
                        $this->pathToNewFile()
                    ),
                ActionStatus::NOT_PROCESSED =>
                    CLIColorizer::applyNOT_PROCESSEDColor(
                        'Creation of new file '.
                        $this->pathToNewFile() .
                        'was not processed.'
                    ),
            }
        );
        return $this;
    }

    private function content(): string
    {
        return trim($this->content);
    }

    private function roadyProjectPathInfo(): RoadyProjectPathInfo
    {
        return $this->roadyProjectPathInfo;
    }

    private function attemptToCreateNewFile(): void
    {
        $this->actionStatus = match(
            !file_exists($this->pathToNewFile()) &&
            (file_put_contents($this->pathToNewFile(), $this->content()) > 0)
        ) {
            true => ActionStatus::SUCCEEDED,
            false => ActionStatus::FAILED,
        };
    }

    private function fileName(): Name
    {
        return $this->fileName;
    }

    private function pathToNewFile(): string
    {
        $safePathParts = $this->roadyProjectPathInfo()
                              ->pathToSafeTextCollection(
                                  $this->relativePathToNewFile
                              );
        $safePath = DIRECTORY_SEPARATOR;
        foreach($safePathParts->collection() as $safePathPart) {
            $safePath .= $safePathPart->__toString() .
                         DIRECTORY_SEPARATOR;
        }
        $safePath .= $this->fileName();
        return $this->roadyProjectPathInfo()
                    ->expectedPathToRoadyProjectsRootDirectory()
                    ->__toString() .
                    $safePath;
    }

}
@sevidmusic sevidmusic added the rigv2.0.0 For Rig v2.0.0 label May 6, 2024
@sevidmusic sevidmusic self-assigned this May 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rigv2.0.0 For Rig v2.0.0
Projects
Status: In Progress
Development

No branches or pull requests

1 participant