/**
  * @test
  * @return void
  */
 public function it_can_delete_a_timer()
 {
     DB::beginTransaction();
     $this->logInUser();
     $timer = Timer::forCurrentUser()->first();
     $response = $this->call('DELETE', '/api/timers/' . $timer->id);
     $this->assertEquals(204, $response->getStatusCode());
     $response = $this->call('DELETE', '/api/timer/' . $timer->id);
     $this->assertEquals(404, $response->getStatusCode());
     DB::rollBack();
 }
 /**
  * Sort entries by date
  * For the graph
  * @param $entries
  * @return static
  */
 public function getTimersInDateRange($entries)
 {
     $formatForUser = '******';
     $earliestDate = Carbon::createFromFormat('Y-m-d H:i:s', Timer::forCurrentUser()->min('start'));
     $lastDate = Carbon::createFromFormat('Y-m-d H:i:s', Timer::forCurrentUser()->max('finish'));
     //Form an array with all the dates in the range of entries
     $entriesByDate = [];
     $index = 0;
     $shortDate = clone $lastDate;
     $shortDate = $shortDate->format('d/m');
     $day = clone $lastDate;
     $day = $day->format('D');
     $entriesByDate[] = ['date' => $lastDate->format($formatForUser), 'orderIndex' => $index, 'shortDate' => $shortDate, 'day' => $day];
     $date = Carbon::createFromFormat('Y-m-d H:i:s', Timer::forCurrentUser()->max('finish'));
     while ($date > $earliestDate) {
         $index++;
         $date = $date->subDays(1);
         $shortDate = clone $date;
         $shortDate = $shortDate->format('d/m');
         $day = clone $date;
         $day = $day->format('D');
         $entriesByDate[] = ['date' => $date->format($formatForUser), 'shortDate' => $shortDate, 'orderIndex' => $index, 'day' => $day];
     }
     //Add each entry to the array I formed
     foreach ($entries as $entry) {
         if ($entry->finish) {
             $startDate = Carbon::createFromFormat('Y-m-d H:i:s', $entry->start)->format($formatForUser);
             $finishDate = Carbon::createFromFormat('Y-m-d H:i:s', $entry->finish)->format($formatForUser);
             if ($startDate === $finishDate) {
                 $array = ['start' => Carbon::createFromFormat('Y-m-d H:i:s', $entry->start)->format('g:ia'), 'finish' => Carbon::createFromFormat('Y-m-d H:i:s', $entry->finish)->format('g:ia'), 'startPosition' => $entry->getStartRelativeHeight(), 'finishPosition' => $entry->getFinishRelativeHeight(), 'startHeight' => $entry->getDurationInMinutesDuringOneDay(), 'color' => $entry->activity->color];
                 $indexOfItem = $this->getIndexOfItem($entriesByDate, $startDate);
                 $entriesByDate[$indexOfItem][] = $array;
             } else {
                 $array = ['start' => Carbon::createFromFormat('Y-m-d H:i:s', $entry->start)->format('g:ia'), 'finish' => null, 'startPosition' => $entry->getStartRelativeHeight(), 'finishPosition' => null, 'startHeight' => $entry->getDurationInMinutesDuringOneDay('finish'), 'color' => $entry->activity->color];
                 $indexOfItem = $this->getIndexOfItem($entriesByDate, $startDate);
                 $entriesByDate[$indexOfItem][] = $array;
                 $finish = $entry->getFinish();
                 $midnight = clone $finish;
                 $midnight = $midnight->hour(0)->minute(0);
                 $array = ['start' => null, 'fakeStart' => $midnight->format('g:ia'), 'fakeStartPosition' => $entry->getStartRelativeHeight(true), 'finish' => $finish->format('g:ia'), 'startPosition' => null, 'finishPosition' => $entry->getFinishRelativeHeight(), 'startHeight' => $entry->getDurationInMinutesDuringOneDay('start'), 'color' => $entry->activity->color];
                 $indexOfItem = $this->getIndexOfItem($entriesByDate, $finishDate);
                 $entriesByDate[$indexOfItem][] = $array;
             }
         }
     }
     return collect($entriesByDate)->reverse();
 }
 /**
  * @test
  * @return void
  */
 public function it_can_update_a_timer()
 {
     DB::beginTransaction();
     $this->logInUser();
     $timer = Timer::forCurrentUser()->first();
     $finish = Carbon::today()->hour(23)->format('Y-m-d H:i:s');
     $this->assertEquals(1, $timer->activity_id);
     $response = $this->call('PUT', '/api/timers/' . $timer->id, ['finish' => $finish, 'start' => '2016-03-01 10:30:00', 'activity_id' => 2]);
     $content = json_decode($response->getContent(), true);
     //        dd($content);
     $this->checkTimerKeysExist($content);
     //Todo: test values are correct
     $this->assertEquals($finish, $content['finish']);
     $this->assertEquals('2016-03-01 10:30:00', $content['start']);
     $this->assertEquals(2, $content['activity']['data']['id']);
     $this->assertEquals(200, $response->getStatusCode());
     DB::rollBack();
 }
 /**
  * @return Response
  */
 public function checkForTimerInProgress()
 {
     $timerInProgress = Timer::forCurrentUser()->whereNull('finish')->first();
     if ($timerInProgress) {
         $timerInProgress = $this->transform($this->createItem($timerInProgress, new TimerTransformer()))['data'];
         return response($timerInProgress, Response::HTTP_OK);
     }
     return response([], Response::HTTP_OK);
 }
 /**
  * Define your route model bindings, pattern filters, etc.
  *
  * @param  \Illuminate\Routing\Router  $router
  * @return void
  */
 public function boot(Router $router)
 {
     parent::boot($router);
     Route::bind('exercises', function ($id) {
         return Exercise::forCurrentUser()->findOrFail($id);
     });
     Route::bind('exerciseEntries', function ($id) {
         return ExerciseEntry::forCurrentUser()->findOrFail($id);
     });
     Route::bind('menuEntries', function ($id) {
         return MenuEntry::forCurrentUser()->findOrFail($id);
     });
     Route::bind('exerciseSeries', function ($id) {
         return ExerciseSeries::forCurrentUser()->findOrFail($id);
     });
     Route::bind('foods', function ($id) {
         return Food::forCurrentUser()->findOrFail($id);
     });
     Route::bind('recipes', function ($id) {
         return Recipe::forCurrentUser()->findOrFail($id);
     });
     Route::bind('exerciseTags', function ($id) {
         return Tag::forCurrentUser()->where('for', 'exercise')->findOrFail($id);
     });
     Route::bind('recipeTags', function ($id) {
         return Tag::forCurrentUser()->where('for', 'recipe')->findOrFail($id);
     });
     Route::bind('foodUnits', function ($id) {
         return Unit::forCurrentUser()->where('for', 'food')->findOrFail($id);
     });
     Route::bind('exerciseUnits', function ($id) {
         return Unit::forCurrentUser()->where('for', 'exercise')->findOrFail($id);
     });
     Route::bind('timers', function ($id) {
         return Timer::forCurrentUser()->findOrFail($id);
     });
     Route::bind('activities', function ($id) {
         return Activity::forCurrentUser()->findOrFail($id);
     });
     Route::bind('weights', function ($idOrDate) {
         if (strrpos($idOrDate, '-')) {
             //parameter is the date of the entry
             $weight = Weight::forCurrentUser()->where('date', $idOrDate)->first();
         } else {
             //parameter is the id of the entry
             $weight = Weight::forCurrentUser()->findOrFail($idOrDate);
         }
         return $weight;
     });
     /**
      * $parameter is either the id or the date
      */
     Route::bind('journal', function ($parameter) {
         /**
          * @VP:
          * Is there a better way to check if the $parameter is an
          * id or a date? When I tried using Carbon to create an object from
          * the parameter, it threw an exception when the $parameter was the id,
          * whereas I just wanted a boolean.
          */
         if (strrpos($parameter, '-')) {
             //$parameter is the date of the entry
             $journal = Journal::forCurrentUser()->where('date', $parameter)->first();
         } else {
             //$parameter is the id of the entry
             $journal = Journal::forCurrentUser()->findOrFail($parameter);
         }
         return $journal;
     });
 }