Skip to content

Commit

Permalink
Web notifications small fixes. Issue #156.
Browse files Browse the repository at this point in the history
  • Loading branch information
corpsee committed Apr 9, 2018
1 parent 3f56e4c commit 56fc76d
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 69 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@
/public/assets/vendor

/public/artifacts
!/public/artifacts/.gitkeep
!/public/artifacts/.gitkeep
2 changes: 0 additions & 2 deletions public/assets/js/dashboard-widgets/all_projects.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ PHPCensor.widgets.allProjects = {
error: PHPCensor.handleFailedAjax
});

//Let's build another mechanism for web notification
//since the above feature is tightly coupled to the view.
$.ajax({
url: APP_URL +
'web-notifications/widgets-all-projects-update/' +
Expand Down
12 changes: 2 additions & 10 deletions src/Controller/BuildController.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,17 +365,9 @@ public function ajaxMeta($buildId)

public function ajaxQueue()
{
$sPending = 'pending';
$sRunning = 'running';

$pending = $this->buildStore->getByStatus(Build::STATUS_PENDING);
$running = $this->buildStore->getByStatus(Build::STATUS_RUNNING);

$rtn = [

$sPending => $this->formatBuilds($pending),
$sRunning => $this->formatBuilds($running),

'pending' => $this->formatBuilds($this->buildStore->getByStatus(Build::STATUS_PENDING)),
'running' => $this->formatBuilds($this->buildStore->getByStatus(Build::STATUS_RUNNING)),
];

$response = new JsonResponse();
Expand Down
50 changes: 15 additions & 35 deletions src/Controller/WebNotificationsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,9 @@
namespace PHPCensor\Controller;

use PHPCensor\Model\Build;
use PHPCensor\Store\BuildStore;
use PHPCensor\WebController;
use PHPCensor\Store\Factory;
//use PHPCensor\View;
//use PHPCensor\Model\Project;
//use PHPCensor\Http\Response;
//use PHPCensor\Store\BuildStore;
//use PHPCensor\Store\ProjectStore;
//use PHPCensor\Store\ProjectGroupStore;
//use PHPCensor\Service\BuildService;
//use b8\Http\Response\JsonResponse;
use PHPCensor\Http\Response\JsonResponse;
use PHPCensor\Service\WebNotificationService;

Expand All @@ -26,35 +19,24 @@ class WebNotificationsController extends WebController
*/
protected $buildStore;

/**
* @var ProjectStore
*/
protected $projectStore;

/**
* @var ProjectGroupStore
*/
protected $groupStore;

/**
* Initialise the controller, set up stores and services.
*/
public function init()
{
parent::init();

$this->buildStore = Factory::getStore('Build');
$this->projectStore = Factory::getStore('Project');
$this->groupStore = Factory::getStore('ProjectGroup');
$this->buildStore = Factory::getStore('Build');
}

/**
* Provides JSON format for web notification UI of all last
* builds that have success and failed status.
* This is similar to WidgetAllProjectsController::update()
* but instead, this only returns JSON.
* Provides JSON format for web notification UI of all last builds that have success and failed status.
* This is similar to WidgetAllProjectsController::update() but instead, this only returns JSON.
*
* @param int $projectId
*
* @return \PHPCensor\Http\Response\JsonResponse
*
* @see \PHPCensor\Controller\WidgetAllProjectsController
*/
public function widgetsAllProjectsUpdate($projectId)
Expand All @@ -65,9 +47,6 @@ public function widgetsAllProjectsUpdate($projectId)
$oSuccess = WebNotificationService::formatBuild($success);
$oFailed = WebNotificationService::formatBuild($failed);

//@keys count and items Follow the for-loop structure
//found in
//\PHPCensor\Service\WebNotificationService::formatBuilds()
$aSuccess = [
'count' => count($oSuccess),
'items' => [$projectId => ['build' => $oSuccess]]
Expand All @@ -90,24 +69,25 @@ public function widgetsAllProjectsUpdate($projectId)


/**
* Provides JSON format for web notification UI of all last
* builds that have pending and running status.
* This is similar to WidgetAllProjectsController::update()
* but instead, this only returns JSON.
* @return \PHPCensor\Http\Response\JsonResponse
* Provides JSON format for web notification UI of all last builds that have pending and running status.
* This is similar to WidgetAllProjectsController::update() but instead, this only returns JSON.
*
* @return JsonResponse
*
* @throws \PHPCensor\Exception\HttpException
*/
public function buildsUpdated()
{
$pending = $this->buildStore->getByStatus(Build::STATUS_PENDING);
$running = $this->buildStore->getByStatus(Build::STATUS_RUNNING);

$rtn = [
$result = [
'pending' => WebNotificationService::formatBuilds($pending),
'running' => WebNotificationService::formatBuilds($running)
];

$response = new JsonResponse();
$response->setContent($rtn);
$response->setContent($result);

return $response;
}
Expand Down
6 changes: 4 additions & 2 deletions src/Controller/WidgetAllProjectsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
use PHPCensor\Store\BuildStore;
use PHPCensor\Store\ProjectStore;
use PHPCensor\Store\ProjectGroupStore;
use PHPCensor\Service\BuildService;
use b8\Http\Response\JsonResponse;

/**
* Widget All Projects Controller
Expand Down Expand Up @@ -106,6 +104,8 @@ protected function getSummaryHtml($projects)
* Get a summary of the project groups we have, and what projects they have in them.
*
* @return array
*
* @throws \Exception
*/
protected function getGroupInfo()
{
Expand All @@ -129,6 +129,8 @@ protected function getGroupInfo()
* @param integer $projectId
*
* @return Response
*
* @throws \PHPCensor\Exception\HttpException
*/
public function update($projectId)
{
Expand Down
4 changes: 4 additions & 0 deletions src/Controller/WidgetBuildErrorsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public function index()

/**
* @return Response
*
* @throws \PHPCensor\Exception\HttpException
*/
public function update()
{
Expand All @@ -65,6 +67,8 @@ public function update()
* @param View $view
*
* @return string
*
* @throws \PHPCensor\Exception\HttpException
*/
protected function renderAllProjectsLatestBuilds($view)
{
Expand Down
29 changes: 10 additions & 19 deletions src/Service/WebNotificationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@

namespace PHPCensor\Service;

use PHPCensor\Config;
use Pheanstalk\Pheanstalk;
use Pheanstalk\PheanstalkInterface;
use PHPCensor\BuildFactory;
use PHPCensor\Model\Build;
use PHPCensor\Model\Project;
use PHPCensor\Store\BuildStore;

/**
* A service that listens for creation, duplication and deletion of builds for web notification UI.
Expand All @@ -18,8 +12,11 @@ class WebNotificationService
/**
* Similar to BuildController::formatBuilds() but uses
* pure object to be used for rendering web notifications.
*
* @param array $builds
*
* @return array Formatted builds
*
* @see \PHPCensor\Controller\WidgetLastBuildsController::webNotificationUpdate().
*/
public static function formatBuilds($builds)
Expand All @@ -37,23 +34,21 @@ public static function formatBuilds($builds)

/**
* Provides structured keys for web notification.
*
* @param Build $build
*
* @return array
*/
public static function formatBuild($build)
{
if (empty($build) || is_null($build)) {
return [];
}
$status = $build->getStatus();

$status = $build->getStatus();
$datePerformed = '';
$dateFinished = '';
$dateFinished = '';

/*
BUG: Lang::out() automatically renders the values for
either 'created_x' or 'started_x' instead of just
returning them.
*/
if ($status === Build::STATUS_PENDING) {
$datePerformed = 'Created: ' . $build->getCreateDate()->format('H:i');
} elseif ($status === Build::STATUS_RUNNING) {
Expand All @@ -66,13 +61,9 @@ public static function formatBuild($build)

return [
'branch' => $build->getBranch(),
'url' => APP_URL .
'build/view/' .
$build->getId(),
'url' => APP_URL . 'build/view/' . $build->getId(),
'committer_email' => $build->getCommitterEmail(),
'img_src' => 'https://www.gravatar.com/avatar/' .
md5($build->getCommitterEmail()) .
'?d=mm&s=40',
'img_src' => 'https://www.gravatar.com/avatar/' . md5($build->getCommitterEmail()) . '?d=mm&s=40',
'project_title' => $build->getProject()->getTitle(),
'status' => $status,
'date_performed' => $datePerformed,
Expand Down

0 comments on commit 56fc76d

Please sign in to comment.