Skip to content

Commit

Permalink
Package ahoy
Browse files Browse the repository at this point in the history
  • Loading branch information
JustSteveKing committed Apr 27, 2023
0 parents commit a9bf4df
Show file tree
Hide file tree
Showing 21 changed files with 521 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
root = true

[*]
charset = utf-8
indent_size = 4
indent_style = space
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false

[*.{yml,yaml,json}]
indent_size = 2
14 changes: 14 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Path-based git attributes
# https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html

# Ignore all test and documentation with "export-ignore".
/.github export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/phpunit.xml.dist export-ignore
/tests export-ignore
/.editorconfig export-ignore
/.php_cs.dist export-ignore
/psalm.xml export-ignore
/psalm.xml.dist export-ignore
/testbench.yaml export-ignore
1 change: 1 addition & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
github: JustSteveKing
43 changes: 43 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: "Run Tests"

on:
- push
- pull_request
- workflow_dispatch

jobs:
test:

runs-on: ubuntu-latest
strategy:
fail-fast: true
matrix:
php: [8.2]
dependency-version: [prefer-stable]

name: PHP ${{ matrix.php }} - ${{ matrix.dependency-version }}

steps:

- name: Checkout Code
uses: actions/checkout@v3

- name: Cache Dependencies
uses: actions/cache@v3
with:
path: ~/.composer/cache/files
key: dependencies-laravel-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }}

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: curl, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, iconv
coverage: none

- name: Install Dependencies
run: |
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction
- name: Execute Tests
run: ./vendor/bin/pest
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/.idea
.phpunit.result.cache
composer.lock
/build
/coverage
/docs
/vendor
/node_modules
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Steve McDougall. <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
121 changes: 121 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
# Laravel Business Process

<!-- BADGES_START -->
[![Latest Version][badge-release]][packagist]
[![Software License][badge-license]][license]
[![Run Tests](https://github.com/JustSteveKing/laravel-business-process/actions/workflows/tests.yml/badge.svg)](https://github.com/JustSteveKing/laravel-business-process/actions/workflows/tests.yml)
[![PHP Version][badge-php]][php]
[![Total Downloads][badge-downloads]][downloads]

[badge-release]: https://img.shields.io/packagist/v/juststeveking/laravel-business-process.svg?style=flat-square&label=release
[badge-license]: https://img.shields.io/packagist/l/juststeveking/laravel-business-process.svg?style=flat-square
[badge-php]: https://img.shields.io/packagist/php-v/juststeveking/laravel-business-process.svg?style=flat-square
[badge-downloads]: https://img.shields.io/packagist/dt/juststeveking/laravel-business-process.svg?style=flat-square&colorB=mediumvioletred

[packagist]: https://packagist.org/packages/juststeveking/laravel-business-process
[license]: https://github.com/juststeveking/laravel-business-process/blob/main/LICENSE
[php]: https://php.net
[downloads]: https://packagist.org/packages/juststeveking/laravel-business-process
<!-- BADGES_END -->

Laravel Business Process is a simple and clean way to run business process using a Laravel Pipeline, in a structured and type-safe way.

## Installation

```shell
composer require juststeveking/laravel-business-process
```

## Usage

To get started with this package, all you need to do is create a new process class:

```php
use JustSteveKing\BusinessProcess\Process;

final class PurchaseProduct extends Process
{
protected array $tasks = [
CheckStockLevel::class,
ProcessOrder::class,
DecreaseStockLevel::class,
NotifyWarehouse::class,
EmailCustomer::class,
];
}
```

Our process class registers the tasks that need to be completed for this process to run, each task must implement the `TaskContract` interface that comes with this package.

```php
use JustSteveKing\BusinessProcess\Contracts\TaskContract;

final class CheckStockLevel implements TaskContract
{
public function __invoke(ProcessPayload $payload, Closure $next): mixed
{
// perform your logic here with the passed in payload.

return $next($payload);
}
}
```

Your tasks are standard classes that the `Pipeline` class from Laravel would expect.

The payload is a class that implements `ProcessPayload` interface, signalling that it is a payload for a process. They are simple plain old PHP classes with no requirements to add methods.

```php
use JustSteveKing\BusinessProcess\Contracts\ProcessPayload;

final class PurchaseProductPayload implements ProcessPayload
{
public function __construct(
// add whatever public properties you need here
public int $product,
public int $user,
public int $order,
) {}
}
```

Finally, we can call this process within our controller/job/cli wherever you need to.

```php
final class PurchaseController
{
public function __construct(
private readonly PurchaseProduct $process,
) {}

public function __invoke(PurchaseRequest $request, int $product): JsonResponse
{
try {
$this->process->run(
payload: $request->payload(),
);
} catch (Throwable $exception) {
// Handle exception
}

// return response.
}
}
```
## Testing

To run the test:

```bash
composer run test
```

## Credits

- [Steve McDougall](https://github.com/JustSteveKing)
- [All Contributors](../../contributors)

## LICENSE

The MIT License (MIT). Please see [License File](./LICENSE) for more information.

59 changes: 59 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
"name": "juststeveking/laravel-business-process",
"description": "Laravel Business Process is a simple and clean way to run business process using a Laravel Pipeline, in a structured and type-safe way.",
"license": "MIT",
"authors": [
{
"role": "Developer",
"name": "Steve McDougall",
"email": "[email protected]",
"homepage": "https://www.juststeveking.uk/"
}
],
"autoload": {
"psr-4": {
"JustSteveKing\\BusinessProcess\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"JustSteveKing\\BusinessProcess\\Tests\\": "tests/"
}
},
"require": {
"php": "^8.2"
},
"require-dev": {
"laravel/pint": "^1.9",
"orchestra/testbench": "^8.5",
"pestphp/pest": "^2.5.2",
"phpstan/phpstan": "^1.10.14"
},
"scripts": {
"pint": [
"./vendor/bin/pint"
],
"stan": [
"./vendor/bin/phpstan analyse"
],
"test": [
"./vendor/bin/pest"
]
},
"extra": {
"laravel": {
"providers": [
"JustSteveKing\\BusinessProcess\\Providers\\PackageServiceProvider"
]
}
},
"config": {
"sort-packages": true,
"optimize-autoloader": true,
"allow-plugins": {
"pestphp/pest-plugin": true
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
12 changes: 12 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
parameters:

paths:
- src/

level: 9

ignoreErrors:

excludePaths:

checkMissingIterableValueType: false
15 changes: 15 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.1/phpunit.xsd" bootstrap="vendor/autoload.php" colors="true">
<testsuites>
<testsuite name="Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<coverage/>
<source>
<include>
<directory suffix=".php">./app</directory>
<directory suffix=".php">./src</directory>
</include>
</source>
</phpunit>
42 changes: 42 additions & 0 deletions pint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"preset": "psr12",
"rules": {
"align_multiline_comment": true,
"array_indentation": true,
"array_syntax": true,
"blank_line_after_namespace": true,
"blank_line_after_opening_tag": true,
"combine_consecutive_issets": true,
"combine_consecutive_unsets": true,
"concat_space": true,
"declare_parentheses": true,
"declare_strict_types": true,
"explicit_string_variable": true,
"final_class": true,
"final_internal_class": false,
"fully_qualified_strict_types": true,
"global_namespace_import": {
"import_classes": true,
"import_constants": true,
"import_functions": true
},
"is_null": true,
"lambda_not_used_import": true,
"logical_operators": true,
"mb_str_functions": true,
"method_chaining_indentation": true,
"modernize_strpos": true,
"new_with_braces": true,
"no_empty_comment": true,
"not_operator_with_space": true,
"ordered_traits": true,
"protected_to_private": true,
"simplified_if_return": true,
"strict_comparison": true,
"ternary_to_null_coalescing": true,
"trim_array_spaces": true,
"use_arrow_functions": true,
"void_return": true,
"yoda_style": true
}
}
9 changes: 9 additions & 0 deletions src/Contracts/ProcessPayload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace JustSteveKing\BusinessProcess\Contracts;

interface ProcessPayload
{
}
12 changes: 12 additions & 0 deletions src/Contracts/TaskContract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace JustSteveKing\BusinessProcess\Contracts;

use Closure;

interface TaskContract
{
public function __invoke(ProcessPayload $payload, Closure $next): mixed;
}
Loading

0 comments on commit a9bf4df

Please sign in to comment.