Skip to content

Instantly share code, notes, and snippets.

@daison12006013
Last active February 26, 2016 03:48
Show Gist options
  • Save daison12006013/58fce498be193e5fd52f to your computer and use it in GitHub Desktop.
Save daison12006013/58fce498be193e5fd52f to your computer and use it in GitHub Desktop.
Hello!
# Comms Supplier Clean
### Route and Controller Handling
**Route:**
```php
Route::get(
'{supplier}/{verb}',
'SupplierController@handle'
);
```
The above code simply handles the a single purpose for each supplier, our current, we have lot's of controllers, what if we have 50+ suppliers? you need to create a new controller again, that is not a good approach.
Nothing special above, but it is more cleaner and easy to maintain, rather than creating multiple routes for each supplier, that's crazy.
Let us say we have a URL like this:
- http://comms.vvv.com/av/search-vehicles
- http://comms.vvv.com/av/search-vehicle
Our current implementation right now, we have AVController.php, HZController.php BGController.php etc...
Mate, that is hard to maintain
**Controller:**
Let us say we have this controller that handles the above dispatched route
```php
namespace App\Controllers;
use Illuminate\Foundation\Support\Facades\Config;
class Controller extends Base Controller
{
public function handle($supplier, $verb)
{
# ... check if the $supplier exists on our config
$adapter_class = Config::get("suppliers.$supplier");
$inputs = Input::all();
# ... sanitize/validate the inputs
$instance = new $adapter_class($inputs);
# if $verb is 'search-vehicle', it will become
# 'search_vehicle'
$verb = str_replace('-', '_', $verb);
# let's apply another one, to transform it to camelCase
# if it is 'search_vehicle' it will become
# 'searchVehicle' now, which follows PSR-2 camelCase
# function declaration
$verb = camel_case($verb);
return $instance->{$verb}();
}
}
```
### Supplier Classes and Abstraction
Remember, we are passing the ``$supplier`` in the controller and we have the ``$verb`` there. The code below shows you how to handle those things.
Each supplier class must extend the abstract supplier, let's assume we have this abstract supplier.
```php
namespace Vroom\Suppliers;
abstract class Supplier
{
protected $inputs;
public function __construct($inputs)
{
$this->inputs = $inputs;
}
public function searchVehicles($inputs);
public function searchVehicle($input);
# ... other possible functions
}
```
let us say we have this array on our **config/suppliers.php** file
```php
return [
'av' => Vroom\Suppliers\Avis::class,
'bg' => Vroom\Suppliers\Budget::class,
# ... other supplier class
];
```
So, if we will analyze the controller we fired, we're getting the supplier's class.
```php
namespace Vroom\Suppliers;
class Avis extends Supplier
{
public function searchVehicles()
{
# ... process the variable class propert $this->inputs
# return an array with something like this
// return [
// 'success' => true,
// 'data' => [..],
// ];
}
public function searchVehicle()
{
# ... same above
}
}
```
We're passing the ``$inputs`` on abstract ``Supplier`` class constructor.
I'm planning to make this as **interface** before, but we're not creating a wrapper or an adapter. That is why i'm planning to make it now an **abstraction** design.
So, if ``Avis`` doesn't have ``searchVehicle()`` as an overriding function, you should get an error that you must declare that function.
### Reason?
Our existing classes were not that kind of maintainable and not single-purpose. Frankly, it doesn't follow the S.O.L.I.D. principles.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment