PHP Software Architecture Part 4: Refactoring

Written by sameernyaupane | Published 2017/10/31
Tech Story Tags: software-architecture | php-software-architecture | mvc | refactoring | php

TLDRvia the TL;DR App

All right. Let’s go over and refactor our code from part one.

Let’s once again look at our FlightController:

<a href="https://medium.com/media/efa0476effa57adf7b1e3b431a1cfda7/href">https://medium.com/media/efa0476effa57adf7b1e3b431a1cfda7/href</a>

First of all let’s move saveData() into a Repository class. It’s easy since all the API related code is in this method. For your own code base, if you do not have a separate method, we would have to do that first. This makes it easier to move it into a different layer.

Now this is our FlightRepository class:

<a href="https://medium.com/media/0a91d7e71d40d8dbd02cc9ac7712f843/href">https://medium.com/media/0a91d7e71d40d8dbd02cc9ac7712f843/href</a>

All we did was move the “saveData” method from the controller to its own separate class.

For the “log” method, we will move it into a Service class. This is because we may need to reuse this log functionality in another place too. So it is a wise choice to move it into a Service class.

This is how our FlightLogger Service will look like:

<a href="https://medium.com/media/1375b38385ae195d43ca63088c21e17a/href">https://medium.com/media/1375b38385ae195d43ca63088c21e17a/href</a>

Now, finally we move all our Flight API call logic into an Adapter class.

<a href="https://medium.com/media/beda1aae460f820a91f09640ef94ecf0/href">https://medium.com/media/beda1aae460f820a91f09640ef94ecf0/href</a>

Note that we are also injecting the FightLogger Service instance to this Flight Adapter. Laravel auto injects dependencies, so we won’t need to inject them manually.

Finally, this is how our Controller looks:

<a href="https://medium.com/media/5c8aafa16e275870e83ea17a2d52bf82/href">https://medium.com/media/5c8aafa16e275870e83ea17a2d52bf82/href</a>

As you can see, we are down to just one method now. Which is much better from our previous four methods for a single functionality. Now there is one more step to make this remaining method lean. Let’s introduce a Domain class to contain the remaining logic.

This is how the Domain class looks like:

<a href="https://medium.com/media/53ecac3818ab7618c9df1d07176222e4/href">https://medium.com/media/53ecac3818ab7618c9df1d07176222e4/href</a>

Now, let’s refactor our Controller once again.

This is how the FlightController looks:

<a href="https://medium.com/media/08c329915a50531ed043da80c0e9e05d/href">https://medium.com/media/08c329915a50531ed043da80c0e9e05d/href</a>

And, there you go. Our FlightController is very thin. And it does not contain any business/domain logic at all. We moved all our code into different layers. We used Domains, Repositories, Adapters and Services to get to the Extensible Architecture.

I hope you enjoyed the series. Please don’t forget to give some “claps” here on Medium. And please subscribe for future articles.

Happy coding! Cheers :)


Published by HackerNoon on 2017/10/31