php-censor/src/PHPCensor/Controller/HomeController.php

123 lines
3.3 KiB
PHP
Raw Normal View History

2013-05-03 17:02:53 +02:00
<?php
2016-07-19 20:28:11 +02:00
namespace PHPCensor\Controller;
2013-05-03 17:02:53 +02:00
use b8;
2016-07-19 20:28:11 +02:00
use PHPCensor\BuildFactory;
use PHPCensor\Helper\Lang;
use PHPCensor\Model\Build;
use PHPCensor\Controller;
2013-05-03 17:02:53 +02:00
/**
2017-03-04 16:39:56 +01:00
* Home Controller - Displays the PHPCI Dashboard.
*
* @author Dan Cryer <dan@block8.co.uk>
*/
2016-05-09 08:20:26 +02:00
class HomeController extends Controller
2013-05-03 17:02:53 +02:00
{
2013-10-08 14:52:05 +02:00
/**
* @var \PHPCensor\Store\BuildStore
2013-10-08 14:52:05 +02:00
*/
protected $buildStore;
2013-10-08 14:52:05 +02:00
/**
* @var \PHPCensor\Store\ProjectStore
2013-10-08 14:52:05 +02:00
*/
protected $projectStore;
2013-10-08 14:52:05 +02:00
2015-10-08 17:33:01 +02:00
/**
* @var \PHPCensor\Store\ProjectGroupStore
2015-10-08 17:33:01 +02:00
*/
protected $groupStore;
/**
* Initialise the controller, set up stores and services.
*/
public function init()
{
$this->buildStore = b8\Store\Factory::getStore('Build');
$this->projectStore = b8\Store\Factory::getStore('Project');
2015-10-08 17:33:01 +02:00
$this->groupStore = b8\Store\Factory::getStore('ProjectGroup');
}
/**
* Display PHPCI dashboard:
*/
public function index()
{
2014-12-04 12:14:04 +01:00
$this->layout->title = Lang::get('dashboard');
$builds = $this->buildStore->getLatestBuilds(null, 10);
foreach ($builds as &$build) {
$build = BuildFactory::getBuild($build);
}
$this->view->builds = $builds;
2015-10-08 17:33:01 +02:00
$this->view->groups = $this->getGroupInfo();
return $this->view->render();
}
/**
* Generate the HTML for the project overview section of the dashboard.
* @param $projects
* @return string
*/
protected function getSummaryHtml($projects)
{
2016-04-20 17:39:48 +02:00
$summaryBuilds = [];
$successes = [];
$failures = [];
$counts = [];
2015-10-08 17:33:01 +02:00
foreach ($projects as $project) {
$summaryBuilds[$project->getId()] = $this->buildStore->getLatestBuilds($project->getId());
2015-02-28 18:51:04 +01:00
$count = $this->buildStore->getWhere(
2016-04-20 17:39:48 +02:00
['project_id' => $project->getId()],
2015-02-28 18:51:04 +01:00
1,
0,
2016-04-20 17:39:48 +02:00
[],
['id' => 'DESC']
2015-02-28 18:51:04 +01:00
);
$counts[$project->getId()] = $count['count'];
$success = $this->buildStore->getLastBuildByStatus($project->getId(), Build::STATUS_SUCCESS);
$failure = $this->buildStore->getLastBuildByStatus($project->getId(), Build::STATUS_FAILED);
$successes[$project->getId()] = $success;
$failures[$project->getId()] = $failure;
}
$view = new b8\View('Home/dashboard-projects');
$view->projects = $projects;
$view->builds = $summaryBuilds;
$view->successful = $successes;
$view->failed = $failures;
$view->counts = $counts;
return $view->render();
}
2015-10-08 17:33:01 +02:00
2015-10-09 10:38:55 +02:00
/**
* Get a summary of the project groups we have, and what projects they have in them.
2017-01-15 16:35:42 +01:00
*
2015-10-09 10:38:55 +02:00
* @return array
*/
2015-10-08 17:33:01 +02:00
protected function getGroupInfo()
{
$rtn = [];
2016-04-20 17:39:48 +02:00
$groups = $this->groupStore->getWhere([], 100, 0, [], ['title' => 'ASC']);
2015-10-08 17:33:01 +02:00
foreach ($groups['items'] as $group) {
$thisGroup = ['title' => $group->getTitle()];
2017-01-15 16:35:42 +01:00
$projects = $this->projectStore->getByGroupId($group->getId(), false);
2015-10-08 17:33:01 +02:00
$thisGroup['projects'] = $projects['items'];
$thisGroup['summary'] = $this->getSummaryHtml($thisGroup['projects']);
$rtn[] = $thisGroup;
2015-10-08 17:33:01 +02:00
}
return $rtn;
}
}