Skip to content

Instantly share code, notes, and snippets.

@acidjazz
Created September 15, 2017 09:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acidjazz/948b4d5b0b1c00b2b21af8baa9bbf7f2 to your computer and use it in GitHub Desktop.
Save acidjazz/948b4d5b0b1c00b2b21af8baa9bbf7f2 to your computer and use it in GitHub Desktop.
service
<?php
namespace Everest\Services\ActiveShopper;
use Everest\Entities\Dealers\DealerInterface;
class ActiveShopper implements ActiveShopperInterface
{
private $dealer;
public function __construct(DealerInterface $dealer)
{
$this->dealer = $dealer;
}
public function getMakes(int $dealerId, int $stage = null, string $location = null)
{
}
public function getStage(int $dealerId, int $model = null, string $location = null)
{
}
protected function mapStage(int $stage)
{
if ($stage <= 1) {
return 'Familiarity';
}
if ($stage >= 2 && $stage < 4) {
return 'Consideration';
}
if ($stage === 5) {
'Purchase';
}
}
protected function parseLocation(string $location)
{
$location = trim(str_replace(', United States', '', $location));
$re = '/\d{5}$/';
$address = [
'zipcode' => null,
'city' => null,
'state' => null,
'state_abr' => null
];
if (preg_match($re, $location, $matches, PREG_OFFSET_CAPTURE, 0)) {
$address['zipcode'] = $matches[0][0];
return $address;
}
$explode = explode(',', $location);
foreach($explode as $i => $value) {
$explode[$i] = trim($value);
}
if (count($explode) > 1) {
$address['city'] = $explode[0];
$state = $explode[1];
} else {
$state = $explode[0];
}
if (strlen($state) === 2) {
$address['state_abr'] = $state;
} else {
$address['state'] = $state;
}
return $address;
}
}
<?php
namespace Everest\Http\Controllers;
use Everest\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Everest\Services\ActiveShopper\ActiveShopperInterface;
class ActiveShopperController extends Controller {
private $request;
private $activeShopper;
public function __construct(Request $request, ActiveShopperInterface $activeShopper)
{
$this->request = $request;
$this->activeShopper = $activeShopper;
}
public function makes(int $dealerId)
{
$input = $this->request->all();
$input['dealerId'] = $dealerId;
$this->request->replace($input);
$this->validate($this->request, [
'dealerId' => 'required|exists:dealers,id',
'stage' => 'integer|between:1,5',
'location' => 'string'
]);
$this->request->replace($input);
return $this->activeShopper->getMakes($dealerId);
}
}
<?php
namespace Everest\Providers;
use Everest\Entities\Dealers\DealerInterface;
use Everest\Services\ActiveShopper\ActiveShopper;
use Everest\Services\ActiveShopper\ActiveShopperInterface;
use Illuminate\Support\ServiceProvider;
class ActiveShopperServiceProvider extends ServiceProvider
{
public function boot()
{
}
/**
* Register any application services
*/
public function register()
{
$this->app->bind(ActiveShopperInterface::class, function ($app) {
$dealer = app(DealerInterface::class);
return new ActiveShopper($dealer);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment