Skip to content

Commit f85549c

Browse files
committed
Refactoring: Exception types.
1 parent d76ce27 commit f85549c

46 files changed

Lines changed: 147 additions & 84 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/Builder.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use DateTime;
66
use Exception;
7+
use PHPCensor\Exception\RuntimeException;
78
use PHPCensor\Helper\BuildInterpolator;
89
use PHPCensor\Helper\MailerFactory;
910
use PHPCensor\Logging\BuildLogger;
@@ -156,8 +157,6 @@ public function getCurrentStage(): ?string
156157
* Set the config array, as read from .php-censor.yml
157158
*
158159
* @param array $config
159-
*
160-
* @throws Exception
161160
*/
162161
public function setConfig(array $config)
163162
{
@@ -461,7 +460,7 @@ protected function setupBuild()
461460
$this->buildLogger->logSuccess(sprintf('Working copy created: %s', $this->buildPath));
462461

463462
if (!$workingCopySuccess) {
464-
throw new Exception('Could not create a working copy.');
463+
throw new RuntimeException('Could not create a working copy.');
465464
}
466465

467466
return true;

src/Command/Action/CreateAdmin.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace PHPCensor\Command\Action;
66

77
use PHPCensor\Exception\InvalidArgumentException;
8+
use PHPCensor\Exception\RuntimeException;
89
use PHPCensor\Service\UserService;
910
use PHPCensor\Store\UserStore;
1011
use Symfony\Component\Console\Helper\QuestionHelper;
@@ -82,7 +83,7 @@ public function create(array $adminDetails): void
8283
try {
8384
$adminUser = $this->userStore->getByEmail($adminDetails['email']);
8485
if ($adminUser) {
85-
throw new \RuntimeException('Admin account already exists!');
86+
throw new RuntimeException('Admin account already exists!');
8687
}
8788

8889
$userService = new UserService($this->userStore);

src/Command/InstallCommand.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
use PHPCensor\Command\Action\CreateAdmin;
1212
use PHPCensor\Configuration;
1313
use PHPCensor\Exception\InvalidArgumentException;
14+
use PHPCensor\Exception\RuntimeException;
1415
use PHPCensor\Model\ProjectGroup;
1516
use PHPCensor\Store\Factory;
1617
use PHPCensor\Store\ProjectGroupStore;
1718
use PHPCensor\Store\UserStore;
18-
use RuntimeException;
1919
use Symfony\Component\Console\Helper\QuestionHelper;
2020
use Symfony\Component\Console\Input\InputInterface;
2121
use Symfony\Component\Console\Input\InputOption;
@@ -207,7 +207,7 @@ private function checkRequirements(OutputInterface $output)
207207
}
208208

209209
if ($errors) {
210-
throw new Exception(
210+
throw new RuntimeException(
211211
'PHP Censor cannot be installed, as not all requirements are met. ' .
212212
'Please review the errors above before continuing.'
213213
);
@@ -262,7 +262,7 @@ private function getConfigInformation(InputInterface $input, OutputInterface $ou
262262

263263
$urlValidator = function ($answer) {
264264
if (!\filter_var($answer, FILTER_VALIDATE_URL)) {
265-
throw new Exception('Must be a valid URL.');
265+
throw new InvalidArgumentException('Must be a valid URL.');
266266
}
267267

268268
return \rtrim($answer, '/');

src/Command/WorkerCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
use Pheanstalk\Pheanstalk;
99
use PHPCensor\ConfigurationInterface;
1010
use PHPCensor\DatabaseManager;
11+
use PHPCensor\Exception\RuntimeException;
1112
use PHPCensor\Service\BuildService;
1213
use PHPCensor\Worker\BuildWorker;
1314
use Psr\Log\LoggerInterface;
14-
use RuntimeException;
1515
use Symfony\Component\Console\Input\InputInterface;
1616
use Symfony\Component\Console\Input\InputOption;
1717
use Symfony\Component\Console\Output\OutputInterface;

src/Console/Application.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use PHPCensor\Command\WorkerCommand;
2424
use PHPCensor\ConfigurationInterface;
2525
use PHPCensor\DatabaseManager;
26+
use PHPCensor\Exception\InvalidArgumentException;
2627
use PHPCensor\Logging\AnsiFormatter;
2728
use PHPCensor\Logging\Handler;
2829
use PHPCensor\Service\BuildService;
@@ -99,7 +100,7 @@ public function __construct(
99100
$oldDatabaseSettings = $applicationConfig->get('b8.database', []);
100101
$databaseSettings = $applicationConfig->get('php-censor.database', []);
101102
if ($oldDatabaseSettings && !$databaseSettings) {
102-
throw new \RuntimeException(
103+
throw new InvalidArgumentException(
103104
'Missing database settings in application config "config.yml" (Section: "php-censor.database")'
104105
);
105106
}

src/Controller/ProjectController.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use PHPCensor\WebController;
2323
use PHPCensor\Helper\Branch;
2424
use PHPCensor\Store\EnvironmentStore;
25+
use PHPCensor\Exception\RuntimeException;
2526

2627
/**
2728
* Project Controller - Allows users to create, edit and view projects.
@@ -692,9 +693,9 @@ protected function getReferenceValidator($values)
692693
];
693694

694695
if (in_array($type, $validators) && !preg_match($validators[$type]['regex'], $val)) {
695-
throw new Exception($validators[$type]['message']);
696+
throw new RuntimeException($validators[$type]['message']);
696697
} elseif (Project::TYPE_LOCAL === $type && !is_dir($val)) {
697-
throw new Exception(Lang::get('error_path'));
698+
throw new RuntimeException(Lang::get('error_path'));
698699
}
699700

700701
return true;

src/Controller/WebhookController.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
use Exception;
66
use GuzzleHttp\Client;
77
use PHPCensor\Controller;
8+
use PHPCensor\Exception\HttpException\ForbiddenException;
89
use PHPCensor\Exception\HttpException\NotFoundException;
910
use PHPCensor\Exception\InvalidArgumentException;
11+
use PHPCensor\Exception\RuntimeException;
1012
use PHPCensor\Helper\Lang;
1113
use PHPCensor\Http\Response;
1214
use PHPCensor\Model\Build;
@@ -292,24 +294,24 @@ protected function createBuild(
292294
protected function fetchProject($projectId, array $expectedType)
293295
{
294296
if (empty($projectId)) {
295-
throw new Exception('Project does not exist: ' . $projectId);
297+
throw new NotFoundException('Project does not exist: ' . $projectId);
296298
}
297299

298300
if (is_numeric($projectId)) {
299301
$project = $this->projectStore->getById((int)$projectId);
300302
} else {
301303
$projects = $this->projectStore->getByTitle($projectId, 2);
302304
if ($projects['count'] < 1) {
303-
throw new Exception('Project does not found: ' . $projectId);
305+
throw new NotFoundException('Project does not found: ' . $projectId);
304306
}
305307
if ($projects['count'] > 1) {
306-
throw new Exception('Project id is ambiguous: ' . $projectId);
308+
throw new NotFoundException('Project id is ambiguous: ' . $projectId);
307309
}
308310
$project = reset($projects['items']);
309311
}
310312

311313
if (!in_array($project->getType(), $expectedType, true)) {
312-
throw new Exception('Wrong project type: ' . $project->getType());
314+
throw new NotFoundException('Wrong project type: ' . $project->getType());
313315
}
314316

315317
return $project;
@@ -529,7 +531,7 @@ protected function bitbucketPullRequest(Project $project, array $payload)
529531
$appPassword = $this->configuration->get('php-censor.bitbucket.app_password');
530532

531533
if (empty($username) || empty($appPassword)) {
532-
throw new Exception('Please provide Username and App Password of your Bitbucket account.');
534+
throw new ForbiddenException('Please provide Username and App Password of your Bitbucket account.');
533535
}
534536

535537
$commitsUrl = $payload['pullrequest']['links']['commits']['href'];
@@ -542,7 +544,7 @@ protected function bitbucketPullRequest(Project $project, array $payload)
542544

543545
// Check we got a success response:
544546
if ($httpStatus < 200 || $httpStatus >= 300) {
545-
throw new Exception('Could not get commits, failed API request.');
547+
throw new RuntimeException('Could not get commits, failed API request.');
546548
}
547549

548550
$results = [];
@@ -833,7 +835,7 @@ protected function githubPullRequest(Project $project, array $payload)
833835

834836
// Check we got a success response:
835837
if ($status < 200 || $status >= 300) {
836-
throw new Exception('Could not get commits, failed API request.');
838+
throw new RuntimeException('Could not get commits, failed API request.');
837839
}
838840

839841
$results = [];

src/DatabaseManager.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace PHPCensor;
66

77
use PHPCensor\Common\Exception\Exception;
8+
use PHPCensor\Exception\RuntimeException;
89

910
class DatabaseManager
1011
{
@@ -84,7 +85,7 @@ public function getConnection(string $type = 'read'): DatabaseConnection
8485

8586
// No connection? Oh dear.
8687
if (!$connection && $type === 'read') {
87-
throw new Exception('Could not connect to any ' . $type . ' servers.');
88+
throw new RuntimeException('Could not connect to any ' . $type . ' servers.');
8889
}
8990

9091
$this->connections[$type] = $connection;

src/Exception/InvalidArgumentException.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@
44

55
namespace PHPCensor\Exception;
66

7-
use PHPCensor\Common\Exception\Exception;
8-
97
/**
108
* @package PHP Censor
119
* @subpackage Application
1210
*
1311
* @author Dmitry Khomutov <poisoncorpsee@gmail.com>
1412
*/
15-
class InvalidArgumentException extends Exception
13+
class InvalidArgumentException extends LogicException
1614
{
1715
}

src/Exception/LogicException.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace PHPCensor\Exception;
6+
7+
use PHPCensor\Common\Exception\Exception;
8+
9+
/**
10+
* @package PHP Censor
11+
* @subpackage Application
12+
*
13+
* @author Dmitry Khomutov <poisoncorpsee@gmail.com>
14+
*/
15+
class LogicException extends Exception
16+
{
17+
}

0 commit comments

Comments
 (0)