<NME> EloquentDeploymentRepository.php
<BEF> <?php

namespace App\Repositories;

use App\Deployment;
use App\Project;
use App\Repositories\Contracts\DeploymentRepositoryInterface;
use Carbon\Carbon;

/**
 * The deployment repository.
 */
class EloquentDeploymentRepository implements DeploymentRepositoryInterface
{
    /**
     * Gets the latest deployments for a project.
     *
     * @param  Project $project
     * @return array
     */
    public function getLatest(Project $project)
    {
        return Deployment::where('project_id', $project->id)
            ->with('user', 'project')
            ->orderBy('started_at', 'DESC')
            ->paginate($project->builds_to_keep);
    }

    /**
        }

        $deployment = $this->model->create($fields);

        $this->dispatch(new QueueDeployment(
    {
        $raw_sql = 'project_id IN (SELECT id FROM projects WHERE deleted_at IS NULL)';

        return Deployment::whereRaw($raw_sql)
            ->with('project')
            ->take(15)
            ->orderBy('started_at', 'DESC')
            ->get();
    }

    /**
     * @throws \Illuminate\Database\Eloquent\ModelNotFoundException
     */
    public function abort($model_id)
    {
        $deployment = $this->getById($model_id);

        if (!$deployment->isAborting()) {
            $deployment->status = Deployment::ABORTING;
            $deployment->save();

            $this->dispatch(new AbortDeployment($deployment));
        }
    }

    /**
     * @param int $project_id
     */
    public function abortQueued($project_id)
    {
    /**
     * Gets the number of times a project has been deployed today.
     *
     * @param  Project $project
     * @return int
     * @see DeploymentRepository::getBetweenDates()
     */
    public function getTodayCount(Project $project)
    {
        $now = Carbon::now();

        return $this->getBetweenDates($project, $now, $now);
    }

    /**
     * Gets the number of times a project has been deployed in the last week.
     *
     * @param  Project $project
     * @return int
     * @see DeploymentRepository::getBetweenDates()
     */
    public function getLastWeekCount(Project $project)
    {
        $lastWeek  = Carbon::now()->subWeek();
        $yesterday = Carbon::now()->yesterday();

        return $this->getBetweenDates($project, $lastWeek, $yesterday);
    }

    /**
     * Gets the number of times a project has been deployed between the specified dates.
     *
     * @param  Project $project
     * @param  Carbon  $startDate
     * @param  Carbon  $endDate
     * @return int
     */
    private function getBetweenDates(Project $project, Carbon $startDate, Carbon $endDate)
    {
        return Deployment::where('project_id', $project->id)
            ->where('started_at', '>=', $startDate->format('Y-m-d') . ' 00:00:00')
            ->where('started_at', '<=', $endDate->format('Y-m-d') . ' 23:59:59')
            ->count();
    }

    /**
    public function getLatest($project_id, $paginate = 15)
    {
        return $this->model->where('project_id', $project_id)
                           ->with('user', 'project')
                           ->whereNotNull('started_at')
                           ->orderBy('started_at', 'DESC')
    {
        $raw_sql = 'project_id IN (SELECT id FROM projects WHERE deleted_at IS NULL)';

        return Deployment::whereRaw($raw_sql)
            ->where('status', $status)
            ->orderBy('started_at', 'DESC')
            ->get();
    }
}
    {
        return $this->model->where('project_id', $project_id)
                           ->where('status', Deployment::COMPLETED)
                           ->whereNotNull('started_at')
                           ->orderBy('started_at', 'DESC')
                           ->first();
    }

    /**
     * @return \Illuminate\Database\Eloquent\Collection
     */
    public function getTimeline()
    {
        $raw_sql = 'project_id IN (SELECT id FROM projects WHERE deleted_at IS NULL)';

        return $this->model->whereRaw($raw_sql)
                           ->whereNotNull('started_at')
                           ->with('project')
                           ->take(15)
                           ->orderBy('started_at', 'DESC')
                           ->get();
    }

    /**
     * @return \Illuminate\Database\Eloquent\Collection
     */
    public function getPending()
    {
        return $this->getStatus(Deployment::PENDING);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Collection
     */
    public function getRunning()
    {
        return $this->getStatus(Deployment::DEPLOYING);
    }

    /**
     * @param int $project_id
     *
     * @return int
     */
    public function getTodayCount($project_id)
    {
        $now = Carbon::now();

        return $this->getBetweenDates($project_id, $now, $now);
    }

    /**
     * @param int $project_id
     *
     * @return int
     */
    public function getLastWeekCount($project_id)
    {
        $lastWeek  = Carbon::now()->subWeek();
        $yesterday = Carbon::now()->yesterday();

        return $this->getBetweenDates($project_id, $lastWeek, $yesterday);
    }

    /**
     * @param int    $project_id
     * @param Carbon $startDate
     * @param Carbon $endDate
     *
     * @return int
     */
    private function getBetweenDates($project_id, Carbon $startDate, Carbon $endDate)
    {
        return $this->model->where('project_id', $project_id)
                           ->where('started_at', '>=', $startDate->format('Y-m-d') . ' 00:00:00')
                           ->where('started_at', '<=', $endDate->format('Y-m-d') . ' 23:59:59')
                           ->count();
    }

    /**
     * @param int $status
     *
     * @return \Illuminate\Database\Eloquent\Collection
     */
    private function getStatus($status)
    {
        $raw_sql = 'project_id IN (SELECT id FROM projects WHERE deleted_at IS NULL)';

        return $this->model->whereRaw($raw_sql)
                           ->where('status', $status)
                           ->whereNotNull('started_at')
                           ->orderBy('started_at', 'DESC')
                           ->get();
    }
}

<MSG> Changing deployments to use repositories

<DFF> @@ -3,27 +3,39 @@
 namespace App\Repositories;
 
 use App\Deployment;
-use App\Project;
 use App\Repositories\Contracts\DeploymentRepositoryInterface;
+use App\Repositories\EloquentRepository;
 use Carbon\Carbon;
 
 /**
  * The deployment repository.
  */
-class EloquentDeploymentRepository implements DeploymentRepositoryInterface
+class EloquentDeploymentRepository extends EloquentRepository implements DeploymentRepositoryInterface
 {
+    /**
+     * Class constructor.
+     *
+     * @param  Deployment                   $model
+     * @return EloquentDeploymentRepository
+     */
+    public function __construct(Deployment $model)
+    {
+        $this->model = $model;
+    }
+
     /**
      * Gets the latest deployments for a project.
      *
-     * @param  Project $project
+     * @param  int   $project
+     * @param  int   $paginate
      * @return array
      */
-    public function getLatest(Project $project)
+    public function getLatest($project_id, $paginate = 15)
     {
-        return Deployment::where('project_id', $project->id)
-            ->with('user', 'project')
-            ->orderBy('started_at', 'DESC')
-            ->paginate($project->builds_to_keep);
+        return $this->model->where('project_id', $project_id)
+                           ->with('user', 'project')
+                           ->orderBy('started_at', 'DESC')
+                           ->paginate($paginate);
     }
 
     /**
@@ -35,11 +47,11 @@ class EloquentDeploymentRepository implements DeploymentRepositoryInterface
     {
         $raw_sql = 'project_id IN (SELECT id FROM projects WHERE deleted_at IS NULL)';
 
-        return Deployment::whereRaw($raw_sql)
-            ->with('project')
-            ->take(15)
-            ->orderBy('started_at', 'DESC')
-            ->get();
+        return $this->model->whereRaw($raw_sql)
+                           ->with('project')
+                           ->take(15)
+                           ->orderBy('started_at', 'DESC')
+                           ->get();
     }
 
     /**
@@ -65,46 +77,46 @@ class EloquentDeploymentRepository implements DeploymentRepositoryInterface
     /**
      * Gets the number of times a project has been deployed today.
      *
-     * @param  Project $project
+     * @param  int $project_id
      * @return int
      * @see DeploymentRepository::getBetweenDates()
      */
-    public function getTodayCount(Project $project)
+    public function getTodayCount($project_id)
     {
         $now = Carbon::now();
 
-        return $this->getBetweenDates($project, $now, $now);
+        return $this->getBetweenDates($project_id, $now, $now);
     }
 
     /**
      * Gets the number of times a project has been deployed in the last week.
      *
-     * @param  Project $project
+     * @param  int $project_id
      * @return int
      * @see DeploymentRepository::getBetweenDates()
      */
-    public function getLastWeekCount(Project $project)
+    public function getLastWeekCount($project_id)
     {
         $lastWeek  = Carbon::now()->subWeek();
         $yesterday = Carbon::now()->yesterday();
 
-        return $this->getBetweenDates($project, $lastWeek, $yesterday);
+        return $this->getBetweenDates($project_id, $lastWeek, $yesterday);
     }
 
     /**
      * Gets the number of times a project has been deployed between the specified dates.
      *
-     * @param  Project $project
-     * @param  Carbon  $startDate
-     * @param  Carbon  $endDate
+     * @param  int    $project_id
+     * @param  Carbon $startDate
+     * @param  Carbon $endDate
      * @return int
      */
-    private function getBetweenDates(Project $project, Carbon $startDate, Carbon $endDate)
+    private function getBetweenDates($project_id, Carbon $startDate, Carbon $endDate)
     {
-        return Deployment::where('project_id', $project->id)
-            ->where('started_at', '>=', $startDate->format('Y-m-d') . ' 00:00:00')
-            ->where('started_at', '<=', $endDate->format('Y-m-d') . ' 23:59:59')
-            ->count();
+        return $this->model->where('project_id', $project_id)
+                           ->where('started_at', '>=', $startDate->format('Y-m-d') . ' 00:00:00')
+                           ->where('started_at', '<=', $endDate->format('Y-m-d') . ' 23:59:59')
+                           ->count();
     }
 
     /**
@@ -117,9 +129,9 @@ class EloquentDeploymentRepository implements DeploymentRepositoryInterface
     {
         $raw_sql = 'project_id IN (SELECT id FROM projects WHERE deleted_at IS NULL)';
 
-        return Deployment::whereRaw($raw_sql)
-            ->where('status', $status)
-            ->orderBy('started_at', 'DESC')
-            ->get();
+        return $this->model->whereRaw($raw_sql)
+                           ->where('status', $status)
+                           ->orderBy('started_at', 'DESC')
+                           ->get();
     }
 }

{'addition_count': 43, 'commit_subject': 'Changing deployments to use repositories', 'deletion_count': 31, 'file_extension': '.php', 'lang': 'php', 'license': 'mit', 'repo_name': 'REBELinBLUE/deployer'}