Skip to content

Instantly share code, notes, and snippets.

@zabaala
Created February 16, 2018 23:38
Show Gist options
  • Save zabaala/9f87c43d8f89c91f6a9aaae897450c28 to your computer and use it in GitHub Desktop.
Save zabaala/9f87c43d8f89c91f6a9aaae897450c28 to your computer and use it in GitHub Desktop.
<?php
// ...
class DbCouponRepository implements CouponRepositoryInterface
{
/**
* @const int
*/
const PAGE_SIZE = 25;
/**
* @var CouponEloquentModel
*/
private $eloquentModel;
/**
* CouponRepository constructor.
*/
public function __construct()
{
$this->eloquentModel = new CouponEloquentModel();
}
/**
* Get all coupons.
*
* @param null $filterableKeyword
* @param array $orders
* @return LengthAwarePaginator
*/
public function getAllCoupons(
$filterableKeyword = null,
array $orders = [
[
'column' => 'name',
'direction' => 'asc'
]
]
)
{
$query = $this->eloquentModel
->newQuery()
->select([
'coupons.*'
]);
if (! is_null($this->filterableKeyword)) {
$query->where(function (QueryBuilder $query) {
$query->where('coupons.name', 'like', '%' . $filterableKeyword . '%');
$query->orWhere('coupons.code', 'like', '%' . $filterableKeyword . '%');
$query->orWhere('coupons.description', 'like', '%' . $filterableKeyword . '%');
$query->orWhere('coupons.internal_description', 'like', '%' . $filterableKeyword . '%');
});
}
$query->leftJoin('plans', 'plans.id', '=', 'coupons.plan_id');
$query->orders = $this->orders;
$results = $query->paginate(self::PAGE_SIZE);
$data = $results->map(function ($item) {
return (new CouponTransform())->transform($item);
});
return new LengthAwarePaginator($data, $results->total(), self::PAGE_SIZE, $results->currentPage());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment