forked from FriendsOfPHP/Sismo
/
Project.php
210 lines (168 loc) · 4.28 KB
/
Project.php
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
<?php
/*
* This file is part of the Sismo utility.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Sismo;
/**
* Describes a project.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class Project
{
private $name;
private $slug;
private $repository;
private $branch = 'master';
private $command = 'phpunit';
private $urlPattern;
private $commits = array();
private $building = false;
private $notifiers = array();
public function __construct($name, $repository = null, $notifiers = array(), $slug = null)
{
$this->name = $name;
$this->slug = $slug ?: $this->slugify($name);
if (null !== $repository) {
$this->setRepository($repository);
}
if (!is_array($notifiers)) {
$notifiers = array($notifiers);
}
foreach ($notifiers as $notifier) {
$this->addNotifier($notifier);
}
}
public function __toString()
{
return $this->name;
}
public function setBuilding($bool)
{
$this->building = (Boolean) $bool;
}
public function isBuilding()
{
return $this->building;
}
public function addNotifier(Notifier $notifier)
{
$this->notifiers[] = $notifier;
}
public function getNotifiers()
{
return $this->notifiers;
}
public function setBranch($branch)
{
$this->branch = $branch;
}
public function getBranch()
{
return $this->branch;
}
public function setCommits(array $commits = array())
{
$this->commits = $commits;
}
public function getCommits()
{
return $this->commits;
}
public function getLatestCommit()
{
return $this->commits ? $this->commits[0] : null;
}
public function getStatusCode()
{
return !$this->commits ? 'no_build' : $this->commits[0]->getStatusCode();
}
public function getStatus()
{
return !$this->commits ? 'not built yet' : $this->commits[0]->getStatus();
}
public function getCCStatus()
{
if (!$this->commits || !$this->commits[0]->isBuilt()) {
return 'Unknown';
}
return $this->commits[0]->isSuccessful() ? 'Success' : 'Failure';
}
public function getCCActivity()
{
return $this->commits && $this->commits[0]->isBuilding() ? 'Building' : 'Sleeping';
}
public function getName()
{
return $this->name;
}
public function getShortName()
{
list($name, ) = explode('(', $this->name);
return trim($name);
}
public function getSubName()
{
if (false !== $pos = strpos($this->name, '(')) {
return trim(substr($this->name, $pos + 1, -1));
}
return '';
}
public function getSlug()
{
return $this->slug;
}
public function setSlug($slug)
{
$this->slug = $slug;
}
public function getRepository()
{
return $this->repository;
}
public function setRepository($url)
{
$this->repository = $url;
}
public function getCommand()
{
return $this->command;
}
public function setCommand($command)
{
$this->command = $command;
}
public function getUrlPattern()
{
return $this->urlPattern;
}
public function setUrlPattern($pattern)
{
$this->urlPattern = $pattern;
}
// code derived from http://php.vrana.cz/vytvoreni-pratelskeho-url.php
private function slugify($text)
{
// replace non letter or digits by -
$text = preg_replace('#[^\\pL\d]+#u', '-', $text);
// trim
$text = trim($text, '-');
// transliterate
if (function_exists('iconv')) {
$text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
}
// lowercase
$text = strtolower($text);
// remove unwanted characters
$text = preg_replace('#[^-\w]+#', '', $text);
if (empty($text)) {
throw new \RuntimeException(sprintf('Unable to compute a slug for "%s". Define it explicitly.', $text));
}
return $text;
}
}