[Laravel] Refactoring

1. add app/controllers/ApiController.php

<?php
class ApiController extends BaseController{
    protected $statusCode = 200;
    
    /**
     * getStatusCode function.
     * 
     * @access public
     * @return void
     */
    public function getStatusCode()
    {
        return $this--->statusCode;
    }
    
    /**
     * setStatusCode function.
     * 
     * @access public
     * @param mixed $statusCode
     * @return void
     */
    public function setStatusCode($statusCode)
    {
        $this->statusCode = $statusCode;
        
        return $this;
    }
    
    /**
     * respondNotFound function.
     * 
     * @access public
     * @param string $message (default: 'Not Found!')
     * @return void
     */
    public function respondNotFound($message = 'Not Found!')
    {
        return $this->setStatusCode(404)->respondWithError($message);
    }
    
    /**
     * respondInternalError function.
     * 
     * @access public
     * @param string $message (default: 'Internal Error!')
     * @return void
     */
    public function respondInternalError($message = 'Internal Error!')
    {
        return $this->setStatusCode(500)->respondWithError($message);
    }
    
    /**
     * respond function.
     * 
     * @access public
     * @param mixed $data
     * @param mixed $headers (default: [])
     * @return void
     */
    public function respond($data, $headers = [])
    {
        return Response::json($data, $this->getStatusCode(), $headers);
    }
    
    public function respondWithError($message)
    {
        return $this->respond([
            'error' => [
                'message' => $message,
                'status_code' => $this->getStatusCode()
            ]
        ]);
    }
}

2. update app/controllers/LessonsController.php

<?php
use Acme\Transformers\LessonTransformer;

class LessonsController extends ApiController {
    
    /**
     * @var My\Transformers\LessonTransformer
     */
    protected $lessonTransformer;
    
    function __construct(LessonTransformer $lessonTransformer)
    {
        $this->lessonTransformer = $lessonTransformer;
    }
    
    
  /**
   * Display a listing of the resource.
   *
   * @return Response
   */
  public function index()
  {
    // 1. All is bad
    // 2. No way to attach meta data
    // 3. Linking db structure to the API output
    // 4. No way to signal headers/response codes
    
    //return Lesson::all();
  
    $lessons = Lesson::all();
    //return Response::json([
      //'data' => $lessons->toArray()
    //], 200);
    //return Response::json([
      //'data' => $this->transformCollection($lessons)
    //], 200);
    //return Response::json([
      //'data' => $this->lessonTransformer->transformCollection($lessons->all())
    //], 200);
    return $this->respond([
      'data' => $this->lessonTransformer->transformCollection($lessons->all())
    ]);
  }


  /**
   * Show the form for creating a new resource.
   *
   * @return Response
   */
  public function create()
  {
    //
  }


  /**
   * Store a newly created resource in storage.
   *
   * @return Response
   */
  public function store()
  {
    //
  }


  /**
   * Display the specified resource.
   *
   * @param  int  $id
   * @return Response
   */
  public function show($id)
  {
    $lesson = Lesson::find($id);
    if( ! $lesson)
    {
        return $this->respondNotFound('Lesson does not exist.');
        
      //return Response::json([
        //'error' => [
          //'message' => 'Lesson does not exist'
        //]
      //], 404);
    }
    
    return $this->respond([
        'data' => $this->lessonTransformer->transform($lesson)
        //'data' => $this->transform($lesson->toArray())
    ]);
  }


  /**
   * Show the form for editing the specified resource.
   *
   * @param  int  $id
   * @return Response
   */
  public function edit($id)
  {
    //
  }


  /**
   * Update the specified resource in storage.
   *
   * @param  int  $id
   * @return Response
   */
  public function update($id)
  {
    //
  }


  /**
   * Remove the specified resource from storage.
   *
   * @param  int  $id
   * @return Response
   */
  public function destroy($id)
  {
    //
  }


}