1: <?php
2: namespace Contrib\Bundle\CoverallsV1Bundle\Entity;
3:
4: use Contrib\Bundle\CoverallsV1Bundle\Entity\Git\Git;
5:
6: 7: 8: 9: 10:
11: class JsonFile extends Coveralls
12: {
13: 14: 15: 16: 17:
18: protected $serviceName;
19:
20: 21: 22: 23: 24:
25: protected $serviceJobId;
26:
27: 28: 29: 30: 31:
32: protected $serviceNumber;
33:
34: 35: 36: 37: 38:
39: protected $serviceEventType;
40:
41: 42: 43: 44: 45:
46: protected $serviceBuildUrl;
47:
48: 49: 50: 51: 52:
53: protected $serviceBranch;
54:
55: 56: 57: 58: 59:
60: protected $servicePullRequest;
61:
62: 63: 64: 65: 66:
67: protected $repoToken;
68:
69: 70: 71: 72: 73:
74: protected $sourceFiles = array();
75:
76: 77: 78: 79: 80:
81: protected $git;
82:
83: 84: 85: 86: 87: 88: 89:
90: protected $runAt;
91:
92: 93: 94: 95: 96:
97: protected $metrics;
98:
99:
100:
101: 102: 103: 104: 105:
106: public function toArray()
107: {
108: $array = array();
109:
110: $arrayMap = array(
111:
112: 'service_name' => 'serviceName',
113: 'service_job_id' => 'serviceJobId',
114: 'service_number' => 'serviceNumber',
115: 'service_build_url' => 'serviceBuildUrl',
116: 'service_branch' => 'serviceBranch',
117: 'service_pull_request' => 'servicePullRequest',
118: 'service_event_type' => 'serviceEventType',
119: 'repo_token' => 'repoToken',
120: 'git' => 'git',
121: 'run_at' => 'runAt',
122: 'source_files' => 'sourceFiles',
123: );
124:
125: foreach ($arrayMap as $jsonKey => $propName) {
126: if (isset($this->$propName)) {
127: $array[$jsonKey] = $this->toJsonProperty($this->$propName);
128: }
129: }
130:
131: return $array;
132: }
133:
134: 135: 136: 137: 138: 139: 140:
141: public function fillJobs(array $env)
142: {
143: return $this
144: ->fillStandardizedEnvVars($env)
145: ->ensureJobs();
146: }
147:
148: 149: 150: 151: 152:
153: public function excludeNoStatementsFiles()
154: {
155: $this->sourceFiles = array_filter(
156: $this->sourceFiles,
157: function (SourceFile $sourceFile) {
158: return $sourceFile->getMetrics()->hasStatements();
159: }
160: );
161: }
162:
163: 164: 165: 166: 167:
168: public function sortSourceFiles()
169: {
170: ksort($this->sourceFiles);
171: }
172:
173: 174: 175: 176: 177:
178: public function reportLineCoverage()
179: {
180: $metrics = $this->getMetrics();
181:
182: foreach ($this->sourceFiles as $sourceFile) {
183:
184: $metrics->merge($sourceFile->getMetrics());
185: }
186:
187: return $metrics->getLineCoverage();
188: }
189:
190:
191:
192: 193: 194: 195: 196: 197:
198: protected function toJsonProperty($prop)
199: {
200: if ($prop instanceof Coveralls) {
201: return $prop->toArray();
202: } elseif (is_array($prop)) {
203: return $this->toJsonPropertyArray($prop);
204: }
205:
206: return $prop;
207: }
208:
209: 210: 211: 212: 213: 214:
215: protected function toJsonPropertyArray(array $propArray)
216: {
217: $array = array();
218:
219: foreach ($propArray as $prop) {
220: $array[] = $this->toJsonProperty($prop);
221: }
222:
223: return $array;
224: }
225:
226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243:
244: protected function fillStandardizedEnvVars(array $env)
245: {
246: $map = array(
247:
248: 'serviceName' => 'CI_NAME',
249: 'serviceNumber' => 'CI_BUILD_NUMBER',
250: 'serviceBuildUrl' => 'CI_BUILD_URL',
251: 'serviceBranch' => 'CI_BRANCH',
252: 'servicePullRequest' => 'CI_PULL_REQUEST',
253:
254:
255: 'serviceJobId' => 'CI_JOB_ID',
256: 'serviceEventType' => 'COVERALLS_EVENT_TYPE',
257: 'repoToken' => 'COVERALLS_REPO_TOKEN',
258: );
259:
260: foreach ($map as $propName => $envName) {
261: if (isset($env[$envName])) {
262: $this->$propName = $env[$envName];
263: }
264: }
265:
266: return $this;
267: }
268:
269: 270: 271: 272: 273: 274:
275: protected function ensureJobs()
276: {
277: if (!$this->hasSourceFiles()) {
278: throw new \RuntimeException('source_files must be set');
279: }
280:
281: if ($this->requireServiceJobId()) {
282: return $this;
283: }
284:
285: if ($this->requireServiceNumber()) {
286: return $this;
287: }
288:
289: if ($this->requireServiceEventType()) {
290: return $this;
291: }
292:
293: if ($this->isUnsupportedServiceJob()) {
294: return $this;
295: }
296:
297: $message = 'requirements are not satisfied.';
298:
299: throw new \RuntimeException($message);
300: }
301:
302: 303: 304: 305: 306:
307: protected function requireServiceJobId()
308: {
309: return isset($this->serviceName) && isset($this->serviceJobId) && !isset($this->repoToken);
310: }
311:
312: 313: 314: 315: 316:
317: protected function requireServiceNumber()
318: {
319: return isset($this->serviceName) && isset($this->serviceNumber) && isset($this->repoToken);
320: }
321:
322: 323: 324: 325: 326:
327: protected function requireServiceEventType()
328: {
329: return isset($this->serviceName) && isset($this->serviceEventType) && isset($this->repoToken);
330: }
331:
332: 333: 334: 335: 336:
337: protected function isUnsupportedServiceJob()
338: {
339: return !isset($this->serviceJobId) && !isset($this->serviceNumber) && !isset($this->serviceEventType) && isset($this->repoToken);
340: }
341:
342:
343:
344: 345: 346: 347: 348: 349:
350: public function hasSourceFile($path)
351: {
352: return isset($this->sourceFiles[$path]);
353: }
354:
355: 356: 357: 358: 359: 360:
361: public function getSourceFile($path)
362: {
363: if ($this->hasSourceFile($path)) {
364: return $this->sourceFiles[$path];
365: }
366:
367: return null;
368: }
369:
370: 371: 372: 373: 374:
375: public function addSourceFile(SourceFile $sourceFile)
376: {
377: $this->sourceFiles[$sourceFile->getPath()] = $sourceFile;
378: }
379:
380: 381: 382: 383: 384:
385: public function hasSourceFiles()
386: {
387: return count($this->sourceFiles) > 0;
388: }
389:
390: 391: 392: 393: 394:
395: public function getSourceFiles()
396: {
397: return $this->sourceFiles;
398: }
399:
400: 401: 402: 403: 404: 405:
406: public function setServiceName($serviceName)
407: {
408: $this->serviceName = $serviceName;
409:
410: return $this;
411: }
412:
413: 414: 415: 416: 417:
418: public function getServiceName()
419: {
420: if (isset($this->serviceName)) {
421: return $this->serviceName;
422: }
423:
424: return null;
425: }
426:
427: 428: 429: 430: 431: 432:
433: public function setRepoToken($repoToken)
434: {
435: $this->repoToken = $repoToken;
436:
437: return $this;
438: }
439:
440: 441: 442: 443: 444:
445: public function getRepoToken()
446: {
447: if (isset($this->repoToken)) {
448: return $this->repoToken;
449: }
450:
451: return null;
452: }
453:
454: 455: 456: 457: 458: 459:
460: public function setServiceJobId($serviceJobId)
461: {
462: $this->serviceJobId = $serviceJobId;
463:
464: return $this;
465: }
466:
467: 468: 469: 470: 471:
472: public function getServiceJobId()
473: {
474: if (isset($this->serviceJobId)) {
475: return $this->serviceJobId;
476: }
477:
478: return null;
479: }
480:
481: 482: 483: 484: 485:
486: public function getServiceNumber()
487: {
488: return $this->serviceNumber;
489: }
490:
491: 492: 493: 494: 495:
496: public function getServiceEventType()
497: {
498: return $this->serviceEventType;
499: }
500:
501: 502: 503: 504: 505:
506: public function getServiceBuildUrl()
507: {
508: return $this->serviceBuildUrl;
509: }
510:
511: 512: 513: 514: 515:
516: public function getServiceBranch()
517: {
518: return $this->serviceBranch;
519: }
520:
521: 522: 523: 524: 525:
526: public function getServicePullRequest()
527: {
528: return $this->servicePullRequest;
529: }
530:
531: 532: 533: 534: 535: 536:
537: public function setGit(Git $git)
538: {
539: $this->git = $git;
540:
541: return $this;
542: }
543:
544: 545: 546: 547: 548:
549: public function getGit()
550: {
551: if (isset($this->git)) {
552: return $this->git;
553: }
554:
555: return null;
556: }
557:
558: 559: 560: 561: 562: 563:
564: public function setRunAt($runAt)
565: {
566: $this->runAt = $runAt;
567:
568: return $this;
569: }
570:
571: 572: 573: 574: 575:
576: public function getRunAt()
577: {
578: if (isset($this->runAt)) {
579: return $this->runAt;
580: }
581:
582: return null;
583: }
584:
585: 586: 587: 588: 589:
590: public function getMetrics()
591: {
592: if (!isset($this->metrics)) {
593: $this->metrics = new Metrics();
594: }
595:
596: return $this->metrics;
597: }
598: }
599: