Guest User

Untitled

a guest
Mar 6th, 2022
1,442
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.40 KB | None | 0 0
  1. // Route
  2. Route::name('user.')->prefix('user')->group(function () {
  3.     Route::get('/', 'UserController@index')->name('index');
  4. });
  5.  
  6. // Controller method
  7. public function index()
  8. {
  9.     $includes = $this->parseIncludes([
  10.         'profile',
  11.         'state',
  12.         'wallets'
  13.     ]);
  14.  
  15.     $from = request()->has('from') ? Carbon::parse(request()->query('from')) : null;
  16.     $to = request()->has('to') ? Carbon::parse(request()->query('to')) : null;
  17.  
  18.     $this->parseOrderBy($this->userRepository, self::ALLOWED_SORT);
  19.  
  20.     $users = $this->userRepository
  21.         ->with($includes)
  22.         ->paginate(request()->query('limit'), request()->query('offset'))
  23.         ->allRegisteredBetween($brand, $from, $to)->setPath(route('api.user.index'));
  24.  
  25.     return fractal($users, $this->userTransformer)->parseIncludes($includes);
  26. }
  27.  
  28. // User Transformer
  29. class UserTransformer extends TransformerAbstract
  30. {
  31.     /**
  32.      * List of resources possible to include
  33.      *
  34.      * @var array
  35.      */
  36.     protected $availableIncludes = [
  37.         'profile',
  38.         'state',
  39.         'wallet'
  40.     ];
  41.  
  42.     /**
  43.      * @var ProfileTransformer
  44.      */
  45.     protected $profileTransformer;
  46.  
  47.     /**
  48.      * @var UserStateTransformer
  49.      */
  50.     protected $userStateTransformer;
  51.  
  52.     /**
  53.      * @var WalletTransformer
  54.      */
  55.     protected $walletTransformer;
  56.  
  57.     /**
  58.      * Create a new user transformer instance.
  59.      *
  60.      * @param ProfileTransformer $profileTransformer
  61.      * @param UserStateTransformer $userStateTransformer
  62.      * @param WalletTransformer $walletTransformer
  63.      */
  64.     public function __construct(
  65.         ProfileTransformer $profileTransformer,
  66.         UserStateTransformer $userStateTransformer,
  67.         WalletTransformer $walletTransformer
  68.     ) {
  69.         $this->profileTransformer = $profileTransformer;
  70.         $this->userStateTransformer = $userStateTransformer;
  71.         $this->walletTransformer = $walletTransformer;
  72.     }
  73.  
  74.     /**
  75.      * A Fractal transformer.
  76.      *
  77.      * @return array
  78.      */
  79.     public function transform($user)
  80.     {
  81.         $visible = [];
  82.  
  83.         if (Gate::allows('user.view', $user) || request()->route()->hasParameter('email')) {
  84.             $visible[] = 'email';
  85.             $visible[] = 'ip_address_on_registration';
  86.         }
  87.  
  88.         return ($user) ? array_merge($user->makeVisible($visible)->toArray(), [
  89.             'referral_code' => Hashids::connection('referral')->encode($user->id)
  90.         ]) : [];
  91.     }
  92.  
  93.     /**
  94.      * Include profile.
  95.      *
  96.      * @return Item
  97.      */
  98.     public function includeProfile($user)
  99.     {
  100.         return $this->item($user->profile, $this->profileTransformer, false);
  101.     }
  102.  
  103.     /**
  104.      * Include state.
  105.      *
  106.      * @return Item
  107.      */
  108.     public function includeState($user)
  109.     {
  110.         return $this->item($user->state, $this->userStateTransformer, false);
  111.     }
  112.  
  113.     /**
  114.      * Include wallet.
  115.      *
  116.      * @return Collection
  117.      */
  118.     public function includeWallet($user)
  119.     {
  120.         return $this->collection($user->wallets, $this->walletTransformer, false);
  121.     }
  122. }
  123.  
  124.  
  125. // User Repository method, user repository extends AppRepository which has `with`, `paginate` and `execute` methods
  126. public function allRegisteredBetween(Brand $brand = null, Carbon $from = null, Carbon $to = null)
  127. {
  128.     $query = User::query();
  129.  
  130.     if ($brand) {
  131.         $query->where('brand_id', $brand->id);
  132.     }
  133.  
  134.     if ($from) {
  135.         $query->where('created_at', '>=', $from);
  136.     }
  137.  
  138.     if ($to) {
  139.         $query->where('created_at', '<=', $to);
  140.     }
  141.  
  142.     return $this->execute($query);
  143. }
  144.  
  145. // App repository
  146. class AppRepository
  147. {
  148.     use UsesRelationships;
  149.  
  150.     /**
  151.      * @var boolean
  152.      */
  153.     protected $paginate = false;
  154.  
  155.     /**
  156.      * @var integer
  157.      */
  158.     protected $limit = null;
  159.  
  160.     /**
  161.      * @var integer
  162.      */
  163.     protected $offset = 1;
  164.  
  165.     /**
  166.      * @var array
  167.      */
  168.     protected $relations = [];
  169.  
  170.     /**
  171.      * Specify the repository to paginate the response.
  172.      *
  173.      * @param integer $limit
  174.      * @param integer $offset
  175.      * @return self
  176.      *
  177.      * @throws BindingResolutionException
  178.      */
  179.     public function paginate($limit = null, $offset = 1)
  180.     {
  181.         $offset = ($offset + 1);
  182.  
  183.         $validator = app()->make('Support\Validators\Pagination\PaginationValidator');
  184.  
  185.         $validator->validate([
  186.             'limit' => $limit,
  187.             'offset' => $offset
  188.         ]);
  189.  
  190.         $this->paginate = true;
  191.         $this->limit = $limit;
  192.         $this->offset = $offset;
  193.  
  194.         return $this;
  195.     }
  196.  
  197.     /**
  198.      * Specify the repository to include relationships.
  199.      *
  200.      * @param array|string $relations
  201.      * @return self
  202.      */
  203.     public function with($relations)
  204.     {
  205.         if (is_string($relations)) {
  206.             $this->relations = explode(',', $relations);
  207.         } else {
  208.             $this->relations = is_array($relations) ? $relations : [];
  209.         }
  210.  
  211.         return $this;
  212.     }
  213.  
  214.     /**
  215.      * Execute the specified query including the specified
  216.      * relations and pagination parameters.
  217.      *
  218.      * @param Builder $query
  219.      * @param bool $first
  220.      * @return LengthAwarePaginator|Collection|Model
  221.      */
  222.     protected function execute($query, $first = false)
  223.     {
  224.         $relations = $this->relations();
  225.  
  226.         if (isset($relations) && count($relations) > 0) {
  227.             $query->with($relations);
  228.         }
  229.  
  230.         $result = null;
  231.  
  232.         if (!$first && $this->paginate) {
  233.             if (isset($this->limit) && !is_null($this->limit)) {
  234.                 $result = $query->paginate($this->limit, ['*'], 'page', $this->offset);
  235.             } else {
  236.                 $result = $query->get();
  237.                 $result = new LengthAwarePaginator($result->all(), $result->count(), max($result->count(), 1), $this->offset);
  238.             }
  239.         } else {
  240.             if ($first) {
  241.                 $result = $query->first();
  242.             } else {
  243.                 $result = $query->get();
  244.             }
  245.         }
  246.  
  247.         $this->relations = [];
  248.         $this->paginate = false;
  249.  
  250.         return $result;
  251.     }
  252. }
  253.  
  254. // User Model
  255. <?php
  256. class User extends Authenticatable
  257. {
  258.     use HasApiTokens, Notifiable, LaratrustUserTrait, Authorizable, SoftDeletes {
  259.         Authorizable::can insteadof LaratrustUserTrait;
  260.     }
  261.  
  262.     /**
  263.      * The attributes that are mass assignable.
  264.      *
  265.      * @var array
  266.      */
  267.     protected $fillable = [
  268.         'referred_by',
  269.         'brand_id',
  270.         'username',
  271.         'email',
  272.         'email_verified_at',
  273.         'email_verification_token',
  274.         'password',
  275.         'password_reset_token',
  276.         'rate_limit',
  277.         'ip_address_on_registration'
  278.     ];
  279.  
  280.     /**
  281.      * The attributes that should be hidden for arrays.
  282.      *
  283.      * @var array
  284.      */
  285.     protected $hidden = [
  286.         'email',
  287.         'password',
  288.         'email_verification_token',
  289.         'password_reset_token',
  290.         'deleted_at',
  291.         'updated_at',
  292.         'rate_limit',
  293.         'ip_address_on_registration'
  294.     ];
  295.  
  296.     /**
  297.      * The attributes that should be mutated to dates.
  298.      *
  299.      * @var array
  300.      */
  301.     protected $dates = [
  302.         'email_verified_at'
  303.     ];
  304.  
  305.     /**
  306.      * Determine the request rate limit of the user.
  307.      *
  308.      * @return int
  309.      */
  310.     public function getRateLimitAttribute()
  311.     {
  312.         $rateLimit = $this->attributes['rate_limit'];
  313.  
  314.         return $rateLimit < 0 ? PHP_INT_MAX : $rateLimit;
  315.     }
  316.  
  317.     /**
  318.      * Set the user's password.
  319.      *
  320.      * @param string $password
  321.      * @return void
  322.      */
  323.     public function setPasswordAttribute($password)
  324.     {
  325.         $this->attributes['password'] = Hash::make($password);
  326.     }
  327.  
  328.     /**
  329.      * Retrieve the state associated to the user.
  330.      *
  331.      * @return HasOne
  332.      */
  333.     public function state()
  334.     {
  335.         return $this->hasOne('Domain\UserState\UserState');
  336.     }
  337.  
  338.     /**
  339.      * Retrieve the profile associated to the user.
  340.      *
  341.      * @return HasOne
  342.      */
  343.     public function profile()
  344.     {
  345.         return $this->hasOne('Domain\Profile\Profile');
  346.     }
  347.  
  348.     /**
  349.      * Retrieve the wallet associated to the user.
  350.      *
  351.      * @return HasMany
  352.      */
  353.     public function wallets()
  354.     {
  355.         return $this->hasMany('Domain\Wallet\Wallet');
  356.     }
  357.  
  358. }
  359.  
Advertisement
Add Comment
Please, Sign In to add comment