{"id":15062502,"url":"https://github.com/defstudio/actions","last_synced_at":"2025-04-10T14:21:57.595Z","repository":{"id":43932331,"uuid":"431039366","full_name":"defstudio/actions","owner":"defstudio","description":"Helpers methods for laravel actions","archived":false,"fork":false,"pushed_at":"2024-05-21T08:25:35.000Z","size":89,"stargazers_count":11,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-18T07:21:44.085Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/defstudio.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":".github/CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":".github/SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"defstudio"}},"created_at":"2021-11-23T09:34:08.000Z","updated_at":"2024-05-21T08:25:39.000Z","dependencies_parsed_at":"2024-05-21T08:47:48.102Z","dependency_job_id":"b17cf244-5f78-4c0b-a447-7f3ab99daf3c","html_url":"https://github.com/defstudio/actions","commit_stats":{"total_commits":95,"total_committers":2,"mean_commits":47.5,"dds":0.2315789473684211,"last_synced_commit":"b28d3c908a64e3a77e4b9d0654d47f073971b6e8"},"previous_names":[],"tags_count":20,"template":false,"template_full_name":"spatie/package-skeleton-laravel","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/defstudio%2Factions","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/defstudio%2Factions/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/defstudio%2Factions/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/defstudio%2Factions/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/defstudio","download_url":"https://codeload.github.com/defstudio/actions/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248233935,"owners_count":21069493,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-09-24T23:41:41.181Z","updated_at":"2025-04-10T14:21:57.578Z","avatar_url":"https://github.com/defstudio.png","language":"PHP","funding_links":["https://github.com/sponsors/defstudio"],"categories":[],"sub_categories":[],"readme":"# Helpers methods for Laravel Actions\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/defstudio/actions.svg)](https://packagist.org/packages/defstudio/actions)\n[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/def-studio/actions/run-tests.yml?branch=main\u0026label=tests)](https://github.com/def-studio/actions/actions?query=workflow%3Arun-tests+branch%3Amain)\n[![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/def-studio/actions/php-cs-fixer.yml?branch=main\u0026label=code%20style)](https://github.com/def-studio/actions/actions?query=workflow%3A\"Check+%26+fix+styling\"+branch%3Amain)\n[![GitHub Static Analysis Action Status](https://img.shields.io/github/actions/workflow/status/def-studio/actions/phpstan.yml?branch=main\u0026label=phpstan)](https://github.com/def-studio/actions/actions?query=workflow%3Aphpstan+branch%3Amain)\n[![Total Downloads](https://img.shields.io/packagist/dt/defstudio/actions.svg)](https://packagist.org/packages/defstudio/actions)\n\nan opinionated lightweight package for creating Action classes\n\n## Installation\n\nYou can install the package via composer:\n\n```bash\ncomposer require defstudio/actions\n```\n\n## Usage\n\nAdd `use DefStudio\\Actions\\ActAsAction;` trait to your action class or extend `DefStudio\\Actions\\Action`\n\n(optional) add a dockblock to hint the static `run` method parameters and return types\n\n```php\n/**\n * @method static void run(Report|int $report)\n */\nclass DeleteReport\n{\n    use ActsAsAction;\n\n    public function handle(Report|int $report): void\n    {\n        if (is_int($report)) {\n            $report = Report::findOrFail($report);\n        }\n\n        DB::transaction(function () use ($report) {\n            $report-\u003edelete_data();\n            $report-\u003edelete();\n        });\n    }\n}\n\nclass DeleteReport extends \\DefStudio\\Actions\\Action\n{\n    public function handle(Report|int $report): bool\n    {\n        if (is_int($report)) {\n            $report = Report::findOrFail($report);\n        }\n\n        return DB::transaction(function () use ($report) {\n            $report-\u003edelete_data();\n            return $report-\u003edelete();\n        });\n    }\n}\n```\n\nUse the new methods:\n\n```php\n$result = DeleteReport::run($report-\u003eid); //true\n\n$result = DeleteReport::make()-\u003ehandle($report-\u003eid); //true\n```\n\n## Run multiple actions as once\n\nan action can be run multiple times by calling its `runMany()` method\n\n```php\n$results = DeleteReport::runMany($report1-\u003eid, $report2-\u003eid, $report3-\u003eid); //[true, false, true]\n```\n\neach run's result will be collected in an array and returned by `runMany()`\n\n_notes_\n\nif multiple parameters are required, they can be wrapped in an array (associative array keys will be treated as named arguments):\n\n```php\nclass{\n    use InjectsItself;\n \n    public function handle($name = 'guest', $title = 'Mr.'): string\n    {\n        return \"$title $name\";\n    }\n}\n\n$result = MyAwesomeAction::runMany(['Elizabeth', \"Ms.\"], ['Fabio'],  ['title' =\u003e 'Mrs.']);\n\n// $result = [\"Ms. Elizabeth\", \"Mr. Fabio\", \"Mrs. guest\"] \n```\n\n## Mockable actions\n\nAlso, you can define a mock for the action (it will be authomatically bound to the app container):\n\n```php\nFindTheAnswerToLifeTheUniverseAndEverything::mock(fn ($report_id) =\u003e 42);\n\nFindTheAnswerToLifeTheUniverseAndEverything::run() // 42\n```\n\nif you are interested in only mocking the return value, you can write:\n\n```php\nFindTheAnswerToLifeTheUniverseAndEverything::mock(42);\n```\n\nif your action has public methods other than `handle`, they can be mocked as well:\n\n```php\nMyWeirdAction::mock(handle: fn() =\u003e 5, handleForAdmin: fn() =\u003e 42);\n```\n\nwithout arguments, `mocks` returns a MockInterface instance ready to be used\n\n```php\nMyAction::mock()-\u003eshouldNotReceive('handle');\n```\n\na partial mock (i.e. for actions with more than a single method)\n\n```php\nBuildOrder::partial_mock(fromRequest: fn() =\u003e true);\n\n//this will not be mocked\nBuildOrder::make()-\u003efromJson($data);\n```\n\nalong with mocks, actions can also be _spied_:\n\n```php\n$spiedAction = MyAction::spy();\n\n$spiedAction-\u003ehandle();\n$spiedAction-\u003ehandle();\n\n$spiedAction-\u003eshouldHaveReceived()-\u003ehandle()-\u003etwice()\n```\n\n## Dispatchable actions\n\nAn action can be made _dispatchable_ as a job with the `ActsAsJob` trait (or extending the `Action` class)\n\na job can be created by calling the `job()` static method:\n\n```php\ndispatch(LongRunningAction::job($argument_1, $argument_2));\n```\n\nor can be dispatched with its dedicated methods:\n\n```php\nLongRunningAction::dispatch($argument_1, $argument_2);\n\nLongRunningAction::dispatchSync($argument_1, $argument_2);\n\nLongRunningAction::dispatchAfterResponse($argument_1, $argument_2);\n```\n\nThe action will be dispatched wrapped in a ActionJob decorator that will proxy properties as needed:\n\n```php\nuse DefStudio\\Actions\\Concerns\\ActsAsJob;\n\nclass LongRunningAction{\n    use ActsAsJob;\n    \n    public int $timeout = 2 * 60 * 60;\n    public int $tries = 4;\n    public array $backoff = [60, 120, 300, 600];\n    public string $queue = 'long-running';\n    \n    public function handle(){...}\n}\n```\n\n### Cleaning up after failed action job\n\nFailed action jobs can be handled by defining a `jobFailed()` method:\n\n```php\nclass LongRunningAction{\n    use ActsAsJob;\n       \n    public function handle(){..}\n    \n    public function jobFailed($exception)\n    {\n        $this-\u003ehandleFailure();\n    }\n    \n    private function handleFailure(){..}\n}\n```\n\n### Batches and Chains of action jobs\n\nSimilarly to the `runMany()` method, a new batch/chain of action jobs can be created starting from an array of parameters:\n\n```php\nMyAction::batch([$name1, $title1], [$name2, $title2])-\u003edispatch();\n\nMyAction::chain([$name1, $title1], [$name2, $title2])-\u003edispatch();\n```\n\n## Testing\n\n```bash\ncomposer test\n```\n\n## Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.\n\n## Contributing\n\nPlease see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.\n\n## Security Vulnerabilities\n\nPlease review [our security policy](../../security/policy) on how to report security vulnerabilities.\n\n## Credits\n\n- [Fabio Ivona](https://github.com/fabio-ivona)\n- [All Contributors](../../contributors)\n\nThis project was inspired and is built as an opinionated and simplified implementation of [Loris Leiva](https://github.com/lorisleiva)'s [Laravel Actions](https://laravelactions.com/). For a more powerful tool, you should take a look at it.\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdefstudio%2Factions","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdefstudio%2Factions","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdefstudio%2Factions/lists"}