mercredi 13 septembre 2017

Add global logs in app - what about SOLID

I have php app, it isn't compilant with SOLID principles, but whole team tries refactore code on changes. I must add global logs (stored in one of databases), saved on creations on updates on models. That models don't use ORM. First solution: create static logger and call after operation on model:

public function save(ObjectEntity $entity)
{
    // Some code to prepare entity
    $this->insert($entity);

    Logger::saveLog('Object has been saved');

    // Or maybe better - separate classes for logs with interface 
    Logger::save(new LogObjectEntitySave());
}

But... is it correct? I think is also not compilant with SOLID and would like not make new mess in current. Where should I add something like this - on models, or maybe on controllers after calling model saving:

public function saveAction()
{
    // Controller code here

    $model->save($objectEntity);
    Logger::save(new LogObjectEntitySave());
}

But there is question: what about one action to save and also update data in model (I add new element when I don't have existing id)? If/else and two log classes.. still looks bad. No idea how should it be right.

Aucun commentaire:

Enregistrer un commentaire