Cleaner code in Laravel for factory pattern
<p>I have an application where I have to deal with multiple vendors - each having their different implementation. For example, let's say Payment Systems, there are many vendors and banks. </p>

<p>There are a few things common, like implementation process. In each case, I have to give a callback URL to them to give me a response. </p>

<p>For now, I have two vendors - VendorPay and VendorA.</p>

<p>I will have two routes: </p>

<pre><code>payment/callback/vendorpay
payment/callback/vendora
</code></pre>

<p>each of them call two methods in controller. </p>

<pre><code>processVendorpay and processVendora
</code></pre>

<p>Now, if I want to add, lets say, 15 more vendors like this, will I have to create methods everytime I add new vendor? Is there any cleaner solution for this?</p>

<p>My controller right now looks like this: </p>

<pre><code>class PaymentController extends BaseController
{
    protected $vendorpay_validator, $vendora_validator, $transaction, $transaction_log, $vendor_product, $vendor_transaction;

    public function __construct(VendorpayValidator $vendorpay_validator, VendorAValidator $vendora_validator, Transaction $transaction, TransactionLog $transaction_log, VendorProduct $vendor_product, VendorTransaction $vendor_transaction)
    {
        $this-&gt;vendorpay_validator = $vendorpay_validator;
        $this-&gt;vendora_validator = $vendora_validator;
        $this-&gt;transaction = $transaction;
        $this-&gt;transaction_log = $transaction_log;
        $this-&gt;vendor_product = $vendor_product;
        $this-&gt;vendor_transaction = $vendor_transaction;
    }
}
</code></pre>

<p>These four are the Model Repository Objects: <code>$transaction, $transaction_log, $vendor_product, $vendor_transaction</code></p>

<p>If I have to add more venodrs, it keeps on adding validator object here. What would be a much cleaner way to do it?</p>

<p>One of the solutions that I thought was - for multiple routes, I just create one method. Now I'll check the route in this method and call the Factory Object basis on that.</p>