/**
  * Show details of all usernames of a certain length
  *
  * @param Request $request
  * @param int $length
  * @return \Illuminate\View\View|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
  */
 public function all(Request $request, $length = 1)
 {
     if (TwitterUser::whereUsernameLength($length)->count() == 0 && $length > 1) {
         return redirect('/all');
     }
     $users = TwitterUser::whereUsernameLength($length)->paginate(444);
     // 444 = number of possible username chars * bootstrap columns = 37*12
     $lengths = TwitterUser::selectUsernameLength()->orderBy('length')->groupBy('length')->get()->pluck('length')->toArray();
     return view('all')->withUsers($users)->withLengths($lengths)->withLength((int) $length);
 }
 /**
  * Choose which usernames to query next
  * If there are no free usernames at the moment, increae the length of the possible usernames by 1
  *
  * @return mixed
  */
 private function selectUsernames()
 {
     $toCheck = TwitterUser::neverQueried();
     if ($toCheck->count() == 0) {
         return TwitterUser::orderBy('last_checked', 'asc')->take(100)->get()->pluck('username');
     }
     return TwitterUser::neverQueried()->take(100)->get()->pluck('username');
 }
 /**
  * Decides of any more usernames should be added to the database
  * Will add more if any more of the same length need to be added
  * Won't add more if above condition passes and there are some usernames still to check or some available usernames
  *
  * @return bool
  */
 private function populateMoreUsernames()
 {
     $currentLength = TwitterUser::select(DB::raw('MAX(CHAR_LENGTH(username)) as Max'))->pluck('Max');
     if (is_null($currentLength) || !TwitterUser::whereUsername(str_repeat('_', $currentLength))->exists()) {
         return true;
     }
     if (TwitterUser::neverQueried()->count() > 0 || TwitterUser::free()->count() > 0) {
         return false;
     }
     return true;
 }