V
V
vitaly_742019-11-03 11:37:15
Software design
vitaly_74, 2019-11-03 11:37:15

How not to build a facade, but write code that does not repeat itself?

Good afternoon, there is a facade in which there are 2 public methods that are 70% similar
, for example:

class facade
{
    
    public function __construct()
    {
        
    }

    public function run(){
        if ($this->card) {
            $transaction =
                new Transaction(
                    new Request( //generate Request (not send)
                        new Curl( //generate sign and curl params for request
                            new HeadersWithoutSign(), //generate headers for curl
                            new FieldsAsString( // generate fields for curl as string
                                new Fields( //generate fields for curl
                                    $this->card,
                                    'usd'
                                )
                            )
                        )
                    )
                ); //don't validate this transaction;
            new Logger($transaction);
            $this->billing($transaction);
            $this->redirect($transaction);
        } else {
            $this->redirect(new EmptyClass());
        }
    }
    
    public function notification(){
        $transaction =
            new ValidTransaction( //need Validate this transaction
                new Transaction(
                    new InputRequest()
                )
            );
        new Logger($transaction);
        $this->confirmOperation($transaction);
        $this->sendToCrm($transaction);
        new Answer($transaction);
    }
}

How can you solve the problem without using a facade, there is some limitation, facade methods can be used in several places in the project.
code must not be repeated.
The following decision comes to mind: 1. write
this "hill" everywhere
2. write some kind of controller - but then it does not differ too much from the facade.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
grinat, 2019-11-05
@grinat

Yes, this is not a facade, it is a controller, judging by the redirects and so on, and the di level is some kind of prohibitive. I would understand if it was FP. See how the guys make http clients - https://github.com/guzzle/guzzle , it's impossible to use your fake.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question