Skip to content

Commit

Permalink
introducing the SubAction component in Porto.
Browse files Browse the repository at this point in the history
Tried to avoid that for long time, but seems now way to avoid it.
In some situations, where you have large Actions, you need to share some
sequence of Tasks (not only tasks) to avoid duplicate code between
both actions, they should be able to call SubAction
  • Loading branch information
Mahmoudz committed Apr 17, 2017
1 parent 19e175d commit 661a6c2
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 2 deletions.
6 changes: 4 additions & 2 deletions app/Ship/Parents/Actions/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
*/
abstract class Action
{

use CallableTrait;

/**
* @var
* Set automatically by the controller after calling an Action.
* Allows the Action to know which UI invoke it, to modify it's behaviour based on it, when needed.
*
* @var string
*/
protected $ui;

Expand Down
13 changes: 13 additions & 0 deletions app/Ship/Parents/Actions/SubAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace App\Ship\Parents\Actions;

/**
* Class Action.
*
* @author Mahmoud Zalt <[email protected]>
*/
abstract class SubAction extends Action
{

}
55 changes: 55 additions & 0 deletions docs/_docs/D.components/subactions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
---
title: "Sub Actions"
category: "Components"
order: 5
---

### Definition & Principles

Read from the [**Porto SAP Documentation (#SubActions)**](https://github.com/Mahmoudz/Porto#SubActions).

### Rules

- All SubActions MUST extend from `App\Ship\Parents\Actions\SubAction`.

### Folder Structure

```
- app
- Containers
- {container-name}
- Actions
- ValidateAddressSubAction.php
- BuildOrderSubAction.php
- ...
```

### Code Sample

**ValidateAddressSubAction User Action:**

```php
<?php

namespace App\Containers\Shipment\Actions;

use App\Containers\Recipient\Models\Recipient;
use App\Containers\Recipient\Tasks\UpdateRecipientTask;
use App\Containers\Shipment\Tasks\ValidateAddressWithEasyPostTask;
use App\Containers\Shipment\Tasks\ValidateAddressWithMelissaDataTask;
use App\Ship\Parents\Actions\SubAction;

class ValidateAddressSubAction extends SubAction
{
public function run(Recipient $recipient)
{
$hasValidAddress = true;

$easyPostResponse = $this->call(ValidateAddressWithEasyPostTask::class, [$recipient]);

...
}
}
```

**Every feature available for Actions, is also available in SubActions.**

0 comments on commit 661a6c2

Please sign in to comment.