Skip to content

Instantly share code, notes, and snippets.

@imanimen
Last active December 29, 2024 05:58
Show Gist options
  • Select an option

  • Save imanimen/9baed2917275f5b60be738e888a5bed1 to your computer and use it in GitHub Desktop.

Select an option

Save imanimen/9baed2917275f5b60be738e888a5bed1 to your computer and use it in GitHub Desktop.
Action base coding
<?php
namespace Modules\Pardakht\Classes;
class StartPayment
{
public const USER_TERMINALS = "get-user-terminals";
public function createPayment()
{
$user_terminals = user()->acceptors->firsts()->terminal; ❌ // instead of writing this in all of the source code
$user_terminals = Action::build(Actions::USER_TERMINALS, user()->id())->execute(); ✅ // use this to do it once
// each action must be registered on the service provider
}
}
_________________________________________________________________________________________________________
<?php
namespace Modules\Pardakht\Actions;
use Modules\...\Entities\Terminal;
use Modules\Core\Abstracts\ActionItem;
class GetUserTerminals extends ActionItem
{
public function logic()
{
$terminals = Terminal::with('acceptor')
->where('user_id', $this->getParam('user_id'))
->get();
// add resource for a cleaner version
return $terminals;
}
public function getValidation(): array
{
return
[
'user_id' => ['required', new IsEligibleOnPlatform]
];
}
}
________________________________________________________________________________________________
// How it should be registered on the ServiceProvider
Action::registerAction('pardakht', USER_TERMINALS, GetUserTerminals::class );
________________________________________________________________________________________________
<?php
namespace Modules\Core\Entities;
use Faker\Provider\Uuid;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Str;
use Modules\Core\Exception\ActionModuleActionNotFoundException;
use Modules\Core\Exception\ActionModuleNotFoundException;
class Action
{
use Dispatchable , InteractsWithSockets , SerializesModels;
public static $namespaces = [];
/**
* @var string
*/
private $module;
/**
* @var string
*/
private $action;
private $params;
public function __construct( string $module , string $action , array $params )
{
$this->module = $module;
$this->action = $action;
$this->params = $params;
}
/**
* @param string $namespace
* @param string $actionKey
* @param string $actionObject
*/
public static function registerAction( string $namespace , string $actionKey , string $actionObject ): void
{
self::$namespaces[ $namespace ][ $actionKey ] = $actionObject;
}
/**
* @param string $module
* @param string $action
* @param array $params
*
* @return ActionCollection
* @return ActionCollection
* @throws ActionModuleActionNotFoundException
*
* @throws ActionModuleNotFoundException
*/
public static function build( string $module , string $action , array $params = [] ): ActionCollection
{
if ( !isset( self::getNamespaces()[ $module ] ) )
{
throw new ActionModuleNotFoundException( 'Unregistered module ' . $module . ' in Actions' );
}
if ( !isset( self::getNamespaces()[ $module ] [ $action ] ) )
{
throw new ActionModuleActionNotFoundException( 'Unregistered action ' . $action . ' in module ' . $module . ' ' . self::suggestOtherActions( $module , $action ) );
}
$class = new self( $module , $action , $params );
$class_fqdn = self::getNamespaces()[ $class->getModule() ][ $class->getAction() ];
$action_class = ( new $class_fqdn( $class->getParams() ) );
event( $action_class );
return new ActionCollection( $action_class , $class->getParams() , Uuid::uuid() );
}
/**
* @return array
*/
public static function getNamespaces(): array
{
return self::$namespaces;
}
/**
* @param string $action
*
* @return string
*/
public static function suggestOtherActions( $module , $action )
{
$seperated = explode( '-' , $action );
$actions = self::getNamespaces()[ $module ];
$result = [];
foreach ( $seperated as $query )
{
foreach ( $actions as $key => $_action )
{
$exists = Str::contains( $action , $key );
if ( $exists )
{
$result[] = $key;
}
}
}
if ( count( $result ) )
{
return ', did you mean ' . implode( ', ' , array_unique( $result ) ) . " ?";
}
return '';
}
/**
* @return string
*/
public function getModule(): string
{
return $this->module;
}
/**
* @param string $module
*/
public function setModule( string $module ): void
{
$this->module = $module;
}
/**
* @return string
*/
public function getAction(): string
{
return $this->action;
}
/**
* @param string $action
*/
public function setAction( string $action ): void
{
$this->action = $action;
}
/**
* @return array
*/
public function getParams(): array
{
return $this->params;
}
/**
* @param array $params
*/
public function setParams( array $params ): void
{
$this->params = $params;
}
}
________________________________________________________________________________________________
<?php
namespace Modules\Core\Entities;
use Modules\Core\Events\ScheduleActionEvent;
class ActionCollection
{
private $class;
private $arguments;
private $hash;
/**
* @param $class
* @param $arguments
* @param $hash
*/
public function __construct( $class , $arguments , $hash )
{
$this->class = $class;
$this->arguments = $arguments;
$this->hash = $hash;
}
/**
* @return mixed
*/
public function getHash()
{
return $this->hash;
}
/**
* @param bool $withExecution
*
* @return array
*/
public function getOverView( bool $withExecution )
{
$data = [
'class' => $this->getClassFQNS() ,
'args' => $this->getArguments() ,
];
if ( $withExecution )
{
$data[ 'result' ] = $this->execute();
}
return $data;
}
/**
* @return string
*/
public function getClassFQNS(): string
{
return get_class( $this->class );
}
/**
* @return mixed
*/
public function getArguments()
{
return $this->arguments;
}
/**
* @return mixed
*/
public function execute()
{
return $this->getClass()->execute();
}
/**
* @return array|null
*/
public function schedule()
{
return event( new ScheduleActionEvent( $this ));
// return $this->getClass()->execute();
}
/**
* @return mixed
*/
public function getClass()
{
return $this->class;
}
/**
* @param mixed $class
*/
private function setClass( $class ): void
{
$this->class = $class;
}
/**
* @param mixed $arguments
*/
private function setArguments( $arguments ): void
{
$this->arguments = $arguments;
}
/**
* @param mixed $hash
*/
private function setHash( $hash ): void
{
$this->hash = $hash;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment