forked from php-censor/php-censor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProject.php
More file actions
267 lines (232 loc) · 7.46 KB
/
Copy pathProject.php
File metadata and controls
267 lines (232 loc) · 7.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
<?php
namespace PHPCensor\Model;
use PHPCensor\Store\Factory;
use PHPCensor\Store\EnvironmentStore;
use PHPCensor\Store\ProjectGroupStore;
use Symfony\Component\Yaml\Parser as YamlParser;
use Symfony\Component\Yaml\Dumper as YamlDumper;
use PHPCensor\Model\Base\Project as BaseProject;
/**
* @author Dan Cryer <dan@block8.co.uk>
*/
class Project extends BaseProject
{
/**
* @return ProjectGroup|null
*/
public function getGroup()
{
$groupId = $this->getGroupId();
if (empty($groupId)) {
return null;
}
/** @var ProjectGroupStore $groupStore */
$groupStore = Factory::getStore('ProjectGroup');
return $groupStore->getById($groupId);
}
/**
* Get Build models by ProjectId for this Project.
*
* @return \PHPCensor\Model\Build[]
*/
public function getProjectBuilds()
{
return Factory::getStore('Build')->getByProjectId($this->getId());
}
/**
* Return the latest build from a specific branch, of a specific status, for this project.
*
* @param string $branch
* @param null $status
*
* @return mixed|null
*/
public function getLatestBuild($branch = 'master', $status = null)
{
$criteria = ['branch' => $branch, 'project_id' => $this->getId()];
if (isset($status)) {
$criteria['status'] = $status;
}
$order = ['id' => 'DESC'];
$builds = Factory::getStore('Build')->getWhere($criteria, 1, 0, $order);
if (is_array($builds['items']) && count($builds['items'])) {
$latest = array_shift($builds['items']);
if (isset($latest) && $latest instanceof Build) {
return $latest;
}
}
return null;
}
/**
* Return the previous build from a specific branch, for this project.
*
* @param string $branch
*
* @return mixed|null
*/
public function getPreviousBuild($branch = 'master')
{
$criteria = ['branch' => $branch, 'project_id' => $this->getId()];
$order = ['id' => 'DESC'];
$builds = Factory::getStore('Build')->getWhere($criteria, 1, 1, $order);
if (is_array($builds['items']) && count($builds['items'])) {
$previous = array_shift($builds['items']);
if (isset($previous) && $previous instanceof Build) {
return $previous;
}
}
return null;
}
/**
* Return the name of a FontAwesome icon to represent this project, depending on its type.
*
* @return string
*/
public function getIcon()
{
switch ($this->getType()) {
case Project::TYPE_GITHUB:
$icon = 'github';
break;
case Project::TYPE_BITBUCKET:
case Project::TYPE_BITBUCKET_HG:
case Project::TYPE_BITBUCKET_SERVER:
$icon = 'bitbucket';
break;
case Project::TYPE_GIT:
case Project::TYPE_GITLAB:
case Project::TYPE_GOGS:
case Project::TYPE_HG:
case Project::TYPE_SVN:
default:
$icon = 'code-fork';
break;
}
return $icon;
}
/**
* @return EnvironmentStore
*/
protected function getEnvironmentStore()
{
/** @var EnvironmentStore $store */
$store = Factory::getStore('Environment');
return $store;
}
/**
* Get Environments
*
* @return array contain items with \PHPCensor\Model\Environment
*/
public function getEnvironmentsObjects()
{
$projectId = $this->getId();
if (empty($projectId)) {
return null;
}
return $this->getEnvironmentStore()->getByProjectId($projectId);
}
/**
* Get Environments
*
* @return string[]
*/
public function getEnvironmentsNames()
{
$environments = $this->getEnvironmentsObjects();
$environmentsNames = [];
foreach ($environments['items'] as $environment) {
/** @var Environment $environment */
$environmentsNames[] = $environment->getName();
}
return $environmentsNames;
}
/**
* Get Environments
*
* @return string yaml
*/
public function getEnvironments()
{
$environments = $this->getEnvironmentsObjects();
$environmentsConfig = [];
foreach ($environments['items'] as $environment) {
/** @var Environment $environment */
$environmentsConfig[$environment->getName()] = $environment->getBranches();
}
$yamlDumper = new YamlDumper();
$value = $yamlDumper->dump($environmentsConfig, 10, 0, true, false);
return $value;
}
/**
* Set Environments
*
* @param string $value yaml
*/
public function setEnvironments($value)
{
$yamlParser = new YamlParser();
$environmentsConfig = $yamlParser->parse($value);
$environmentsNames = !empty($environmentsConfig) ? array_keys($environmentsConfig) : [];
$currentEnvironments = $this->getEnvironmentsObjects();
$store = $this->getEnvironmentStore();
foreach ($currentEnvironments['items'] as $environment) {
/** @var Environment $environment */
$key = array_search($environment->getName(), $environmentsNames);
if ($key !== false) {
// already exist
unset($environmentsNames[$key]);
$environment->setBranches(!empty($environmentsConfig[$environment->getName()]) ? $environmentsConfig[$environment->getName()] : []);
$store->save($environment);
} else {
// remove
$store->delete($environment);
}
}
if (!empty($environmentsNames)) {
// add
foreach ($environmentsNames as $environmentName) {
$environment = new Environment();
$environment->setProjectId($this->getId());
$environment->setName($environmentName);
$environment->setBranches(!empty($environmentsConfig[$environment->getName()]) ? $environmentsConfig[$environment->getName()] : []);
$store->save($environment);
}
}
}
/**
* @param string $branch
*
* @return string[]
*/
public function getEnvironmentsNamesByBranch($branch)
{
$environmentsNames = [];
$environments = $this->getEnvironmentsObjects();
$defaultBranch = ($branch == $this->getBranch());
foreach ($environments['items'] as $environment) {
/** @var Environment $environment */
if ($defaultBranch || in_array($branch, $environment->getBranches())) {
$environmentsNames[] = $environment->getName();
}
}
return $environmentsNames;
}
/**
* @param string $environmentName
*
* @return string[]
*/
public function getBranchesByEnvironment($environmentName)
{
$branches = [];
$environments = $this->getEnvironmentsObjects();
foreach ($environments['items'] as $environment) {
/** @var Environment $environment */
if ($environmentName == $environment->getName()) {
return $environment->getBranches();
}
}
return $branches;
}
}