jeudi 27 avril 2017

PHP - Design pattern for dynamic "validators"

I struggling with following problem:

  1. User send message via form
  2. Request is passed to controller
    (In controller)
  3. App checks if user is logged
  4. App checks if user has permission to send messages
  5. App prepares (filter/validate) data send from app
  6. Controller pass data to Service layer
    (In service)
  7. Service inserts data to database
  8. Service returns result to controller
    (In controller)
  9. App creates View Model and send result to user's browser

Problem
Users have status (it's kind of role but more detailed) and I have to check if user's status allows to specific actions.

For example:
I have "status-checkers":
- MessageChecker - class to check if user with provided status may send message
- LimitChecker - class to check if user with provided status didn't exceeded message limit
- SomethingElseChecker

I'm looking for a way (design pattern) to easily add/remove checker without modifying controller or service.
Let's say I have configuration array with these active checkers:

checker.config.php

return [
    'Namespace\Checker\MessageChecker',
    'Namespace\Checker\LimitChecker',
    'Namespace\Checker\FooChecker'
];

What next? Should I pass it to controller and check it on this level? Or to service and check it on this layer? What design pattern will be suitable for my problem?

Aucun commentaire:

Enregistrer un commentaire