diff --git a/src/BuildFactory.php b/src/BuildFactory.php index f217b295..3d66aa9c 100644 --- a/src/BuildFactory.php +++ b/src/BuildFactory.php @@ -2,6 +2,7 @@ namespace PHPCensor; +use PHPCensor\Model\Project; use PHPCensor\Store\Factory; use PHPCensor\Model\Build; @@ -41,31 +42,31 @@ class BuildFactory if (!empty($project)) { switch ($project->getType()) { - case 'local': + case Project::TYPE_LOCAL: $type = 'LocalBuild'; break; - case 'git': + case Project::TYPE_GIT: $type = 'GitBuild'; break; - case 'github': + case Project::TYPE_GITHUB: $type = 'GithubBuild'; break; - case 'bitbucket': + case Project::TYPE_BITBUCKET: $type = 'BitbucketBuild'; break; - case 'gitlab': + case Project::TYPE_GITLAB: $type = 'GitlabBuild'; break; - case 'gogs': + case Project::TYPE_GOGS: $type = 'GogsBuild'; break; - case 'hg': + case Project::TYPE_HG: $type = 'HgBuild'; break; - case 'bitbucket-hg': + case Project::TYPE_BITBUCKET_HG: $type = 'BitbucketHgBuild'; break; - case 'svn': + case Project::TYPE_SVN: $type = 'SvnBuild'; break; default: diff --git a/src/Controller/ProjectController.php b/src/Controller/ProjectController.php index 700f149b..25abafce 100644 --- a/src/Controller/ProjectController.php +++ b/src/Controller/ProjectController.php @@ -16,6 +16,7 @@ use PHPCensor\Model\Build; use PHPCensor\Http\Response\RedirectResponse; use PHPCensor\View; use PHPCensor\Store\Factory; +use PHPCensor\Model\Project; /** * Project Controller - Allows users to create, edit and view projects. @@ -426,16 +427,16 @@ class ProjectController extends PHPCensor\Controller $form->addField(new Form\Element\Hidden('pubkey')); $options = [ - 'choose' => Lang::get('select_repository_type'), - 'github' => 'GitHub', - 'bitbucket' => 'Bitbucket (Git)', - 'bitbucket-hg' => 'Bitbucket (Hg)', - 'gitlab' => 'GitLab', - 'gogs' => 'Gogs', - 'git' => 'Git', - 'local' => Lang::get('local'), - 'hg' => 'Hg (Mercurial)', - 'svn' => 'Svn (Subversion)', + 'choose' => Lang::get('select_repository_type'), + Project::TYPE_GITHUB => 'GitHub', + Project::TYPE_BITBUCKET => 'Bitbucket (Git)', + Project::TYPE_BITBUCKET_HG => 'Bitbucket (Hg)', + Project::TYPE_GITLAB => 'GitLab', + Project::TYPE_GOGS => 'Gogs', + Project::TYPE_GIT => 'Git', + Project::TYPE_LOCAL => Lang::get('local'), + Project::TYPE_HG => 'Hg (Mercurial)', + Project::TYPE_SVN => 'Svn (Subversion)', ]; $field = Form\Element\Select::create('type', Lang::get('where_hosted'), true); @@ -566,7 +567,7 @@ class ProjectController extends PHPCensor\Controller if (in_array($type, $validators) && !preg_match($validators[$type]['regex'], $val)) { throw new \Exception($validators[$type]['message']); - } elseif ($type == 'local' && !is_dir($val)) { + } elseif (Project::TYPE_LOCAL === $type && !is_dir($val)) { throw new \Exception(Lang::get('error_path')); } diff --git a/src/Controller/WebhookController.php b/src/Controller/WebhookController.php index 7ffe654e..5888cb8f 100644 --- a/src/Controller/WebhookController.php +++ b/src/Controller/WebhookController.php @@ -99,7 +99,12 @@ class WebhookController extends Controller */ public function bitbucket($projectId) { - $project = $this->fetchProject($projectId, ['bitbucket', 'bitbucket-hg', 'hg', 'git']); + $project = $this->fetchProject($projectId, [ + Project::TYPE_BITBUCKET, + Project::TYPE_BITBUCKET_HG, + Project::TYPE_HG, + Project::TYPE_GIT, + ]); // Support both old services and new webhooks if ($payload = $this->getParam('payload')) { @@ -292,7 +297,10 @@ class WebhookController extends Controller */ public function git($projectId) { - $project = $this->fetchProject($projectId, ['local', 'git']); + $project = $this->fetchProject($projectId, [ + Project::TYPE_LOCAL, + Project::TYPE_GIT, + ]); $branch = $this->getParam('branch', $project->getBranch()); $commit = $this->getParam('commit'); $commitMessage = $this->getParam('message'); @@ -318,7 +326,10 @@ class WebhookController extends Controller */ public function github($projectId) { - $project = $this->fetchProject($projectId, ['github', 'git']); + $project = $this->fetchProject($projectId, [ + Project::TYPE_GITHUB, + Project::TYPE_GIT, + ]); switch ($_SERVER['CONTENT_TYPE']) { case 'application/json': @@ -503,7 +514,10 @@ class WebhookController extends Controller */ public function gitlab($projectId) { - $project = $this->fetchProject($projectId, ['gitlab', 'git']); + $project = $this->fetchProject($projectId, [ + Project::TYPE_GITLAB, + Project::TYPE_GIT, + ]); $payloadString = file_get_contents("php://input"); $payload = json_decode($payloadString, true); @@ -570,7 +584,9 @@ class WebhookController extends Controller */ public function svn($projectId) { - $project = $this->fetchProject($projectId, 'svn'); + $project = $this->fetchProject($projectId, [ + Project::TYPE_SVN + ]); $branch = $this->getParam('branch', $project->getBranch()); $commit = $this->getParam('commit'); $commitMessage = $this->getParam('message'); @@ -596,7 +612,11 @@ class WebhookController extends Controller */ public function gogs($projectId) { - $project = $this->fetchProject($projectId, ['gogs', 'git']); + $project = $this->fetchProject($projectId, [ + Project::TYPE_GOGS, + Project::TYPE_GIT, + ]); + switch ($_SERVER['CONTENT_TYPE']) { case 'application/json': $payload = json_decode(file_get_contents('php://input'), true); @@ -883,13 +903,13 @@ class WebhookController extends Controller * Fetch a project and check its type. * * @param integer $projectId id or title of project - * @param string $expectedType + * @param array $expectedType * * @return Project * * @throws Exception If the project does not exist or is not of the expected type. */ - protected function fetchProject($projectId, $expectedType) + protected function fetchProject($projectId, array $expectedType) { if (empty($projectId)) { throw new Exception('Project does not exist: ' . $projectId); @@ -908,10 +928,7 @@ class WebhookController extends Controller $project = reset($projects['items']); } - if (is_array($expectedType) - ? !in_array($project->getType(), $expectedType) - : $project->getType() !== $expectedType - ) { + if (!in_array($project->getType(), $expectedType, true)) { throw new Exception('Wrong project type: ' . $project->getType()); } diff --git a/src/Model/Base/Project.php b/src/Model/Base/Project.php index 0843ddb4..15e0c250 100644 --- a/src/Model/Base/Project.php +++ b/src/Model/Base/Project.php @@ -2,10 +2,21 @@ namespace PHPCensor\Model\Base; +use PHPCensor\Exception\HttpException\ValidationException; use PHPCensor\Model; class Project extends Model { + const TYPE_LOCAL = 'local'; + const TYPE_GIT = 'git'; + const TYPE_GITHUB = 'github'; + const TYPE_BITBUCKET = 'bitbucket'; + const TYPE_GITLAB = 'gitlab'; + const TYPE_GOGS = 'gogs'; + const TYPE_HG = 'hg'; + const TYPE_BITBUCKET_HG = 'bitbucket-hg'; + const TYPE_SVN = 'svn'; + /** * @var string */ @@ -32,6 +43,21 @@ class Project extends Model 'user_id' => 0, ]; + /** + * @var array + */ + protected $allowedTypes = [ + self::TYPE_LOCAL, + self::TYPE_GIT, + self::TYPE_GITHUB, + self::TYPE_BITBUCKET, + self::TYPE_GITLAB, + self::TYPE_GOGS, + self::TYPE_HG, + self::TYPE_BITBUCKET_HG, + self::TYPE_SVN, + ]; + /** * @return integer */ @@ -247,12 +273,20 @@ class Project extends Model * @param string $value * * @return boolean + * + * @throws ValidationException */ public function setType($value) { $this->validateNotNull('type', $value); $this->validateString('type', $value); + if (!in_array($value, $this->allowedTypes, true)) { + throw new ValidationException( + 'Column "type" must be one of: ' . join(', ', $this->allowedTypes) . '.' + ); + } + if ($this->data['type'] === $value) { return false; }