Skip to content
/ chain Public

Kidding with chain of responsibility design pattern

License

Notifications You must be signed in to change notification settings

phlllpe/chain

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CHAIN OF RESPONSABILITY


Scrutinizer Code Quality Code Coverage Build Status Code Intelligence Status


HOW TO INSTALL

composer req phlllpe/chain

USAGE

  • Create a Handlers, example:
namespace Any\Handler;

use Chain\AbstractHandler;
use Chain\ContextInterface;

class MyHandler extends AbstractHandler 
{
    public function handle(ContextInterface $context)
    {
        // TO DO ANY ACTION HERE, WITH ANY TEST
        if ($context->get('any') == 'HERE') {
            $context->set('myClass', static::class);
        }
        
        return parent::handle($context);
    }
}
  • Define a sequence to setup/analyse your context with your handlers;
namespace Any\Service;

use Chain\Context;
use Any\Handler\{
    MainHandler,
    AddressHandler,
    BudgetHandler,
    FamilyHandler
};

class MyService
{

    public function __invoke()
    {
        $context = new Context();
        $context->set('name', 'Philipe Fernandes');
        
        $mainHandler = new MainHandler();
        $addressHandler = new AddressHandler();
        $budgetHandler = new BudgetHandler();
        $familyHandler = new FamilyHandler();
    
        $mainHandler
            ->setNext($addressHandler)
            ->setNext($budgetHandler)
            ->setNext($familyHandler)
            
        (new Runner())->run($mainHandler, $context);
    }
}

About

Kidding with chain of responsibility design pattern

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages