php-censor/src/Model/Project.php

267 lines
7.4 KiB
PHP
Raw Permalink Normal View History

2013-05-03 17:02:53 +02:00
<?php
2016-07-19 20:28:11 +02:00
namespace PHPCensor\Model;
2013-05-03 17:02:53 +02:00
2018-03-04 08:30:34 +01:00
use PHPCensor\Store\Factory;
2017-03-23 13:53:24 +01:00
use PHPCensor\Store\EnvironmentStore;
2018-03-09 19:00:53 +01:00
use PHPCensor\Store\ProjectGroupStore;
2017-03-23 13:53:24 +01:00
use Symfony\Component\Yaml\Parser as YamlParser;
use Symfony\Component\Yaml\Dumper as YamlDumper;
2018-03-09 19:00:53 +01:00
use PHPCensor\Model\Base\Project as BaseProject;
2013-05-03 17:02:53 +02:00
/**
2017-02-16 13:45:50 +01:00
* @author Dan Cryer <dan@block8.co.uk>
*/
2018-03-09 19:00:53 +01:00
class Project extends BaseProject
2013-05-03 17:02:53 +02:00
{
2017-02-16 13:45:50 +01:00
/**
2018-03-09 19:00:53 +01:00
* @return ProjectGroup|null
2017-02-16 13:45:50 +01:00
*/
public function getGroup()
{
2018-03-03 18:27:48 +01:00
$groupId = $this->getGroupId();
if (empty($groupId)) {
2017-02-16 13:45:50 +01:00
return null;
}
2018-03-09 19:00:53 +01:00
/** @var ProjectGroupStore $groupStore */
$groupStore = Factory::getStore('ProjectGroup');
return $groupStore->getById($groupId);
2017-02-16 13:45:50 +01:00
}
/**
* Get Build models by ProjectId for this Project.
*
* @return \PHPCensor\Model\Build[]
*/
public function getProjectBuilds()
{
2018-03-04 08:30:34 +01:00
return Factory::getStore('Build')->getByProjectId($this->getId());
2017-02-16 13:45:50 +01:00
}
/**
* 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)
{
2016-04-20 17:39:48 +02:00
$criteria = ['branch' => $branch, 'project_id' => $this->getId()];
if (isset($status)) {
$criteria['status'] = $status;
}
2016-04-20 17:39:48 +02:00
$order = ['id' => 'DESC'];
2018-03-04 08:30:34 +01:00
$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;
}
2014-02-26 16:23:24 +01:00
2015-10-13 17:22:39 +02:00
/**
* Return the previous build from a specific branch, for this project.
*
2015-10-13 17:22:39 +02:00
* @param string $branch
*
2015-10-13 17:22:39 +02:00
* @return mixed|null
*/
public function getPreviousBuild($branch = 'master')
{
2016-04-20 17:39:48 +02:00
$criteria = ['branch' => $branch, 'project_id' => $this->getId()];
$order = ['id' => 'DESC'];
2018-03-04 08:30:34 +01:00
$builds = Factory::getStore('Build')->getWhere($criteria, 1, 1, $order);
2015-10-13 17:22:39 +02:00
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:
$icon = 'bitbucket';
break;
case Project::TYPE_GIT:
case Project::TYPE_GITLAB:
case Project::TYPE_GOGS:
case Project::TYPE_HG:
case Project::TYPE_SVN:
default:
2017-02-11 10:49:56 +01:00
$icon = 'code-fork';
break;
}
return $icon;
}
2017-03-23 13:53:24 +01:00
/**
* @return EnvironmentStore
*/
protected function getEnvironmentStore()
{
/** @var EnvironmentStore $store */
2018-03-04 08:30:34 +01:00
$store = Factory::getStore('Environment');
return $store;
}
2017-03-23 13:53:24 +01:00
/**
* Get Environments
*
* @return array contain items with \PHPCensor\Model\Environment
*/
public function getEnvironmentsObjects()
{
2018-03-03 18:27:48 +01:00
$projectId = $this->getId();
2017-03-23 13:53:24 +01:00
2018-03-03 18:27:48 +01:00
if (empty($projectId)) {
2017-03-23 13:53:24 +01:00
return null;
}
2018-03-03 18:27:48 +01:00
return $this->getEnvironmentStore()->getByProjectId($projectId);
2017-03-23 13:53:24 +01:00
}
/**
* Get Environments
*
* @return string[]
*/
public function getEnvironmentsNames()
{
$environments = $this->getEnvironmentsObjects();
$environmentsNames = [];
2017-03-23 13:53:24 +01:00
foreach($environments['items'] as $environment) {
/** @var Environment $environment */
$environmentsNames[] = $environment->getName();
2017-03-23 13:53:24 +01:00
}
return $environmentsNames;
2017-03-23 13:53:24 +01:00
}
/**
* Get Environments
*
* @return string yaml
*/
public function getEnvironments()
{
$environments = $this->getEnvironmentsObjects();
$environmentsConfig = [];
2017-03-23 13:53:24 +01:00
foreach($environments['items'] as $environment) {
/** @var Environment $environment */
$environmentsConfig[$environment->getName()] = $environment->getBranches();
2017-03-23 13:53:24 +01:00
}
$yamlDumper = new YamlDumper();
$value = $yamlDumper->dump($environmentsConfig, 10, 0, true, false);
2017-03-23 13:53:24 +01:00
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) {
2017-03-23 13:53:24 +01:00
/** @var Environment $environment */
$key = array_search($environment->getName(), $environmentsNames);
2017-03-23 13:53:24 +01:00
if ($key !== false) {
// already exist
unset($environmentsNames[$key]);
$environment->setBranches(!empty($environmentsConfig[$environment->getName()]) ? $environmentsConfig[$environment->getName()] : []);
$store->save($environment);
2017-03-23 13:53:24 +01:00
} else {
// remove
$store->delete($environment);
}
}
if (!empty($environmentsNames)) {
2017-03-23 13:53:24 +01:00
// add
foreach ($environmentsNames as $environmentName) {
2017-03-23 13:53:24 +01:00
$environment = new Environment();
$environment->setProjectId($this->getId());
$environment->setName($environmentName);
$environment->setBranches(!empty($environmentsConfig[$environment->getName()]) ? $environmentsConfig[$environment->getName()] : []);
2017-03-23 13:53:24 +01:00
$store->save($environment);
}
}
}
/**
* @param string $branch
*
2017-03-23 13:53:24 +01:00
* @return string[]
*/
public function getEnvironmentsNamesByBranch($branch)
{
$environmentsNames = [];
$environments = $this->getEnvironmentsObjects();
$defaultBranch = ($branch == $this->getBranch());
2017-03-23 13:53:24 +01:00
foreach($environments['items'] as $environment) {
/** @var Environment $environment */
if ($defaultBranch || in_array($branch, $environment->getBranches())) {
$environmentsNames[] = $environment->getName();
2017-03-23 13:53:24 +01:00
}
}
return $environmentsNames;
2017-03-23 13:53:24 +01:00
}
/**
* @param string $environmentName
*
2017-03-23 13:53:24 +01:00
* @return string[]
*/
public function getBranchesByEnvironment($environmentName)
2017-03-23 13:53:24 +01:00
{
$branches = [];
2017-03-23 13:53:24 +01:00
$environments = $this->getEnvironmentsObjects();
foreach($environments['items'] as $environment) {
/** @var Environment $environment */
if ($environmentName == $environment->getName()) {
2017-03-23 13:53:24 +01:00
return $environment->getBranches();
}
}
2017-03-23 13:53:24 +01:00
return $branches;
}
2015-10-13 17:28:37 +02:00
}