1

During development with MVC I figured out this Architecture (based on Symfony Framework but I need to apply it more generally on MVC Pattern base software):

enter image description here

In other words each model is a Facade of my business Logic. Usually is in this Form:

<?php

//Namespace definition
class SomeModel
{
  public function __construct(Service1 $s, Service2 $s2)
  {
    //Setting services into private instance variables
  }

  public function someMethod()
  {
    $status=new StatusObj();
    //Implementation
    return $status;
  }

  public function anotherMethod()
  {
    $status=new StatusObj();
    //Implementation
    return $status;
  }
}

The StatusObj Implementation I usually use is this one: https://github.com/pc-magas/commonlibs/blob/master/src/Status/ReturnStatus.php

The reason I do this is because I want a unified way to handle errors and an easy way to fabricate Http Responses depending the case. Usually I use factory pattern In order to generate the response. eg: On example I use the

<?php
//Namespace definitions

class HttpResponseFactory
{
  public static function generateRestHttpResponse(StatusObj $s)
  {
    //We suppose that this class models my http reposne
    $response=new HttpResponse();

    if($s->isErr()) {//In Case an error occured
       $err_type=$s->getErrType();//Getting the error Type
       switch($err_type){

         case StatusObj::Type1:
           $response->setHttpStatus(500);
           break;

         case StatusObj::Type2:
           $response->setHttpStatus(500);
           break;

       }
    } else {
       $response->setHttpStatus(200);
    } 

    return $response
  }
}

So if we suppose that we have this controller

<?php
//Namespace definitions

class MyController extends SomeGenericControllerClass
{


   public function someMethod()
   {
      //We suppose that somehow we get the Modes via Depedency Injection Container
      $model= $this->getServiceFromDepenednyInjection('SomeModel'); 
      $status= $model->someMethod();

      return HttpResponseFactory::generateRestHttpResponse($status);
   }

   public function anotherMethod()
   {
      //We suppose that somehow we get the Modes via Depedency Injection Container
      $model= $this->getServiceFromDepenednyInjection('SomeModel'); 
      $status= $model->anotherMethod();

      return HttpResponseFactory::generateRestHttpResponse($status);
   }
}

With that way I try to have and as DRY way as possible for http response (and not only) handling. In case that exception has been thrown is gets handled on Model and the correct Status gets generated. Also Depending the http response type it May have more than one factories, also same applies in case we want to run some commands depending the framework.

But I want to know what caveats - mishaps this architecture mentioned above may have in terms of:

  • Code reusability and readability.
  • Maintainace of software.
  • Future Improvement end Extension with new features.

Would you suggest it this one to your boss?

1

The problem with this form is that the base class will end up knowing all flavors of error types and their handling. This abstraction will break really quick. You can also wait for the moment where some error will require attention from the user, the controller is preferably the place to look for control flow.

You are now hiding part of your control flow in a base class, not making things clearer. Instead of needing to classify your error to the correct type to get the response you want you could set it directly without all the indirection and the added need to look for commonality. A base class that knows about all it's derived classes is bad design.

If any, the base class should solve a problem. Perhaps create the problem first? Or that is what I usually suggest, let the patterns emerge as you refactor out duplication.

  • But for a specific type or error I need to generate a specific http response without the need for writing the same code all over the time which is the best way to do that? eg When a incorrect parameter is given then return an http response with http status code 400. I want a unified way to handle these stuff. – Dimitrios Desyllas Feb 11 '17 at 17:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.