php-censor/src/PHPCI/BuildFactory.php

80 lines
2.1 KiB
PHP
Raw Normal View History

<?php
2013-05-16 03:16:56 +02:00
/**
* PHPCI - Continuous Integration for PHP
*
* @copyright Copyright 2014, Block 8 Limited.
* @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md
* @link https://www.phptesting.org/
2013-05-16 03:16:56 +02:00
*/
namespace PHPCI;
use b8\Store\Factory;
use PHPCI\Model\Build;
2013-05-16 03:16:56 +02:00
/**
* PHPCI Build Factory - Takes in a generic "Build" and returns a type-specific build model.
* @author Dan Cryer <dan@block8.co.uk>
2013-05-16 03:16:56 +02:00
*/
class BuildFactory
{
/**
* @param $buildId
* @return Build
* @throws \Exception
*/
public static function getBuildById($buildId)
{
$build = Factory::getStore('Build')->getById($buildId);
if (empty($build)) {
throw new \Exception('Build ID ' . $buildId . ' does not exist.');
}
return self::getBuild($build);
}
/**
* Takes a generic build and returns a type-specific build model.
2015-10-05 15:41:13 +02:00
* @param Build $build The build from which to get a more specific build type.
* @return Build
*/
2015-10-05 15:48:17 +02:00
public static function getBuild(Build $build)
{
2015-10-05 15:48:17 +02:00
$project = $build->getProject();
2015-10-05 15:48:17 +02:00
if (!empty($project)) {
switch ($project->getType()) {
case 'remote':
$type = 'RemoteGitBuild';
break;
case 'local':
$type = 'LocalBuild';
break;
case 'github':
$type = 'GithubBuild';
break;
case 'bitbucket':
$type = 'BitbucketBuild';
break;
case 'gitlab':
$type = 'GitlabBuild';
break;
case 'hg':
$type = 'MercurialBuild';
break;
case 'svn':
$type = 'SubversionBuild';
break;
2015-10-09 11:14:54 +02:00
default:
return $build;
2015-10-05 15:48:17 +02:00
}
2015-10-05 16:07:25 +02:00
2015-10-05 15:48:17 +02:00
$class = '\\PHPCI\\Model\\Build\\' . $type;
$build = new $class($build->getDataArray());
2015-10-05 15:41:13 +02:00
}
2015-10-05 15:41:13 +02:00
return $build;
}
}