PSR2 compliance for PHPCI/Controller - Issue #18

This commit is contained in:
Dan Cryer 2013-05-16 15:25:39 +01:00
parent 65f93555be
commit e4a6e224c4
7 changed files with 558 additions and 580 deletions

View file

@ -8,9 +8,10 @@
*/
namespace PHPCI\Controller;
use b8,
b8\Store,
PHPCI\Model\Build;
use b8;
use b8\Store;
use PHPCI\Model\Build;
/**
* BitBucket Controller - Processes webhook pings from BitBucket.
@ -31,19 +32,15 @@ class BitbucketController extends b8\Controller
$branches = array();
$commits = array();
foreach($payload['commits'] as $commit)
{
if(!in_array($commit['branch'], $branches))
{
foreach ($payload['commits'] as $commit) {
if (!in_array($commit['branch'], $branches)) {
$branches[] = $commit['branch'];
$commits[$commit['branch']] = $commit['raw_node'];
}
}
foreach($branches as $branch)
{
try
{
foreach ($branches as $branch) {
try {
$build = new Build();
$build->setProjectId($project);
@ -53,9 +50,7 @@ class BitbucketController extends b8\Controller
$build->setCreated(new \DateTime());
$build->setBranch($branch);
$this->_buildStore->save($build);
}
catch(\Exception $ex)
{
} catch (\Exception $ex) {
}
}

View file

@ -8,9 +8,10 @@
*/
namespace PHPCI\Controller;
use b8,
b8\Registry,
PHPCI\Model\Build;
use b8;
use b8\Registry;
use PHPCI\Model\Build;
/**
* Build Controller - Allows users to run and view builds.
@ -74,8 +75,7 @@ class BuildController extends b8\Controller
public function delete($buildId)
{
if(!Registry::getInstance()->get('user')->getIsAdmin())
{
if (!Registry::getInstance()->get('user')->getIsAdmin()) {
throw new \Exception('You do not have permission to do that.');
}

View file

@ -8,9 +8,10 @@
*/
namespace PHPCI\Controller;
use b8,
b8\Store,
PHPCI\Model\Build;
use b8;
use b8\Store;
use PHPCI\Model\Build;
/**
* Github Controller - Processes webhook pings from Github.
@ -29,8 +30,7 @@ class GithubController extends b8\Controller
{
$payload = json_decode($this->getParam('payload'), true);
try
{
try {
$build = new Build();
$build->setProjectId($project);
$build->setCommitId($payload['after']);
@ -38,21 +38,16 @@ class GithubController extends b8\Controller
$build->setLog('');
$build->setCreated(new \DateTime());
$build->setBranch(str_replace('refs/heads/', '', $payload['ref']));
}
catch(\Exception $ex)
{
} catch (\Exception $ex) {
header('HTTP/1.1 400 Bad Request');
header('Ex: ' . $ex->getMessage());
die('FAIL');
}
try
{
try {
$build = $this->_buildStore->save($build);
$build->sendStatusPostback();
}
catch(\Exception $ex)
{
} catch (\Exception $ex) {
header('HTTP/1.1 500 Internal Server Error');
header('Ex: ' . $ex->getMessage());
die('FAIL');

View file

@ -8,6 +8,7 @@
*/
namespace PHPCI\Controller;
use b8;
/**

View file

@ -8,11 +8,12 @@
*/
namespace PHPCI\Controller;
use b8,
PHPCI\Model\Build,
PHPCI\Model\Project,
b8\Form,
b8\Registry;
use b8;
use PHPCI\Model\Build;
use PHPCI\Model\Project;
use b8\Form;
use b8\Registry;
/**
* Project Controller - Allows users to create, edit and view projects.
@ -59,8 +60,7 @@ class ProjectController extends b8\Controller
public function delete($id)
{
if(!Registry::getInstance()->get('user')->getIsAdmin())
{
if (!Registry::getInstance()->get('user')->getIsAdmin()) {
throw new \Exception('You do not have permission to do that.');
}
@ -78,7 +78,8 @@ class ProjectController extends b8\Controller
protected function getLatestBuildsHtml($projectId, $start = 0)
{
$builds = $this->_buildStore->getWhere(array('project_id' => $projectId), 10, $start, array(), array('id' => 'DESC'));
$criteria = array('project_id' => $projectId), 10, $start, array(), array('id' => 'DESC');
$builds = $this->_buildStore->getWhere($criteria);
$view = new b8\View('BuildsTable');
$view->builds = $builds['items'];
@ -87,20 +88,16 @@ class ProjectController extends b8\Controller
public function add()
{
if(!Registry::getInstance()->get('user')->getIsAdmin())
{
if (!Registry::getInstance()->get('user')->getIsAdmin()) {
throw new \Exception('You do not have permission to do that.');
}
$method = Registry::getInstance()->get('requestMethod');
if($method == 'POST')
{
if ($method == 'POST') {
$values = $this->getParams();
$pub = null;
}
else
{
} else {
$tempPath = sys_get_temp_dir() . '/';
// FastCGI fix for Windows machines, where temp path is not available to
@ -112,9 +109,11 @@ class ProjectController extends b8\Controller
}
$id = $tempPath . md5(microtime(true));
if (!is_dir($tempPath)) {
mkdir($tempPath);
}
shell_exec('ssh-keygen -q -t rsa -b 2048 -f '.$id.' -N "" -C "deploy@phpci"');
$pub = file_get_contents($id . '.pub');
@ -125,18 +124,17 @@ class ProjectController extends b8\Controller
$form = $this->projectForm($values);
if($method != 'POST' || ($method == 'POST' && !$form->validate()))
{
if ($method != 'POST' || ($method == 'POST' && !$form->validate())) {
$gh = \b8\Registry::getInstance()->get('github_app');
$code = $this->getParam('code', null);
if(!is_null($code))
{
if (!is_null($code)) {
$http = new \b8\HttpClient();
$resp = $http->post('https://github.com/login/oauth/access_token', array('client_id' => $gh['id'], 'client_secret' => $gh['secret'], 'code' => $code));
$url = 'https://github.com/login/oauth/access_token';
$params = array('client_id' => $gh['id'], 'client_secret' => $gh['secret'], 'code' => $code)
$resp = $http->post($url, $params);
if($resp['success'])
{
if ($resp['success']) {
parse_str($resp['body'], $resp);
$_SESSION['github_token'] = $resp['access_token'];
header('Location: /project/add');
@ -168,28 +166,23 @@ class ProjectController extends b8\Controller
public function edit($id)
{
if(!Registry::getInstance()->get('user')->getIsAdmin())
{
if (!Registry::getInstance()->get('user')->getIsAdmin()) {
throw new \Exception('You do not have permission to do that.');
}
$method = Registry::getInstance()->get('requestMethod');
$project = $this->_projectStore->getById($id);
if($method == 'POST')
{
if ($method == 'POST') {
$values = $this->getParams();
}
else
{
} else {
$values = $project->getDataArray();
$values['key'] = $values['git_key'];
}
$form = $this->projectForm($values, 'edit/' . $id);
if($method != 'POST' || ($method == 'POST' && !$form->validate()))
{
if ($method != 'POST' || ($method == 'POST' && !$form->validate())) {
$view = new b8\View('ProjectForm');
$view->type = 'edit';
$view->project = $project;
@ -218,16 +211,22 @@ class ProjectController extends b8\Controller
$form->addField(new Form\Element\Hidden('token'));
$form->addField(new Form\Element\Hidden('pubkey'));
$options = array(
'choose' => 'Select repository type...',
'github' => 'Github',
'bitbucket' => 'Bitbucket',
'local' => 'Local Path'
);
$field = new Form\Element\Select('type');
$field->setRequired(true);
$field->setPattern('^(github|bitbucket|local)');
$field->setOptions(array('choose' => 'Select repository type...', 'github' => 'Github', 'bitbucket' => 'Bitbucket', 'local' => 'Local Path'));
$field->setOptions($options);
$field->setLabel('Where is your project hosted?');
$field->setClass('span4');
$form->addField($field);
if(isset($_SESSION['github_token']))
{
if (isset($_SESSION['github_token'])) {
$field = new Form\Element\Select('github');
$field->setLabel('Choose a Github repository:');
$field->setClass('span4');
@ -235,29 +234,29 @@ class ProjectController extends b8\Controller
$form->addField($field);
}
$field = new Form\Element\Text('reference');
$field->setRequired(true);
$field->setValidator(function($val) use ($values)
{
$referenceValidator = function ($val) use ($values) {
$type = $values['type'];
switch($type) {
case 'local':
if(!is_dir($val)) {
if (!is_dir($val)) {
throw new \Exception('The path you specified does not exist.');
}
break;
case 'github':
case 'bitbucket':
if(!preg_match('/^[a-zA-Z0-9_\-]+\/[a-zA-Z0-9_\-]+$/', $val)) {
if (!preg_match('/^[a-zA-Z0-9_\-]+\/[a-zA-Z0-9_\-]+$/', $val)) {
throw new \Exception('Repository name must be in the format "owner/repo".');
}
break;
}
return true;
});
};
$field = new Form\Element\Text('reference');
$field->setRequired(true);
$field->setValidator($referenceValidator);
$field->setLabel('Repository Name / URL (Remote) or Path (Local)');
$field->setClass('span4');
$form->addField($field);
@ -287,14 +286,13 @@ class ProjectController extends b8\Controller
protected function getGithubRepositories()
{
$http = new \b8\HttpClient();
$res = $http->get('https://api.github.com/user/repos', array('type' => 'all', 'access_token' => $_SESSION['github_token']));
$url = 'https://api.github.com/user/repos';
$res = $http->get($url, array('type' => 'all', 'access_token' => $_SESSION['github_token']));
$rtn = array();
$rtn['choose'] = 'Select a repository...';
if($res['success'])
{
foreach($res['body'] as $repo)
{
if ($res['success']) {
foreach ($res['body'] as $repo) {
$rtn[$repo['full_name']] = $repo['full_name'];
}
}

View file

@ -8,6 +8,7 @@
*/
namespace PHPCI\Controller;
use b8;
/**
@ -25,12 +26,10 @@ class SessionController extends b8\Controller
public function login()
{
if(b8\Registry::getInstance()->get('requestMethod') == 'POST')
{
if (b8\Registry::getInstance()->get('requestMethod') == 'POST') {
$user = $this->_userStore->getByEmail($this->getParam('email'));
if($user && password_verify($this->getParam('password', ''), $user->getHash()))
{
if ($user && password_verify($this->getParam('password', ''), $user->getHash())) {
$_SESSION['user_id'] = $user->getId();
header('Location: /');
die;

View file

@ -8,10 +8,11 @@
*/
namespace PHPCI\Controller;
use b8,
b8\Registry,
PHPCI\Model\User,
b8\Form;
use b8;
use b8\Registry;
use PHPCI\Model\User;
use b8\Form;
/**
* User Controller - Allows an administrator to view, add, edit and delete users.
@ -37,26 +38,21 @@ class UserController extends b8\Controller
public function add()
{
if(!Registry::getInstance()->get('user')->getIsAdmin())
{
if (!Registry::getInstance()->get('user')->getIsAdmin()) {
throw new \Exception('You do not have permission to do that.');
}
$method = Registry::getInstance()->get('requestMethod');
if($method == 'POST')
{
if ($method == 'POST') {
$values = $this->getParams();
}
else
{
} else {
$values = array();
}
$form = $this->userForm($values);
if($method != 'POST' || ($method == 'POST' && !$form->validate()))
{
if ($method != 'POST' || ($method == 'POST' && !$form->validate())) {
$view = new b8\View('UserForm');
$view->type = 'add';
$view->user = null;
@ -80,28 +76,23 @@ class UserController extends b8\Controller
public function edit($id)
{
if(!Registry::getInstance()->get('user')->getIsAdmin())
{
if (!Registry::getInstance()->get('user')->getIsAdmin()) {
throw new \Exception('You do not have permission to do that.');
}
$method = Registry::getInstance()->get('requestMethod');
$user = $this->_userStore->getById($id);
if($method == 'POST')
{
if ($method == 'POST') {
$values = $this->getParams();
}
else
{
} else {
$values = $user->getDataArray();
$values['admin'] = $values['is_admin'];
}
$form = $this->userForm($values, 'edit/' . $id);
if($method != 'POST' || ($method == 'POST' && !$form->validate()))
{
if ($method != 'POST' || ($method == 'POST' && !$form->validate())) {
$view = new b8\View('UserForm');
$view->type = 'edit';
$view->user = $user;
@ -113,8 +104,7 @@ class UserController extends b8\Controller
$values = $form->getValues();
$values['is_admin'] = $values['admin'] ? 1 : 0;
if(!empty($values['password']))
{
if (!empty($values['password'])) {
$values['hash'] = password_hash($values['password'], PASSWORD_DEFAULT);
}
@ -167,8 +157,7 @@ class UserController extends b8\Controller
public function delete($id)
{
if(!Registry::getInstance()->get('user')->getIsAdmin())
{
if (!Registry::getInstance()->get('user')->getIsAdmin()) {
throw new \Exception('You do not have permission to do that.');
}
@ -176,5 +165,6 @@ class UserController extends b8\Controller
$this->_userStore->delete($user);
header('Location: /user');
die;
}
}