title: Refactoring Laravel 4 App
body: <p>I'm in the process of refactoring my Laravel 4 app and I'd like some advice on my approach and best practice going forward. I'm trying to be as DRY as possible while also following SOLID principles.</p>
  
  <p>I have an update method that updates delegates in my app. There is a many to many relationship between delegates and events, and I need to check several conditions are met when updating in order to perform the correct action/function.</p>
  
  <p>For example, what I have so far checks whether the delegate is a floating delegate which isn't assigned to an event, and if it is it then needs to check event capacity, resit capacity and exam capacity, while also checking whether an event has been selected to move the delegate to.</p>
  
  <pre><code>public function update($id)
  {
      $delegate = $this-&gt;delegate-&gt;findById($id);
      $input = Input::all();
  
      if ($delegate-&gt;isFloater())
      {
          if (empty($input['new_event_id']))
          {
              $message = (object) array(
                  'title'         =&gt; 'Oops!',
                  'content'       =&gt; 'You did not select an event to move this floating delegate to.',
                  'alert_type'    =&gt; 'error'
              );
  
              return Redirect::back()-&gt;withInput()-&gt;with('message', $message);
          }
  
          $newEvent = $this-&gt;event-&gt;findById($input['new_event_id']);
  
          if ( ! $newEvent-&gt;hasCapacity())
          {
              $message = (object) array(
                  'title'         =&gt; 'Oops!',
                  'content'       =&gt; 'The event you are trying to move this floating delegate to has no availability.',
                  'alert_type'    =&gt; 'error'
              );
  
              return Redirect::back()-&gt;withInput()-&gt;with('message', $message);
          }
  
          if ( ! $newEvent-&gt;hasResitCapacity($input))
          {
              $message = (object) array(
                  'title'         =&gt; 'Oops!',
                  'content'       =&gt; 'The event you are trying to move this floating delegate to has no resit spaces left.',
                  'alert_type'    =&gt; 'error'
              );
  
              return Redirect::back()-&gt;withInput()-&gt;with('message', $message);
          }
  
          if ( ! $newEvent-&gt;hasExamCapacity($input))
          {
              $message = (object) array(
                  'title'         =&gt; 'Oops!',
                  'content'       =&gt; 'The event you are trying to move this floating delegate to has no exam spaces left.',
                  'alert_type'    =&gt; 'error'
              );
  
              return Redirect::back()-&gt;withInput()-&gt;with('message', $message);
          }
  
          $this-&gt;delegate-&gt;moveFloater($id, $input, $newEvent);
  
          $message = (object) array(
              'title'         =&gt; 'Excellent!',
              'content'       =&gt; 'This floating delegate was successfully moved.',
              'alert_type'    =&gt; 'success'
          );
  
          return Redirect::to('admin/events')-&gt;with('message', $message);
      }
  }
  </code></pre>
  
  <p>It's certainly much better than what I had previously, and I am abstracting code to various model methods such as hasCapacity() and isFloater() etc but already the method is big, and I'm repeating code with regards to the messages and redirects.</p>
  
  <p>I'm unsure at the moment how best to approach this going forward and I'm looking for some advice on how to proceed and clean it up further.</p>
  
  <p>Any help would be much appreciated. Thanks in advance.</p>