/**
  * Handle middleware
  *
  * @param Request $request
  * @param callable $next
  * @return mixed
  */
 public function handle(Request $request, Closure $next)
 {
     //Get account
     $account = $this->getAccountFromRouting();
     //Set account in context
     $this->context->setAccount($account);
     //If the owner type is User
     if ($this->authorizer->getResourceOwnerType() == 'user') {
         //Find the user
         $user = $this->userRepository->find($this->authorizer->getResourceOwnerId());
         //If we have account in the route
         if ($account) {
             //Check if the user has access to the account
             if (!$user->isAssociateToAccount($account)) {
                 return $this->response->errorUnauthorized("You don't have access to the account {$account->uuid}");
             }
         }
         //Add context processor to log
         $this->log->addProcessors([new ContextProcessor($user, isset($account) ? $account : null)]);
         //Set the user in context
         $this->context->setUser($user);
     }
     // Set application locale
     $this->setApplicationLocale();
     return $next($request);
 }
 public function after($number)
 {
     if (Input::has('key') == false) {
         return $this->response->errorUnauthorized();
     }
     $ads = Ad::whereRaw('id > ' . $number)->orderBy('message_number', 'DESC')->paginate(50);
     // Pass this array (collection) into a resource, which will also have a "Transformer"
     // This "Transformer" can be a callback or a new instance of a Transformer object
     // We type hint for array, because each item in the $ads var is an array
     return $this->response->withPaginator($ads, new AdTransformer());
 }
 /**
  * Get request includes to prepare the eager loading request
  *
  * @return array
  */
 protected function withInclude()
 {
     $with = [];
     // Get includes
     $includes = $this->response->getManager()->getRequestedIncludes();
     // Check if the includes is in the available list
     // For security reason and validation
     foreach ($includes as $include) {
         // Add the include in the with value for eager loading, THIS IS VERY IMPORTANT
         // @see http://laravel.com/docs/eloquent#eager-loading
         if (in_array($include, $this->transformer->getAvailableIncludes())) {
             $with[] = $include;
         }
     }
     return $with;
 }
 public function keywords()
 {
     $keywords = DB::collection('map_reduce_twitter_words')->orderBy('value', 'desc')->paginate(25);
     return $this->response->withPaginator($keywords, new KeywordsTransformer());
 }
 /**
  * @param $userId
  * @return mixed
  */
 public function show($userId)
 {
     $this->authorize('current', $this->userRepository->findByUuid($userId));
     return $this->response->withItem($this->userRepository->findByUuid($userId), new UserTransformer());
 }
 /**
  * @param $error
  * @param $statusCode
  * @return mixed
  */
 private function errorResponse($error, $statusCode)
 {
     $error = new Error($error);
     $error->requestId = Uuid::uuid4()->toString();
     return $this->response->setStatusCode($statusCode)->withArray($this->errorTransformer->transform($error), ['Request-Id' => $error->requestId]);
 }