Skip to content

Instantly share code, notes, and snippets.

@VusalGhasanov
Last active January 3, 2022 06:32
Show Gist options
  • Save VusalGhasanov/175a212acc7ea178e33260a01c85672d to your computer and use it in GitHub Desktop.
Save VusalGhasanov/175a212acc7ea178e33260a01c85672d to your computer and use it in GitHub Desktop.
Customer store
<?php
namespace App\Http\Controller;
use App\Service\CustomerService;
use App\Requests\CustomerRequest;
class CustomerController extends Controller
{
/**
* service
*
* @var CustomerService
*/
protected $service;
/**
* __construct
*
* @param CustomerService $service
* @return void
*/
public function __construct(CustomerService $service)
{
$this->service = $service;
}
/**
* store data
*
* @param CustomerRequest $request
* @return array
*/
public function store(CustomerRequest $request): array
{
$payload = $request->validated();
return $this->service->store($payload);
}
}
<?php
namespace App\Repositories;
use DB;
class BranchGoodRepository
{
/**
* __construct
*
* @param string $tablename
* @return void
*/
public function __construct(string $tablename)
{
$this->tablename = $tablename;
}
/**
* store data
*
* @param array $payload
* @return bool
*/
public function store(array $payload): bool
{
return DB::table($this->tablename)->insert($payload);
}
}
<?php
namespace App\Repositories;
interface BranchGoodRepositoryInterface
{
/**
* store data
*
* @param array $payload
* @return bool
*/
public function store(array $payload): bool;
}
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class CustomerRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => ['required'],
'email' => ['required']
];
}
}
<?php
namespace App\Services;
use App\Repositories\CustomerRepositoryInterface;
use App\Jobs\SendMailToCustomerJob;
class CustomerService
{
/**
* repository
*
* @var CustomerRepositoryInterface
*/
protected $repository;
/**
* __construct
*
* @param CustomerRepositoryInterface $repository
* @return void
*/
public function __construct(CustomerRepositoryInterface $repository)
{
$this->repository = $repository;
}
/**
* store data
*
* @param array $payload
* @return array
*/
public function store(array $payload): array
{
$saved = $this->repository->store($payload);
// job for send email with queue.
SendMailToCustomerJob::dispatch($payload['email'], $payload);
return [
'saved' => $saved
]
}
}
1. Binded repository and interface in RepositoryServiceProvide =)
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use App\Mail\WelcomeNewCustomer;
use Mail;
class SendMailToCustomerJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* @var string
*/
private string $email;
/**
* @var array
*/
private array $data;
/**
* Create a new job instance.
*
* @param string $email
* @return void
*/
public function __construct(string $email, array $data)
{
$this->email = $email;
$this->data = $data;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
Mail::to($this->email)->send(new WelcomeNewCustomer($this->data));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment