|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Model\Finder; |
|
4
|
|
|
|
|
5
|
|
|
use App\Model\Project; |
|
6
|
|
|
use App\Model\Deployment; |
|
7
|
|
|
use Ronanchilvers\Orm\Finder; |
|
8
|
|
|
use ClanCats\Hydrahon\Query\Expression; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Finder for deployment models |
|
12
|
|
|
* |
|
13
|
|
|
* @author Ronan Chilvers <[email protected]> |
|
14
|
|
|
*/ |
|
15
|
|
|
class DeploymentFinder extends Finder |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* Get the deployments for a project |
|
19
|
|
|
* |
|
20
|
|
|
* @return array |
|
21
|
|
|
* @author Ronan Chilvers <[email protected]> |
|
22
|
|
|
*/ |
|
23
|
|
|
public function forProject(Project $project) |
|
24
|
|
|
{ |
|
25
|
|
|
return $this->select() |
|
26
|
|
|
->where(Deployment::prefix('project'), $project->id) |
|
27
|
|
|
->orderBy(Deployment::prefix('number'), 'desc') |
|
28
|
|
|
->execute(); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Get the next deployment for a project |
|
33
|
|
|
* |
|
34
|
|
|
* This method returns a new unsaved deployment with the correct deployment number. |
|
35
|
|
|
* |
|
36
|
|
|
* @param \App\Model\Project $project |
|
37
|
|
|
* @return \App\Model\deployment |
|
38
|
|
|
* @author Ronan Chilvers <[email protected]> |
|
39
|
|
|
*/ |
|
40
|
|
|
public function nextForProject(Project $project) |
|
41
|
|
|
{ |
|
42
|
|
|
$existing = $this->select() |
|
43
|
|
|
->where(Deployment::prefix('project'), $project->id) |
|
44
|
|
|
->orderBy(Deployment::prefix('number'), 'desc') |
|
45
|
|
|
->one(); |
|
46
|
|
|
if ($existing instanceof Deployment) { |
|
47
|
|
|
$number = $existing->number; |
|
48
|
|
|
} else { |
|
49
|
|
|
$number = 0; |
|
50
|
|
|
} |
|
51
|
|
|
$deployment = new Deployment; |
|
52
|
|
|
$deployment->project = $project->id; |
|
53
|
|
|
$deployment->number = ++$number; |
|
54
|
|
|
|
|
55
|
|
|
return $deployment; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Get an array of deployments with a number lower than a specified one |
|
60
|
|
|
* |
|
61
|
|
|
* @param \App\Model\Project $project |
|
62
|
|
|
* @param int $number |
|
63
|
|
|
* @author Ronan Chilvers <[email protected]> |
|
64
|
|
|
*/ |
|
65
|
|
|
public function earlierThan(Project $project, $number) |
|
66
|
|
|
{ |
|
67
|
|
|
$sql = "SELECT * |
|
68
|
|
|
FROM deployments |
|
69
|
|
|
WHERE deployment_project = :project AND |
|
70
|
|
|
deployment_number <= ( |
|
71
|
|
|
SELECT MAX(deployment_number) |
|
72
|
|
|
FROM deployments |
|
73
|
|
|
WHERE deployment_project = :project |
|
74
|
|
|
) - :number"; |
|
75
|
|
|
|
|
76
|
|
|
return $this->query( |
|
77
|
|
|
$sql, |
|
78
|
|
|
[ |
|
79
|
|
|
'project' => $project->id, |
|
80
|
|
|
'number' => $number |
|
81
|
|
|
] |
|
82
|
|
|
); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Get a deployment for a given project and deployment number |
|
87
|
|
|
* |
|
88
|
|
|
* @param int $projectId |
|
89
|
|
|
* @param int $number |
|
90
|
|
|
* @return Deployment|null |
|
91
|
|
|
* @author Ronan Chilvers <[email protected]> |
|
92
|
|
|
*/ |
|
93
|
|
|
public function forProjectIdAndNumber( |
|
94
|
|
|
int $projectId, |
|
95
|
|
|
int $number |
|
96
|
|
|
) { |
|
97
|
|
|
return $this |
|
98
|
|
|
->select() |
|
99
|
|
|
->where(Deployment::prefix('project'), $projectId) |
|
100
|
|
|
->where(Deployment::prefix('number'), $number) |
|
101
|
|
|
->one(); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|