Skip to content

Instantly share code, notes, and snippets.

@zabaala
Created February 17, 2018 00:51
Show Gist options
  • Select an option

  • Save zabaala/73136ef70b8358bead25eb0127094a3e to your computer and use it in GitHub Desktop.

Select an option

Save zabaala/73136ef70b8358bead25eb0127094a3e to your computer and use it in GitHub Desktop.
<?php
//...
class FetchAllCouponsMethodObject
{
/**
* @var null|string
*/
private $filterableKeyword;
/**
* @var array
*/
private $orders;
/**
* @var Model
*/
private $eloquentModel;
/**
* @var int
*/
private $pageSize;
/**
* FetchAllCouponsMethodObject constructor.
*
* @param $eloquentModel
* @param $filterableKeyword
* @param array $orders
* @param $pageSize
*/
public function __construct($eloquentModel, $filterableKeyword, array $orders, $pageSize)
{
$this->filterableKeyword = $filterableKeyword;
$this->orders = $orders;
$this->eloquentModel = $eloquentModel;
$this->pageSize = $pageSize;
}
/**
* Handle method object.
*
* @return LengthAwarePaginator
*/
public function handle()
{
$query = $this->eloquentModel
->newQuery()
->select([
'coupons.*'
]);
if (! is_null($this->filterableKeyword)) {
$query->where(function (QueryBuilder $query) {
$query->where('coupons.name', 'like', '%' . $this->filterableKeyword . '%');
$query->orWhere('coupons.code', 'like', '%' . $this->filterableKeyword . '%');
$query->orWhere('coupons.description', 'like', '%' . $this->filterableKeyword . '%');
$query->orWhere('coupons.internal_description', 'like', '%' . $this->filterableKeyword . '%');
});
}
$query->leftJoin('plans', 'plans.id', '=', 'plan_id');
$query->orders = $this->orders;
$results = $query->paginate($this->pageSize);
$data = $results->map(function ($item) {
return (new CouponTransform())->transform($item);
});
return new LengthAwarePaginator($data, $results->total(), $this->pageSize, $results->currentPage());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment