Page MenuHomeDevCentral

No OneTemporary

diff --git a/app/Actions/AMQPAction.php b/app/Actions/AMQPAction.php
index 3e8fe8d..ef22f04 100644
--- a/app/Actions/AMQPAction.php
+++ b/app/Actions/AMQPAction.php
@@ -1,37 +1,41 @@
<?php
namespace Nasqueron\Notifications\Actions;
class AMQPAction extends Action {
/**
* The action done on the broker ('publish', 'consume')
*
* @var string
*/
public $method;
/**
* The queue or exchange target on the broker
*
* @var string
*/
public $target;
/**
* The routing key
*
* @var string
*/
public $routingKey;
/**
* Initializes a new instance of a AMQP action to report
+ *
+ * @param string $method The action done on the broker (e.g. 'publish')
+ * @param string $target The queue or exchange target on the broker
+ * @param string $routingKey The routing key for this exchange or queue
*/
- public function __construct ($method, $target, $routingKey = '') {
+ public function __construct (string $method, string $target, string $routingKey = '') {
parent::__construct();
$this->method = $method;
$this->target = $target;
$this->routingKey = $routingKey;
}
}
diff --git a/app/Actions/Action.php b/app/Actions/Action.php
index 9946018..1004974 100644
--- a/app/Actions/Action.php
+++ b/app/Actions/Action.php
@@ -1,36 +1,36 @@
<?php
namespace Nasqueron\Notifications\Actions;
abstract class Action {
/**
* @var string
*/
public $action;
/**
* @var ActionError
*/
public $error;
/**
* Initializes a new instance of an action to report
*/
public function __construct () {
$this->action = class_basename(get_called_class());
}
/**
* Attaches an error to the action to report
*
* To attach an exception, you can use:
* <code>
* $actionToReport->attachError(new ActionError($exception));
* </code>
*
* @param ActionError $error The error to attach
*/
- public function attachError (ActionError $error) {
+ public function attachError (ActionError $error) : void {
$this->error = $error;
}
}
diff --git a/app/Actions/ActionsReport.php b/app/Actions/ActionsReport.php
index 0940fd8..a1ae343 100644
--- a/app/Actions/ActionsReport.php
+++ b/app/Actions/ActionsReport.php
@@ -1,90 +1,90 @@
<?php
namespace Nasqueron\Notifications\Actions;
class ActionsReport {
/**
* List of actions
*
* @var Action[]
*/
public $actions = [];
/**
* Report created date
*
* @var int
*/
public $created;
/**
* The entry gate
*
* @var string
*/
public $gate;
/**
* The entry door
*
* @var string
*/
public $door;
/**
* Initializes a new instance of an actions report
*/
public function __construct () {
$this->created = time();
}
///
/// Properties
///
/**
* Sets the gate and the door for this report
*
* @param string $gate The gate
* @param string $door The door
*/
- public function attachToGate ($gate, $door) {
+ public function attachToGate (string $gate, string $door) : void {
$this->gate = $gate;
$this->door = $door;
}
/**
* Adds an action to the list of actions to report
*
* @param Action $action The action to add
*/
- public function addAction (Action $action) {
+ public function addAction (Action $action) : void {
$this->actions[] = $action;
}
/**
* Determines if one of the action has failed.
*
* @return bool
*/
- public function containsError () {
+ public function containsError () : bool {
foreach ($this->actions as $action) {
if ($action->error !== null) {
return true;
}
}
return false;
}
///
/// Output
///
/**
* Gets a JSON string representation of the current instance
*/
- public function __toString () {
+ public function __toString () : string {
return json_encode($this, JSON_PRETTY_PRINT);
}
}
diff --git a/app/Actions/NotifyNewCommitsAction.php b/app/Actions/NotifyNewCommitsAction.php
index 74992eb..af1d9ae 100644
--- a/app/Actions/NotifyNewCommitsAction.php
+++ b/app/Actions/NotifyNewCommitsAction.php
@@ -1,21 +1,23 @@
<?php
namespace Nasqueron\Notifications\Actions;
class NotifyNewCommitsAction extends Action {
/**
* The Phabricator repository call sign
*
* @var string
*/
public $callSign;
/**
- * Initializes a new instance of a AMQP action to report
+ * Initializes a new instance of a AMQP action to report.
+ *
+ * @param string $callSign The Phabricator repository call sign
*/
- public function __construct ($callSign) {
+ public function __construct (string $callSign) {
parent::__construct();
$this->callSign = $callSign;
}
}
diff --git a/app/Actions/TriggerDockerHubBuildAction.php b/app/Actions/TriggerDockerHubBuildAction.php
index 04019e7..4b243f1 100644
--- a/app/Actions/TriggerDockerHubBuildAction.php
+++ b/app/Actions/TriggerDockerHubBuildAction.php
@@ -1,21 +1,23 @@
<?php
namespace Nasqueron\Notifications\Actions;
class TriggerDockerHubBuildAction extends Action {
/**
* The Docker Hub image
*
* @var string
*/
public $image;
/**
* Initializes a new instance of a DockerHub build trigger action to report
+ *
+ * @param string $image The Docker Hub image to trigger
*/
- public function __construct ($image) {
+ public function __construct (string $image) {
parent::__construct();
$this->image = $image;
}
}
diff --git a/app/Analyzers/BasePayloadAnalyzer.php b/app/Analyzers/BasePayloadAnalyzer.php
index 3029d22..eaf80ea 100644
--- a/app/Analyzers/BasePayloadAnalyzer.php
+++ b/app/Analyzers/BasePayloadAnalyzer.php
@@ -1,178 +1,174 @@
<?php
namespace Nasqueron\Notifications\Analyzers;
use Config;
use Storage;
use BadMethodCallException;
use InvalidArgumentException;
abstract class BasePayloadAnalyzer {
///
/// Constants
///
/**
* The name of the service, used to get specific classes and config
*/
const SERVICE_NAME = "UnknownService";
///
/// Private members
///
/**
* The project name, used to load specific configuration and offer defaults
* @var string
*/
protected $project;
/**
* The request content, as a structured data
* @var \stdClass
*/
protected $payload;
/**
* The configuration for the payload analyzer
* @var PayloadAnalyzerConfiguration;
*/
protected $configuration;
///
/// Constructor
///
/**
* Creates a new JenkinsPayloadAnalyzer instance.
*
* @param string $project
* @param \stdClass $payload
*/
- public function __construct($project, $payload) {
- if (!is_object($payload)) {
- throw new InvalidArgumentException("Payload must be an object.");
- }
-
+ public function __construct(string $project, \stdClass $payload) {
$this->project = $project;
$this->payload = $payload;
$this->loadConfiguration();
}
///
/// Configuration
///
/**
* The default name of the configuration file
*/
const CONFIG_DEFAULT_FILE = 'default.json';
/**
* Gets the full path to the configuration file.
*
* @return string
*/
- public function getConfigurationFileName () {
+ public function getConfigurationFileName () : string {
$dir = Config::get('services.' . strtolower(static::SERVICE_NAME) . '.analyzer.configDir');
$filename = $dir . '/' . $this->project . '.json';
if (!Storage::has($filename)) {
return $dir . '/' . static::CONFIG_DEFAULT_FILE;
}
return $filename;
}
/**
* Gets full qualified class name for configuration.
*
* @return string
*/
- private function getCandidateConfigurationClassName() {
+ private function getCandidateConfigurationClassName() : string {
$namespace = 'Nasqueron\Notifications\Analyzers\\' . static::SERVICE_NAME;
return $namespace . "\\" . static::SERVICE_NAME . 'PayloadAnalyzerConfiguration';
}
/**
* Gets full qualified class name for configuration if existing,
* or PayloadAnalyzerConfiguration class if not.
*
* @return string The configuration class to use
*/
- private function getConfigurationClassName () {
+ private function getConfigurationClassName () : string {
$class = $this->getCandidateConfigurationClassName();
if (class_exists($class)) {
return $class;
}
return PayloadAnalyzerConfiguration::class;
}
/**
* Loads configuration for the analyzer
*/
- public function loadConfiguration () {
+ public function loadConfiguration () : void {
$fileName = $this->getConfigurationFileName();
$class = $this->getConfigurationClassName();
$mapper = new \JsonMapper();
$this->configuration = $mapper->map(
json_decode(Storage::get($fileName)),
new $class($this->project)
);
}
///
/// Properties
///
/**
* Gets the name of the item.
*
* @var string
*/
- public function getItemName () {
+ public function getItemName () : string {
throw new BadMethodCallException("The getItemName method must be implemented in the analyzer class if used.");
}
/**
* Determines if the event isn't related to a specific item,
* but to the general service.
*
* @return bool
*/
- public function isAdministrativeEvent () {
+ public function isAdministrativeEvent () : bool {
return false;
}
/**
* Gets the group for a specific payload.
*
* @return string The group, central part of the routing key
*/
- public function getGroup () {
+ public function getGroup () : string {
// Some events are organization-level only and can't be mapped
// to projects.
if ($this->isAdministrativeEvent()) {
return $this->configuration->administrativeGroup;
}
// If the payload is about some repository matching a table of
// symbols, we need to sort it to the right group.
$item = $this->getItemName();
foreach ($this->configuration->map as $mapping) {
if ($mapping->doesItemBelong($item)) {
return $mapping->group;
}
}
return $this->configuration->getDefaultGroup();
}
}
diff --git a/app/Analyzers/GitHub/Events/CommitCommentEvent.php b/app/Analyzers/GitHub/Events/CommitCommentEvent.php
index 15c4fab..d24087b 100644
--- a/app/Analyzers/GitHub/Events/CommitCommentEvent.php
+++ b/app/Analyzers/GitHub/Events/CommitCommentEvent.php
@@ -1,38 +1,38 @@
<?php
namespace Nasqueron\Notifications\Analyzers\GitHub\Events;
/**
* CommitCommentEvent payload analyzer
*
* @link https://developer.github.com/v3/activity/events/types/#commitcommentevent
*/
class CommitCommentEvent extends Event {
/**
* Gets description for the payload
*
* @return string
*/
- public function getDescription () {
+ public function getDescription () : string {
$comment = $this->payload->comment;
return trans(
'GitHub.EventsDescriptions.CommitCommentEvent',
[
'author' => $comment->user->login,
'commit' => substr($comment->commit_id, 0, 8),
'excerpt' => self::cut($comment->body),
]
);
}
/**
* Gets link for the payload
*
* @return string
*/
- public function getLink () {
+ public function getLink () : string {
return $this->payload->comment->html_url;
}
}
diff --git a/app/Analyzers/GitHub/Events/CreateEvent.php b/app/Analyzers/GitHub/Events/CreateEvent.php
index ff91925..d46b2d3 100644
--- a/app/Analyzers/GitHub/Events/CreateEvent.php
+++ b/app/Analyzers/GitHub/Events/CreateEvent.php
@@ -1,78 +1,78 @@
<?php
namespace Nasqueron\Notifications\Analyzers\GitHub\Events;
/**
* CreateEvent payload analyzer
*
* We don't support the repository ref type, as, according the documentation,
* "webhooks will not receive this event for created repositories".webhooks
*
* Another case when we won't receive the event is when at least four tags are
* pushed at once.
*
* @link https://developer.github.com/v3/activity/events/types/#createevent
*/
class CreateEvent extends Event {
use WithRef;
/**
* Gets description for the payload
*
* @return string
*/
- public function getDescription () {
+ public function getDescription () : string {
$repository = $this->payload->repository->full_name;
$type = $this->payload->ref_type;
$ref = $this->payload->ref;
if (!self::isValidRefType($type)) {
return trans(
'GitHub.EventsDescriptions.CreateEventUnknown',
[
'type' => $type,
'ref' => $ref,
]
);
}
return trans(
'GitHub.EventsDescriptions.CreateEvent',
[
'type' => $type,
'ref' => $ref,
'repository' => $repository,
]
);
}
/**
* Gets link segments for the type
*
- * @return Array
+ * @return array
* @SuppressWarnings(PHPMD.UnusedPrivateMethod)
*/
- private function getLinkRefSegments () {
+ private function getLinkRefSegments () : array {
return [
'tag' => '/releases/tag/',
'branch' => '/tree/',
];
}
/**
* Gets link for the payload
*
* @return string
*/
- public function getLink () {
+ public function getLink () : string {
$type = $this->payload->ref_type;
$ref = $this->payload->ref;
$url = $this->payload->repository->html_url;
$url .= $this->getLinkRefSegment($type);
$url .= $ref;
return $url;
}
}
diff --git a/app/Analyzers/GitHub/Events/DeleteEvent.php b/app/Analyzers/GitHub/Events/DeleteEvent.php
index 136b9ca..418161c 100644
--- a/app/Analyzers/GitHub/Events/DeleteEvent.php
+++ b/app/Analyzers/GitHub/Events/DeleteEvent.php
@@ -1,73 +1,73 @@
<?php
namespace Nasqueron\Notifications\Analyzers\GitHub\Events;
/**
* DeleteEvent payload analyzer
*
* Another case when we won't receive the event is when at least four tags are
* deleted at once.
*
* @link https://developer.github.com/v3/activity/events/types/#deleteevent
*/
class DeleteEvent extends Event {
use WithRef;
/**
* Gets description for the payload
*
* @return string
*/
- public function getDescription () {
+ public function getDescription () : string {
$repository = $this->payload->repository->full_name;
$type = $this->payload->ref_type;
$ref = $this->payload->ref;
if (!self::isValidRefType($type)) {
return trans(
'GitHub.EventsDescriptions.DeleteEventUnknown',
[
'type' => $type,
'ref' => $ref,
]
);
}
return trans(
'GitHub.EventsDescriptions.DeleteEvent',
[
'type' => $type,
'ref' => $ref,
'repository' => $repository,
]
);
}
/**
* Gets link segments for the type
*
* @return Array
* @SuppressWarnings(PHPMD.UnusedPrivateMethod)
*/
private function getLinkRefSegments () {
return [
'tag' => '/tags',
'branch' => '/branches',
];
}
/**
* Gets link for the payload
*
* @return string
*/
- public function getLink () {
+ public function getLink () : string {
$type = $this->payload->ref_type;
$url = $this->payload->repository->html_url;
$url .= $this->getLinkRefSegment($type);
return $url;
}
}
diff --git a/app/Analyzers/GitHub/Events/ForkEvent.php b/app/Analyzers/GitHub/Events/ForkEvent.php
index 06b6a9f..078a4fb 100644
--- a/app/Analyzers/GitHub/Events/ForkEvent.php
+++ b/app/Analyzers/GitHub/Events/ForkEvent.php
@@ -1,37 +1,37 @@
<?php
namespace Nasqueron\Notifications\Analyzers\GitHub\Events;
/**
* ForkEvent payload analyzer
*
* Triggered when a repository is forked.
*
* @link https://developer.github.com/v3/activity/events/types/#forkevent
*/
class ForkEvent extends Event {
/**
* Gets description for the payload
*
* @return string
*/
- public function getDescription () {
+ public function getDescription () : string {
return trans(
'GitHub.EventsDescriptions.ForkEvent',
[
'repo_base' => $this->payload->repository->full_name,
'repo_fork' => $this->payload->forkee->full_name,
]
);
}
/**
* Gets link for the payload
*
* @return string
*/
- public function getLink () {
+ public function getLink () : string {
return $this->payload->forkee->html_url;
}
}
diff --git a/app/Analyzers/GitHub/Events/IssueCommentEvent.php b/app/Analyzers/GitHub/Events/IssueCommentEvent.php
index 1556048..b5d8aaa 100644
--- a/app/Analyzers/GitHub/Events/IssueCommentEvent.php
+++ b/app/Analyzers/GitHub/Events/IssueCommentEvent.php
@@ -1,65 +1,65 @@
<?php
namespace Nasqueron\Notifications\Analyzers\GitHub\Events;
/**
* IssueCommentEvent payload analyzer
*
* @link https://developer.github.com/v3/activity/events/types/#issuecommentevent
*/
class IssueCommentEvent extends Event {
/**
* Determines if the action is valid.
*
* @param string $type The action to check
* @return bool true if the action is valid; otherwise, false
*/
protected static function isValidAction ($action) {
$actions = ['created', 'edited', 'deleted'];
return in_array($action, $actions);
}
/**
* Gets description for the payload.
*
* @return string
*/
- public function getDescription () {
+ public function getDescription () : string {
$action = $this->payload->action;
if (!static::isValidAction($action)) {
return trans(
'GitHub.EventsDescriptions.IssueCommentEventUnknown',
['action' => $action]
);
}
$key = 'GitHub.EventsDescriptions.IssueCommentEventPerAction.';
$key .= $action;
$comment = $this->payload->comment;
$issue = $this->payload->issue;
return trans(
$key,
[
'author' => $comment->user->login,
'issueNumber' => $issue->number,
'issueTitle' => $issue->title,
'excerpt' => self::cut($comment->body),
]
);
}
/**
* Gets link for the payload.
*
* @return string
*/
- public function getLink () {
+ public function getLink () : string {
return $this->payload->comment->html_url;
}
}
diff --git a/app/Analyzers/GitHub/Events/PingEvent.php b/app/Analyzers/GitHub/Events/PingEvent.php
index e6311cd..7583108 100644
--- a/app/Analyzers/GitHub/Events/PingEvent.php
+++ b/app/Analyzers/GitHub/Events/PingEvent.php
@@ -1,35 +1,35 @@
<?php
namespace Nasqueron\Notifications\Analyzers\GitHub\Events;
/**
* PingEvent payload analyzer
*
* @link https://developer.github.com/webhooks/#ping-event
*/
class PingEvent extends Event {
/**
* Gets description for the payload
*
* @return string
*/
- public function getDescription () {
+ public function getDescription () : string {
return trans(
'GitHub.EventsDescriptions.PingEvent',
[
'zen' => $this->payload->zen,
'hook_id' => $this->payload->hook_id,
]
);
}
/**
* Gets link for the payload
*
* @return string
*/
- public function getLink () {
+ public function getLink () : string {
return '';
}
}
diff --git a/app/Analyzers/GitHub/Events/PullRequestEvent.php b/app/Analyzers/GitHub/Events/PullRequestEvent.php
index 9e33c7f..07de777 100644
--- a/app/Analyzers/GitHub/Events/PullRequestEvent.php
+++ b/app/Analyzers/GitHub/Events/PullRequestEvent.php
@@ -1,91 +1,91 @@
<?php
namespace Nasqueron\Notifications\Analyzers\GitHub\Events;
/**
* PingEvent payload analyzer
*
* @link https://developer.github.com/v3/activity/events/types/#pullrequestevent
*/
class PullRequestEvent extends Event {
/**
* Determines if the action is valid.
*
* @param string $type The action to check
* @return bool true if the action is valid; otherwise, false
*/
protected static function isValidAction ($action) {
$actions = [
'assigned', 'unassigned',
'labeled', 'unlabeled',
'opened', 'closed',
'edited', 'reopened',
];
return in_array($action, $actions);
}
/**
* Gets description for the payload.
*
* @return string
*/
- public function getDescription () {
+ public function getDescription () : string {
$action = $this->payload->action;
if (!static::isValidAction($action)) {
return trans(
'GitHub.EventsDescriptions.PullRequestEventUnknown',
['action' => $action]
);
}
$key = 'GitHub.EventsDescriptions.PullRequestEventPerAction.';
$key .= $action;
return trans($key, $this->getLocalisationParameters());
}
/**
* Gets the parameters to pass to the localisation message
*
* @return array
*/
private function getLocalisationParameters () {
$parameters = [
'author' => $this->payload->sender->login,
'number' => $this->payload->number,
'title' => $this->payload->pull_request->title,
];
switch ($this->payload->action) {
case 'assigned':
$parameters['assignee'] = $this->getLastAssignee();
break;
}
return $parameters;
}
/**
* @return string The last assignee username, or "" if there is no assignee.
*/
private function getLastAssignee() {
$assignees = $this->payload->pull_request->assignees;
if (count($assignees) === 0) {
return ""; // No assignee.
}
$assignee = array_pop($assignees);
return $assignee->login;
}
/**
* Gets link for the payload.
*
* @return string
*/
- public function getLink () {
+ public function getLink () : string {
return $this->payload->pull_request->html_url;
}
}
diff --git a/app/Analyzers/GitHub/Events/PushEvent.php b/app/Analyzers/GitHub/Events/PushEvent.php
index 7193b35..8ac69de 100644
--- a/app/Analyzers/GitHub/Events/PushEvent.php
+++ b/app/Analyzers/GitHub/Events/PushEvent.php
@@ -1,67 +1,67 @@
<?php
namespace Nasqueron\Notifications\Analyzers\GitHub\Events;
/**
* PushEvent payload analyzer
*
* @link https://developer.github.com/v3/activity/events/types/#pushevent
*/
class PushEvent extends Event {
use WithCommit;
use WithRepoAndBranch;
/**
* Gets the description message key according the amount of commits
*
* @param int $count The count of commits
* @return string The l10n message key for description
*/
private static function getDescriptionMessageKey ($count) {
$key = 'GitHub.EventsDescriptions.PushEvent';
if ($count === 0) {
return $key . '.0';
}
return $key . '.n';
}
/**
* Gets description for the payload
*
* @return string
*/
- public function getDescription () {
+ public function getDescription () : string {
$n = count($this->payload->commits);
if ($n === 1) {
// If only one commit is pushed at the time,
// we want a description for this commit.
return $this->getHeadCommitDescription();
}
// Otherwise, we want a description for the push.
return trans(self::getDescriptionMessageKey($n), [
'user' => $this->payload->pusher->name,
'count' => $n,
'repoAndBranch' => $this->getWhere(),
]);
}
/**
* Gets link for the payload
*
* @return string
*/
- public function getLink () {
+ public function getLink () : string {
$n = count($this->payload->commits);
if ($n === 1) {
return $this->payload->head_commit->url;
}
return $this->payload->compare;
}
}
diff --git a/app/Analyzers/GitHub/Events/RepositoryEvent.php b/app/Analyzers/GitHub/Events/RepositoryEvent.php
index 8c85b33..861122b 100644
--- a/app/Analyzers/GitHub/Events/RepositoryEvent.php
+++ b/app/Analyzers/GitHub/Events/RepositoryEvent.php
@@ -1,65 +1,65 @@
<?php
namespace Nasqueron\Notifications\Analyzers\GitHub\Events;
/**
* RepositoryEvent payload analyzer
*
* @link https://developer.github.com/v3/activity/events/types/#repositoryevent
*/
class RepositoryEvent extends Event {
/**
* Determines if the action is valid.
*
* @param string $type The action to check
* @return bool true if the action is valid; otherwise, false
*/
protected static function isValidAction ($action) {
$actions = ['created', 'deleted', 'publicized', 'privatized'];
return in_array($action, $actions);
}
/**
* Gets description for the payload
*
* @return string
*/
- public function getDescription () {
+ public function getDescription () : string {
$action = $this->payload->action;
if (!static::isValidAction($action)) {
return trans(
'GitHub.EventsDescriptions.RepositoryEventUnknown',
['action' => $action]
);
}
$key = 'GitHub.EventsDescriptions.RepositoryEventPerAction.';
$key .= $action;
$repository = $this->payload->repository->full_name;
$message = trans($key, ['repository' => $repository]);
if ($this->payload->repository->fork) {
$message .= trans('GitHub.EventsDescriptions.RepositoryEventFork');
}
if ($description = $this->payload->repository->description) {
$message .= trans('GitHub.Separator');
$message .= $description;
}
return $message;
}
/**
* Gets link for the payload
*
* @return string
*/
- public function getLink () {
+ public function getLink () : string {
return $this->payload->repository->html_url;
}
}
diff --git a/app/Analyzers/GitHub/Events/StatusEvent.php b/app/Analyzers/GitHub/Events/StatusEvent.php
index 28de9ec..f7b5079 100644
--- a/app/Analyzers/GitHub/Events/StatusEvent.php
+++ b/app/Analyzers/GitHub/Events/StatusEvent.php
@@ -1,65 +1,65 @@
<?php
namespace Nasqueron\Notifications\Analyzers\GitHub\Events;
/**
* StatusEvent payload analyzer
*
* @link https://developer.github.com/v3/activity/events/types/#statusevent
*/
class StatusEvent extends Event {
/**
* Gets state localized message
*
* @return string
*/
private function getState () {
$state = $this->payload->state; // pending, success, failure, or error
$key = 'GitHub.StatusEventState.' . $state;
return trans($key);
}
/**
* Gets status result
*
* @return string
*/
private function getStatusResult () {
$glue = trans('GitHub.Separator');
$fragments = array_filter([
$this->payload->context,
$this->payload->description,
$this->getState(),
]);
return implode($glue, $fragments);
}
/**
* Gets description for the payload
*
* @return string
*/
- public function getDescription () {
+ public function getDescription () : string {
return trans('GitHub.EventsDescriptions.StatusEvent', [
'commit' => substr($this->payload->sha, 0, 8),
'status' => $this->getStatusResult(),
]);
}
/**
* Gets link for the payload
*
* @return string
*/
- public function getLink () {
+ public function getLink () : string {
$url = $this->payload->target_url;
if ($url === null) {
return "";
}
return $url;
}
}
diff --git a/app/Analyzers/GitHub/Events/UnknownEvent.php b/app/Analyzers/GitHub/Events/UnknownEvent.php
index 9d943e1..9e38340 100644
--- a/app/Analyzers/GitHub/Events/UnknownEvent.php
+++ b/app/Analyzers/GitHub/Events/UnknownEvent.php
@@ -1,45 +1,45 @@
<?php
namespace Nasqueron\Notifications\Analyzers\GitHub\Events;
/**
* Unknown event payload analyzer
*
* This is a fallack when no specific class exists for this event type.
*/
class UnknownEvent extends Event {
/**
* @var string
*/
private $eventType;
/**
* Initializes a new instance of the UnknownEvent class, an Event analyzer
* class to handle unknown events type.
*
* @param string $eventType The event type (e.g. push)
*/
public function __construct ($eventType, $payload = null) {
$this->eventType = $eventType;
parent::__construct($payload);
}
/**
* Gets description for the payload
*
* @return string
*/
- public function getDescription () {
+ public function getDescription () : string {
return "Some $this->eventType happened";
}
/**
* Gets link for the payload
*
* @return string
*/
- public function getLink () {
+ public function getLink () : string {
return "";
}
}
diff --git a/app/Analyzers/GitHub/Events/WatchEvent.php b/app/Analyzers/GitHub/Events/WatchEvent.php
index 74dff7b..c118d01 100644
--- a/app/Analyzers/GitHub/Events/WatchEvent.php
+++ b/app/Analyzers/GitHub/Events/WatchEvent.php
@@ -1,41 +1,41 @@
<?php
namespace Nasqueron\Notifications\Analyzers\GitHub\Events;
/**
* WatchEvent payload analyzer
*
* Triggered when an user stars a repository.
*
* The current starring action on GitHub is the old watch action. To avoid
* to break code, former event name have been kept in the API.
*
* @link https://developer.github.com/changes/2012-09-05-watcher-api/
* @link https://developer.github.com/v3/activity/events/types/#watchevent
*/
class WatchEvent extends Event {
/**
* Gets description for the payload
*
* @return string
*/
- public function getDescription () {
+ public function getDescription () : string {
return trans(
'GitHub.EventsDescriptions.WatchEvent',
[
'user' => $this->payload->sender->login,
'repository' => $this->payload->repository->full_name,
]
);
}
/**
* Gets link for the payload
*
* @return string
*/
- public function getLink () {
+ public function getLink () : string {
return $this->payload->sender->html_url;
}
}
diff --git a/app/Analyzers/GitHub/GitHubPayloadAnalyzer.php b/app/Analyzers/GitHub/GitHubPayloadAnalyzer.php
index 495526a..3a2904d 100644
--- a/app/Analyzers/GitHub/GitHubPayloadAnalyzer.php
+++ b/app/Analyzers/GitHub/GitHubPayloadAnalyzer.php
@@ -1,112 +1,112 @@
<?php
namespace Nasqueron\Notifications\Analyzers\GitHub;
use Nasqueron\Notifications\Analyzers\BasePayloadAnalyzer;
use Nasqueron\Notifications\Analyzers\GitHub\Events\Event;
use Nasqueron\Notifications\Analyzers\GitHub\Events\UnknownEvent;
class GitHubPayloadAnalyzer extends BasePayloadAnalyzer {
/**
* The name of the service, used to get specific classes and config
*/
const SERVICE_NAME = "GitHub";
///
/// Private members
///
/**
* The GitHub event triggering this request
* @var string
*/
private $event;
/**
* The payload analyzer event
*
* @var \Nasqueron\Notifications\Analyzers\GitHub\Events\Event;
*/
private $analyzerEvent;
///
/// Constructor
///
/**
* Creates a new GitHubPayloadAnalyzer instance.
*
* @param string $project
* @param string $event
* @param \stdClass $payload
*/
- public function __construct($project, $event, $payload) {
+ public function __construct(string $project, string $event, \stdClass $payload) {
parent::__construct($project, $payload);
$this->event = $event;
try {
$this->analyzerEvent = Event::forPayload($event, $payload);
} catch (\InvalidArgumentException $ex) {
$this->analyzerEvent = new UnknownEvent($event);
}
}
///
/// Properties
///
/**
* Gets the name of the item, ie here of the name of the repository.
*
* @var string
*/
- public function getItemName () {
+ public function getItemName () : string {
if ($this->isAdministrativeEvent()) {
return '';
}
return $this->payload->repository->name;
}
///
/// Qualification of the payload
///
/**
* @return bool
*/
- public function isAdministrativeEvent () {
+ public function isAdministrativeEvent () : bool {
$administrativeEvents = [
'membership', // Member added to team
'ping', // Special ping pong event, fired on new hook
'repository', // Repository created
];
return in_array($this->event, $administrativeEvents);
}
///
/// Description of the payload
///
/**
* Gets a short textual description of the event.
*
* @return string
*/
- public function getDescription () {
+ public function getDescription () : string {
return $this->analyzerEvent->getDescription();
}
/**
* Gets a link to view the event on GitHub.
*
* @return string The most relevant URL
*/
- public function getLink () {
+ public function getLink () : string {
return $this->analyzerEvent->getLink();
}
}
diff --git a/app/Analyzers/ItemGroupMapping.php b/app/Analyzers/ItemGroupMapping.php
index 051e113..405d228 100644
--- a/app/Analyzers/ItemGroupMapping.php
+++ b/app/Analyzers/ItemGroupMapping.php
@@ -1,60 +1,60 @@
<?php
namespace Nasqueron\Notifications\Analyzers;
/**
* Map items (repositories, projects, items, etc.) names to groups
*/
class ItemGroupMapping {
///
/// Properties
///
/**
* The group the mapped items belong to
*
* @var string
*/
public $group;
/**
* An array of the items to map, each item a string with the name of the
* repository, project or item used for mapping.
* The wildcard '*' is allowed to specify several items.
*
* @var array
*/
public $items;
///
/// Helper methods
///
/**
* Determines if the specified item matches a pattern.
*
* @param string $pattern The pattern, with * allowed as wildcard character
* @param string $item The item name to compare with the pattern
* @return bool
*/
- public static function doesItemMatch ($pattern, $item) {
+ public static function doesItemMatch (string $pattern, string $item) : bool {
return str_is($pattern, $item);
}
/**
* Determines if the specified item belong to this mapping
*
* @return bool
*/
- public function doesItemBelong ($actualItem) {
+ public function doesItemBelong (string $actualItem) : bool {
foreach ($this->items as $candidateItem) {
if (static::doesItemMatch($candidateItem, $actualItem)) {
return true;
}
}
return false;
}
}
diff --git a/app/Analyzers/Jenkins/JenkinsPayloadAnalyzer.php b/app/Analyzers/Jenkins/JenkinsPayloadAnalyzer.php
index ff122a1..9c868a9 100644
--- a/app/Analyzers/Jenkins/JenkinsPayloadAnalyzer.php
+++ b/app/Analyzers/Jenkins/JenkinsPayloadAnalyzer.php
@@ -1,80 +1,82 @@
<?php
namespace Nasqueron\Notifications\Analyzers\Jenkins;
use Nasqueron\Notifications\Analyzers\BasePayloadAnalyzer;
class JenkinsPayloadAnalyzer extends BasePayloadAnalyzer {
/**
* The name of the service, used to get specific classes and config
*/
const SERVICE_NAME = "Jenkins";
///
/// Payload custom properties
///
/**
* Gets the name of the item, ie here of the job.
*
* @var string
*/
- public function getItemName () {
+ public function getItemName () : string {
return $this->payload->name;
}
///
/// Notify only on failure helper methods
///
/**
* Tries to get build status.
*
* @param out string $status
* @return bool indicates if the build status is defined in the payload
*/
- private function tryGetBuildStatus (&$status) {
+ private function tryGetBuildStatus (string &$status) : bool {
if (!isset($this->payload->build->status)) {
return false;
}
$status = $this->payload->build->status;
return true;
}
/**
* @return bool
*/
- public function shouldNotifyOnlyOnFailure () {
+ public function shouldNotifyOnlyOnFailure () : bool {
return in_array(
$this->getItemName(),
$this->configuration->notifyOnlyOnFailure
);
}
/**
* Determines if the build status is a failure.
*
* @return bool
*/
- public function isFailure () {
+ public function isFailure () : bool {
+ $status = "";
+
if (!$this->tryGetBuildStatus($status)) {
return false;
}
return $status === "FAILURE"
|| $status === "ABORTED"
|| $status === "UNSTABLE";
}
/**
* Indicates if we should handle this payload to trigger a notification.
*
* @return bool if false, this payload is to be ignored for notifications
*/
- public function shouldNotify () {
+ public function shouldNotify () : bool {
return $this->isFailure() || !$this->shouldNotifyOnlyOnFailure();
}
}
diff --git a/app/Analyzers/PayloadAnalyzerConfiguration.php b/app/Analyzers/PayloadAnalyzerConfiguration.php
index c2c8b15..3a8ff08 100644
--- a/app/Analyzers/PayloadAnalyzerConfiguration.php
+++ b/app/Analyzers/PayloadAnalyzerConfiguration.php
@@ -1,74 +1,74 @@
<?php
namespace Nasqueron\Notifications\Analyzers;
class PayloadAnalyzerConfiguration {
///
/// Private members
///
/**
* The project this configuration is for
*
* @var string
*/
protected $project;
///
/// Public properties
///
/**
* The group for organization only events
*
* @var string
*/
public $administrativeGroup;
/**
* The default group to fallback for any event not mapped in another group
*
* @var string
*/
public $defaultGroup;
/**
* An array of RepositoryGroupMapping objects to match repositories & groups
*
* @var \Nasqueron\Notifications\Analyzers\ItemGroupMapping[]
*/
public $map;
///
/// Constructor
///
/**
* Initializes a new instance of the BasePayloadAnalyzerConfiguration class
*
* @param string $project The project name this configuration is related to
*/
- public function __construct ($project) {
+ public function __construct (string $project) {
$this->project = $project;
}
///
/// Helper methods
///
/**
* Gets the default group
*
* @return string the default group, as set in the configuration,
* or if omitted, the project name as fallback.
*/
- public function getDefaultGroup () {
+ public function getDefaultGroup () : string {
if (empty($this->defaultGroup)) {
return strtolower($this->project);
}
return $this->defaultGroup;
}
}
diff --git a/app/Analyzers/Phabricator/PhabricatorGroupMapping.php b/app/Analyzers/Phabricator/PhabricatorGroupMapping.php
index 697b2c0..a89ff66 100644
--- a/app/Analyzers/Phabricator/PhabricatorGroupMapping.php
+++ b/app/Analyzers/Phabricator/PhabricatorGroupMapping.php
@@ -1,39 +1,39 @@
<?php
namespace Nasqueron\Notifications\Analyzers\Phabricator;
use Nasqueron\Notifications\Analyzers\ItemGroupMapping;
use Nasqueron\Notifications\Phabricator\PhabricatorStory;
class PhabricatorGroupMapping extends ItemGroupMapping {
///
/// Extra properties
///
/**
* An array of words, each item a string with a word to find in the story.
*
* @var array
*/
public $words = [];
///
/// Helper methods to process words
///
/**
* Determines if the specified story belong to this mapping
*
* @return bool
*/
- public function doesStoryBelong (PhabricatorStory $story) {
+ public function doesStoryBelong (PhabricatorStory $story) : bool {
foreach ($this->words as $word) {
if (stripos($story->text, $word) !== false) {
return true;
}
}
return false;
}
}
diff --git a/app/Analyzers/Phabricator/PhabricatorPayloadAnalyzer.php b/app/Analyzers/Phabricator/PhabricatorPayloadAnalyzer.php
index 8e4b1b1..c5ac65b 100644
--- a/app/Analyzers/Phabricator/PhabricatorPayloadAnalyzer.php
+++ b/app/Analyzers/Phabricator/PhabricatorPayloadAnalyzer.php
@@ -1,77 +1,77 @@
<?php
namespace Nasqueron\Notifications\Analyzers\Phabricator;
use Nasqueron\Notifications\Analyzers\BasePayloadAnalyzer;
use Nasqueron\Notifications\Phabricator\PhabricatorStory;
class PhabricatorPayloadAnalyzer extends BasePayloadAnalyzer {
/**
* The name of the service, used to get specific classes and config
*/
const SERVICE_NAME = "Phabricator";
///
/// Private members
///
/**
* The story
* @var PhabricatorStory
*/
private $story;
///
/// Constructor
///
/**
* Creates a new PhabricatorPayloadAnalyzer instance.
*
* @param string $project
* @param PhabricatorStory $story
*/
- public function __construct($project, PhabricatorStory $story) {
+ public function __construct(string $project, PhabricatorStory $story) {
$this->project = $project;
$this->story = $story;
$this->loadConfiguration();
}
///
/// Qualification of the story
///
/**
* Gets the group for a specific story.
*
* @return string the group, central part of the routing key
*/
- public function getGroup () {
+ public function getGroup () : string {
// If the payload is about some repository matching a table of
// symbols, we need to sort it to the right group.
foreach ($this->configuration->map as $mapping) {
foreach ($this->story->getProjects() as $project) {
if ($mapping->doesItemBelong($project)) {
return $mapping->group;
}
}
}
// Words
foreach ($this->configuration->map as $mapping) {
if ($mapping->doesStoryBelong($this->story)) {
return $mapping->group;
}
}
// By default, fallback group is the project name or a specified value.
if (empty($this->configuration->defaultGroup)) {
return strtolower($this->project);
}
return $this->configuration->defaultGroup;
}
}
diff --git a/app/Console/Commands/ConfigShow.php b/app/Console/Commands/ConfigShow.php
index 367f7a9..86f11ba 100644
--- a/app/Console/Commands/ConfigShow.php
+++ b/app/Console/Commands/ConfigShow.php
@@ -1,134 +1,131 @@
<?php
namespace Nasqueron\Notifications\Console\Commands;
use Illuminate\Console\Command;
use Nasqueron\Notifications\Features;
+use Nasqueron\Notifications\Services\Service;
use Config;
use ProjectsMap;
use Services;
class ConfigShow extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'config:show';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Show notifications center configuration';
/**
* Creates a new command instance.
- *
- * @return void
*/
public function __construct () {
parent::__construct();
}
///
/// Prepare information tables
///
/**
- * Gets the services (defined in credentials.json) as table rows
+ * Gets the services (defined in credentials.json) as table rows.
*
- * @return array
+ * @return \Nasqueron\Notifications\Services\Service[]
*/
- protected function getServicesTableRows () {
+ protected function getServicesTableRows () : array {
$rows = [];
foreach (Services::get() as $service) {
$rows[] = [
$service->gate,
$service->door,
$service->getInstanceName(),
$this->getServiveStatus($service)
];
}
return $rows;
}
/**
- * Gets service status
+ * Gets service status.
*
- * @param $service The service to check
+ * @param \Nasqueron\Notifications\Services\Service $service The service to check
* @return string A description of the issue if something is wrong; otherwise, "✓".
*/
- protected function getServiveStatus ($service) {
+ protected function getServiveStatus (Service $service) : string {
if ($service->gate === 'Phabricator') {
// Ensure the projects map is cached
$map = \ProjectsMap::fetch($service->door);
if (!$map->isCached()) {
return "Projects map not cached.";
}
}
return "✓";
}
/**
* Gets features as table rows
*
* @return array
*/
- protected function getFeaturesTableRows () {
+ protected function getFeaturesTableRows () : array {
$rows = [];
foreach (Features::getAll() as $key => $value) {
if ($value) {
$checkMark = '✓';
} else {
$checkMark = '';
}
$rows[] = [$key, $checkMark];
}
return $rows;
}
///
/// Handle the command
///
/**
* Executes the console command.
- *
- * @return mixed
*/
- public function handle () {
+ public function handle () : void {
$this->printGates();
$this->printFeatures();
$this->printServices();
}
- protected final function printGates () {
+ protected final function printGates () : void {
$this->info("Gates:\n");
foreach (Config::get('gate.controllers') as $gate) {
$this->line('- ' . $gate);
}
}
- protected final function printFeatures () {
+ protected final function printFeatures () : void {
$this->info("\nFeatures:\n");
$this->table(
['Feature', 'Enabled'],
$this->getFeaturesTableRows()
);
}
- protected final function printServices () {
+ protected final function printServices () : void {
$this->info("\nServices declared in credentials:\n");
$this->table(
['Gate', 'Door', 'Instance', 'Status'],
$this->getServicesTableRows()
);
}
}
diff --git a/app/Console/Commands/Inspire.php b/app/Console/Commands/Inspire.php
index 1cd6283..1c69d0e 100644
--- a/app/Console/Commands/Inspire.php
+++ b/app/Console/Commands/Inspire.php
@@ -1,33 +1,29 @@
<?php
namespace Nasqueron\Notifications\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Foundation\Inspiring;
-class Inspire extends Command
-{
+class Inspire extends Command {
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'inspire';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Display an inspiring quote';
/**
- * Execute the console command.
- *
- * @return mixed
+ * Executes the console command.
*/
- public function handle()
- {
- $this->comment(PHP_EOL.Inspiring::quote().PHP_EOL);
+ public function handle() : void {
+ $this->comment(PHP_EOL . Inspiring::quote() . PHP_EOL);
}
}
diff --git a/app/Console/Commands/NotificationsPayload.php b/app/Console/Commands/NotificationsPayload.php
index 4924c9a..78deb6e 100644
--- a/app/Console/Commands/NotificationsPayload.php
+++ b/app/Console/Commands/NotificationsPayload.php
@@ -1,222 +1,214 @@
<?php
namespace Nasqueron\Notifications\Console\Commands;
+use Nasqueron\Notifications\Notification;
use Nasqueron\Notifications\Phabricator\PhabricatorStory;
use Illuminate\Console\Command;
use InvalidArgumentException;
use ReflectionClass;
class NotificationsPayload extends Command {
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'notifications:payload {service} {payload} {args*}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Gets a notification payload from a service payload';
/**
* The service to handle a payload for.
*
* @var string
*/
private $service;
/**
* The payload.
*
* @var string
*/
private $payload;
/**
* The parameters to pass to the notifications class constructor.
*
* An array with arguments' names as keys, arguments' values as values.
*
* @var array
*/
private $constructor;
- /**
- * Creates a new command instance.
- *
- * @return void
- */
- public function __construct() {
- parent::__construct();
- }
-
/**
* Executes the console command.
*/
- public function handle() {
+ public function handle() : void {
if ($this->parseArguments()) {
$this->printNotification();
}
}
/**
* Parses arguments passed to the command.
*
* @return bool true if arguments looks good; otherwise, false.
*/
- private function parseArguments () {
+ private function parseArguments () : bool {
try {
$this->parseService();
$this->parsePayload();
$this->parseConstructorParameters();
} catch (InvalidArgumentException $ex) {
$this->error($ex->getMessage());
return false;
}
return true;
}
/**
* Parses service argument.
*
* Fills it to the service property.
*
* @throws InvalidArgumentException when a notification class can't be found for the requested service.
*/
- private function parseService () {
+ private function parseService () : void {
$this->service = $this->argument('service');
if (!class_exists($this->getNotificationClass())) {
throw new InvalidArgumentException("Unknown service: $this->service");
}
}
/**
* Parses path to the payload argument.
*
* Fills the content of the file to the payload property.
*
* @throws InvalidArgumentException when payload file is not found.
*/
- private function parsePayload () {
+ private function parsePayload () : void {
$payloadFile = $this->argument('payload');
if (!file_exists($payloadFile)) {
throw new InvalidArgumentException("File not found: $payloadFile");
}
$this->payload = file_get_contents($payloadFile);
}
/**
* Parses all the extra arguments and sets the constructor property
* as an array of constructor arguments.
*
* @throws InvalidArgumentException when too many or too few arguments have been given.
*/
- private function parseConstructorParameters () {
+ private function parseConstructorParameters () : void {
$keys = $this->getNotificationConstructorParameters();
$values = $this->argument('args');
$values['payload'] = $this->payload;
$this->constructor = self::argumentsArrayCombine($keys, $values);
$this->constructor['payload'] = $this->formatPayload();
}
/**
* Formats payload to pass to constructor
*
* @return PhabricatorStory|stdClass A deserialization of the payload
*/
private function formatPayload() {
if ($this->service === "Phabricator") {
$project = $this->constructor['project'];
return PhabricatorStory::loadFromJson($project, $this->payload);
}
return json_decode($this->payload);
}
/**
* Creates an array by using one array for keys and another for its values.
*
* @param array $keys
* @param array $values
* @return array
*
* @throws InvalidArgumentException when keys and values counts don't match
*/
- public static function argumentsArrayCombine ($keys, $values) {
+ public static function argumentsArrayCombine (array $keys, array $values) : array {
$countKeys = count($keys);
$countValues = count($values);
if ($countKeys != $countValues) {
throw new InvalidArgumentException("Number of arguments mismatch: got $countValues but expected $countKeys.");
}
return array_combine($keys, $values);
}
/**
* Initializes a new instance of the relevant notification class,
* with the arguments given in the constructor property.
*
* @return \Nasqueron\Notifications\Notification
*/
- private function getNotification () {
+ private function getNotification () : Notification {
$class = $this->getNotificationClass();
$args = array_values($this->constructor);
return new $class(...$args);
}
/**
* Gets the notification in JSON format.
*
* @return string
*/
- private function formatNotification () {
+ private function formatNotification () : string {
return json_encode($this->getNotification(), JSON_PRETTY_PRINT);
}
/**
* Prints the notification for the service, payload and specified arguments.
*/
- private function printNotification () {
+ private function printNotification () : void {
$this->line($this->formatNotification());
}
/**
* Gets the notification class for the specified service.
*
* @return string
*/
- private function getNotificationClass () {
+ private function getNotificationClass () : string {
$namespace = "Nasqueron\Notifications\Notifications\\";
return $namespace . $this->service . "Notification";
}
/**
* Gets an array with the parameters to pass to the constructor
* of the notification class for the specified service.
*
* @return array
*/
- private function getNotificationConstructorParameters () {
+ private function getNotificationConstructorParameters () : array {
$parameters = [];
$class = new ReflectionClass($this->getNotificationClass());
foreach ($class->getConstructor()->getParameters() as $parameter) {
$parameters[] = $parameter->getName();
}
return $parameters;
}
}
diff --git a/app/Console/Commands/PhabricatorProjectsMap.php b/app/Console/Commands/PhabricatorProjectsMap.php
index 70f0bcc..c817fe8 100644
--- a/app/Console/Commands/PhabricatorProjectsMap.php
+++ b/app/Console/Commands/PhabricatorProjectsMap.php
@@ -1,50 +1,46 @@
<?php
namespace Nasqueron\Notifications\Console\Commands;
use Illuminate\Console\Command;
use ProjectsMap;
use Services;
class PhabricatorProjectsMap extends Command {
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'phabricator:projectsmap';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Regenerate the projects map for each Phabricator instances';
/**
- * Create a new command instance.
- *
- * @return void
+ * Creates a new command instance.
*/
public function __construct() {
parent::__construct();
}
/**
- * Execute the console command.
- *
- * @return mixed
+ * Executes the console command.
*/
- public function handle() {
+ public function handle() : void {
foreach (Services::getForGate('Phabricator') as $service) {
$this->info("Querying projects map for " . $service->instance);
$map = ProjectsMap::fetch($service->door);
$map->saveToCache();
$this->table(
['PHID', 'Project name'],
$map->toArray()
);
}
}
}
diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php
index 7aad8bf..5b93e8e 100644
--- a/app/Console/Kernel.php
+++ b/app/Console/Kernel.php
@@ -1,33 +1,32 @@
<?php
namespace Nasqueron\Notifications\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
- * @var array
+ * @var string[]
*/
protected $commands = [
\Nasqueron\Notifications\Console\Commands\ConfigShow::class,
\Nasqueron\Notifications\Console\Commands\Inspire::class,
\Nasqueron\Notifications\Console\Commands\NotificationsPayload::class,
\Nasqueron\Notifications\Console\Commands\PhabricatorProjectsMap::class,
];
/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
- protected function schedule(Schedule $schedule)
- {
+ protected function schedule (Schedule $schedule) : void {
$schedule->command('inspire')
->hourly();
}
}
diff --git a/app/Events/DockerHubPayloadEvent.php b/app/Events/DockerHubPayloadEvent.php
index d3ee44c..f82d623 100644
--- a/app/Events/DockerHubPayloadEvent.php
+++ b/app/Events/DockerHubPayloadEvent.php
@@ -1,53 +1,53 @@
<?php
namespace Nasqueron\Notifications\Events;
use Nasqueron\Notifications\Events\Event;
use Illuminate\Queue\SerializesModels;
class DockerHubPayloadEvent extends Event {
use SerializesModels;
/**
* The gate door which receives the request
* @var string
*/
public $door;
/**
* The event triggering this request
* @var string
*/
public $event;
/**
* The request content, as a structured data
* @var \stdClass
*/
public $payload;
/**
* Gets event according the kind of payload we receive.
*
* @return string
*/
- public function getEvent () {
+ public function getEvent () : string {
if (isset($this->payload->repository->repo_url)) {
return "push";
}
return "buildFailure";
}
/**
* Creates a new event instance.
*
* @param string $door
* @param \stdClass $payload
*/
public function __construct($door, $payload) {
$this->door = $door;
$this->payload = $payload;
$this->event = $this->getEvent();
}
}
diff --git a/app/Facades/Broker.php b/app/Facades/Broker.php
index 9538b7f..9b1f6b7 100644
--- a/app/Facades/Broker.php
+++ b/app/Facades/Broker.php
@@ -1,21 +1,21 @@
<?php
namespace Nasqueron\Notifications\Facades;
use Illuminate\Support\Facades\Facade;
/**
* @see \Keruald\Broker\Broker
*/
class Broker extends Facade {
/**
* Gets the registered name of the component.
*
* @return string
*/
- protected static function getFacadeAccessor() {
+ protected static function getFacadeAccessor() : string {
return 'broker';
}
}
diff --git a/app/Facades/DockerHub.php b/app/Facades/DockerHub.php
index 6b409dd..573905f 100644
--- a/app/Facades/DockerHub.php
+++ b/app/Facades/DockerHub.php
@@ -1,21 +1,21 @@
<?php
namespace Nasqueron\Notifications\Facades;
use Illuminate\Support\Facades\Facade;
/**
* @see \Keruald\DockerHub\Build\TriggerBuildFactory
*/
class DockerHub extends Facade {
/**
* Gets the registered name of the component.
*
* @return string
*/
- protected static function getFacadeAccessor() {
+ protected static function getFacadeAccessor() : string {
return 'dockerhub';
}
}
diff --git a/app/Facades/Mailgun.php b/app/Facades/Mailgun.php
index ac8629a..5ca14eb 100644
--- a/app/Facades/Mailgun.php
+++ b/app/Facades/Mailgun.php
@@ -1,21 +1,21 @@
<?php
namespace Nasqueron\Notifications\Facades;
use Illuminate\Support\Facades\Facade;
/**
* @see \Keruald\Mailgun\MailgunMessageFactory
*/
class Mailgun extends Facade {
/**
* Gets the registered name of the component.
*
* @return string
*/
- protected static function getFacadeAccessor() {
+ protected static function getFacadeAccessor() : string {
return 'mailgun';
}
}
diff --git a/app/Facades/PhabricatorAPI.php b/app/Facades/PhabricatorAPI.php
index 4b76d01..b3235a2 100644
--- a/app/Facades/PhabricatorAPI.php
+++ b/app/Facades/PhabricatorAPI.php
@@ -1,21 +1,21 @@
<?php
namespace Nasqueron\Notifications\Facades;
use Illuminate\Support\Facades\Facade;
/**
* @see \Nasqueron\Notifications\Phabricator\PhabricatorAPIFactory
*/
class PhabricatorAPI extends Facade {
/**
* Gets the registered name of the component.
*
* @return string
*/
- protected static function getFacadeAccessor() {
+ protected static function getFacadeAccessor() : string {
return 'phabricator-api';
}
}
diff --git a/app/Facades/ProjectsMap.php b/app/Facades/ProjectsMap.php
index 55c9ca1..ccac4dc 100644
--- a/app/Facades/ProjectsMap.php
+++ b/app/Facades/ProjectsMap.php
@@ -1,21 +1,21 @@
<?php
namespace Nasqueron\Notifications\Facades;
use Illuminate\Support\Facades\Facade;
/**
* @see \Nasqueron\Notifications\Phabricator\PhabricatorAPIFactory
*/
class ProjectsMap extends Facade {
/**
* Gets the registered name of the component.
*
* @return string
*/
- protected static function getFacadeAccessor() {
+ protected static function getFacadeAccessor() : string {
return 'phabricator-projectsmap';
}
}
diff --git a/app/Facades/Raven.php b/app/Facades/Raven.php
index e584ae1..b1745f2 100644
--- a/app/Facades/Raven.php
+++ b/app/Facades/Raven.php
@@ -1,29 +1,29 @@
<?php
namespace Nasqueron\Notifications\Facades;
use Illuminate\Support\Facades\Facade;
use Config;
/**
* @see \Raven_Client
*/
class Raven extends Facade {
/**
* Gets the registered name of the component.
*
* @return string
*/
- protected static function getFacadeAccessor() {
+ protected static function getFacadeAccessor() : string {
return 'raven';
}
/**
* Determines if a Sentry DSN is provided in the configuration
*/
- public static function isConfigured () {
+ public static function isConfigured () : bool {
return Config::get('services.sentry.dsn') !== null;
}
}
diff --git a/app/Facades/Report.php b/app/Facades/Report.php
index 3b083ee..6fe1da4 100644
--- a/app/Facades/Report.php
+++ b/app/Facades/Report.php
@@ -1,21 +1,21 @@
<?php
namespace Nasqueron\Notifications\Facades;
use Illuminate\Support\Facades\Facade;
/**
* @see \Keruald\Broker\Broker
*/
class Report extends Facade {
/**
* Gets the registered name of the component.
*
* @return string
*/
- protected static function getFacadeAccessor() {
+ protected static function getFacadeAccessor() : string {
return 'report';
}
}
diff --git a/app/Facades/Services.php b/app/Facades/Services.php
index 668c40f..330666f 100644
--- a/app/Facades/Services.php
+++ b/app/Facades/Services.php
@@ -1,21 +1,21 @@
<?php
namespace Nasqueron\Notifications\Facades;
use Illuminate\Support\Facades\Facade;
/**
* @see \Nasqueron\Notifications\Services\Services
*/
class Services extends Facade {
/**
* Gets the registered name of the component.
*
* @return string
*/
- protected static function getFacadeAccessor() {
+ protected static function getFacadeAccessor() : string {
return 'services';
}
}
diff --git a/app/Features.php b/app/Features.php
index bd75776..c6d2c53 100644
--- a/app/Features.php
+++ b/app/Features.php
@@ -1,91 +1,91 @@
<?php
namespace Nasqueron\Notifications;
use Config;
/**
* The features class offers a sugar syntax to check if a feature is enabled
* in the Config repository, at app.features.
*
* Features could be added to config/app.php to the features array.
*/
class Features {
///
/// Feature information
///
/**
* @param string $feature The feature to get the config key
* @return string The config key
*/
- private static function getFeatureConfigKey ($feature) {
+ private static function getFeatureConfigKey (string $feature) : string {
return 'app.features.' . $feature;
}
/**
* Determines if the specified feature is enabled.
*
* @param string $feature The feature to check in the config
* @return bool
*/
- public static function isEnabled ($feature) {
+ public static function isEnabled (string $feature) : bool {
$key = self::getFeatureConfigKey($feature);
return Config::has($key) && (bool)Config::get($key);
}
/**
* Enables a feature in our current configuration instance.
*
* @param string $feature The feature
*/
- public static function enable ($feature) {
+ public static function enable (string $feature) : void {
$key = self::getFeatureConfigKey($feature);
Config::set($key, true);
}
/**
* Disables a feature in our current configuration instance.
*
* @param string $feature The feature
*/
- public static function disable ($feature) {
+ public static function disable (string $feature) : void {
$key = self::getFeatureConfigKey($feature);
Config::set($key, false);
}
///
/// Features lists
///
/**
* Gets all the features, with the toggle status.
*
* @return array An array with features as keys, bool as values (true if enabled)
*/
- public static function getAll () {
+ public static function getAll () : array {
return Config::get('app.features');
}
/**
* Lists all the features.
*
* @return string[] a list of all features
*/
- public static function getAvailable () {
+ public static function getAvailable () : array {
$features = self::getAll();
return array_keys($features);
}
/**
* Lists the enabled features
*
* @return string[] a list of enabled features
*/
- public static function getEnabled () {
+ public static function getEnabled () : array {
$features = self::getAll();
$enabledFeatures = array_filter($features);
return array_keys($enabledFeatures);
}
}
diff --git a/app/Http/Controllers/Gate/DockerHubGateController.php b/app/Http/Controllers/Gate/DockerHubGateController.php
index db7e05f..61e6e22 100644
--- a/app/Http/Controllers/Gate/DockerHubGateController.php
+++ b/app/Http/Controllers/Gate/DockerHubGateController.php
@@ -1,86 +1,88 @@
<?php
namespace Nasqueron\Notifications\Http\Controllers\Gate;
+use Nasqueron\Notifications\Events\DockerHubPayloadEvent;
+
+use Symfony\Component\HttpFoundation\Response;
+
use Event;
use Request;
-use Nasqueron\Notifications\Events\DockerHubPayloadEvent;
-
class DockerHubGateController extends GateController {
///
/// Private members
///
/**
* The request content, as a structured data
*
* @var \stdClass
*/
private $payload;
/**
* The request content
*
* @var string
*/
private $rawRequestContent;
///
/// Constants
///
/**
* The name of the service this gate accepts payload from.
*/
const SERVICE_NAME = 'DockerHub';
///
/// Request processing
///
/**
* Handles POST requests
*
* @param Request $request the HTTP request
- * @return \Illuminate\Http\Response
+ * @return \Symfony\Component\HttpFoundation\Response
*/
- public function onPost ($door) {
+ public function onPost ($door) : Response {
// Parses the request and check if it's legit
$this->door = $door;
$this->extractPayload();
// Process the request
$this->logRequest();
$this->onPayload();
// Output
return parent::renderReport();
}
/**
* Extracts payload from the request
*/
protected function extractPayload () {
$request = Request::instance();
$this->rawRequestContent = $request->getContent();
$this->payload = json_decode($this->rawRequestContent);
}
///
/// Payload processing
///
protected function onPayload () {
$this->initializeReport();
Event::fire(new DockerHubPayloadEvent(
$this->door,
$this->payload
));
}
}
diff --git a/app/Http/Controllers/Gate/GateController.php b/app/Http/Controllers/Gate/GateController.php
index c12677a..adaf680 100644
--- a/app/Http/Controllers/Gate/GateController.php
+++ b/app/Http/Controllers/Gate/GateController.php
@@ -1,114 +1,120 @@
<?php
namespace Nasqueron\Notifications\Http\Controllers\Gate;
use Nasqueron\Notifications\Features;
use Nasqueron\Notifications\Http\Controllers\Controller;
+use Nasqueron\Notifications\Services\Service;
+
+use Symfony\Component\HttpFoundation\Response as BaseResponse;
+use Illuminate\View\View;
use App;
use Log;
use Report;
use Response;
use Services;
/**
* Represents a controller handling an entry-point for API payloads
*/
class GateController extends Controller {
///
/// Private members
///
/**
* @var string
*/
protected $door;
///
/// Requests
///
/**
* Handles GET requests
*/
- public function onGet () {
+ public function onGet () : View {
// Virtually all the push APIs will send they payloads
// using a POST request, so we can provide a sensible
// default GET error message.
return view('gate/ispostonly');
}
/**
* Logs the request
*/
- protected function logRequest ($extraContextualData = []) {
+ protected function logRequest (array $extraContextualData = []) : void {
Log::info('[Gate] New payload.', [
'service' => static::SERVICE_NAME,
'door' => $this->door,
] + $extraContextualData);
}
///
/// Reports
///
/**
* Initializes the report and registers it
*/
- protected function initializeReport () {
+ protected function initializeReport () : void {
if (Features::isEnabled('ActionsReport')) {
Report::attachToGate(static::SERVICE_NAME, $this->door);
}
}
/**
* Renders the report
*
- * @return \Illuminate\Http\Response|null
+ * @return \Symfony\Component\HttpFoundation\Response
*/
- protected function renderReport () {
- if (Features::isEnabled('ActionsReport')) {
- $report = App::make('report');
- $statusCode = $report->containsError() ? 503 : 200;
- return Response::json($report)
- ->setStatusCode($statusCode);
+ protected function renderReport () : BaseResponse {
+ if (!Features::isEnabled('ActionsReport')) {
+ return response("");
}
+
+ $report = App::make('report');
+ $statusCode = $report->containsError() ? 503 : 200;
+ return Response::json($report)
+ ->setStatusCode($statusCode);
}
///
/// Credentials
///
/**
* Gets service credentials for this gate and door
*
- * @return \stdClass the service credentials
+ * @return Nasqueron\Notifications\Services\Service|null The service information is found; otherwise, null.
*/
- public function getService () {
+ public function getService () : ?Service {
return Services::findServiceByDoor(static::SERVICE_NAME, $this->door);
}
/**
- * Checks if a registered service exists for this service and door
+ * Checks if a registered service exists for this service and door.
*/
- protected function doesServiceExist () {
+ protected function doesServiceExist () : bool {
return $this->getService() !== null;
}
/**
* Gets secret for this service and door.
*
* @return string the secret, or if unknown, an empty string
*/
- protected function getSecret () {
+ protected function getSecret () : string {
$service= $this->getService();
if ($service !== null) {
return $service->secret;
}
return "";
}
}
diff --git a/app/Http/Controllers/Gate/GitHubGateController.php b/app/Http/Controllers/Gate/GitHubGateController.php
index 7d8ee78..4b1ee0b 100644
--- a/app/Http/Controllers/Gate/GitHubGateController.php
+++ b/app/Http/Controllers/Gate/GitHubGateController.php
@@ -1,192 +1,194 @@
<?php
namespace Nasqueron\Notifications\Http\Controllers\Gate;
-use Event;
-use Request;
-
use Nasqueron\Notifications\Events\GitHubPayloadEvent;
+
use Keruald\GitHub\XHubSignature;
+use Symfony\Component\HttpFoundation\Response;
+
+use Event;
+use Request;
class GitHubGateController extends GateController {
///
/// Private members
///
/**
* The request signature, allowing to determine if the payload is legit
*
* @var string
*/
private $signature;
/**
* The GitHub event triggering this request
*
* @var string
*/
private $event;
/**
* The request delivery GUID
*
* @var string
*/
private $delivery;
/**
* The request content, as a structured data
*
* @var \stdClass
*/
private $payload;
/**
* The request content
*
* @var string
*/
private $rawRequestContent;
///
/// Constants
///
/**
* The name of the service this gate accepts payload from.
*/
const SERVICE_NAME = 'GitHub';
///
/// Request processing
///
/**
* Handles POST requests
*
* @param Request $request the HTTP request
- * @return \Illuminate\Http\Response
+ * @return \Symfony\Component\HttpFoundation\Response
*/
- public function onPost ($door) {
+ public function onPost ($door) : Response {
// Parses the request and check if it's legit
$this->door = $door;
$this->extractHeaders();
$this->extractPayload();
if (!$this->isLegitRequest()) {
abort(403, 'Unauthorized action.');
}
if (!$this->isValidRequest()) {
abort(400, 'Bad request.');
}
// Process the request
$this->logGateRequest();
$this->onPayload();
// Output
return parent::renderReport();
}
/**
* Extracts headers from the request
*/
protected function extractHeaders () {
$this->signature = $this->getSignature();
$this->event = Request::header('X-Github-Event');
$this->delivery = Request::header('X-Github-Delivery');
}
/**
* Gets the signature from an X-Hub-Signature header
*
* @param string the signature part of the header
*/
private function getSignature () {
$headerSignature = Request::header('X-Hub-Signature');
return XHubSignature::parseSignature($headerSignature);
}
/**
* Extracts payload from the request
*/
protected function extractPayload () {
$request = Request::instance();
$this->rawRequestContent = $request->getContent();
$this->payload = json_decode($this->rawRequestContent);
}
/**
* Determines if the request is valid, ie contains the mandatory headers
* and a payload.
*
* @return bool true if the request looks valid; otherwise, false.
*/
protected function isValidRequest () {
if (empty($this->event)) {
return false;
}
if (empty($this->delivery)) {
return false;
}
if (empty($this->payload) || !is_object($this->payload)) {
return false;
}
return true;
}
/**
* Determines if the request is legit.
*
* @return bool true if the request looks legit; otherwise, false.
*/
protected function isLegitRequest () {
$secret = $this->getSecret();
// If the secret is not defined, request legitimation is bypassed
if (empty($secret)) {
return true;
}
// If the secret is defined, but signature is missing from the
// request, we don't need to perform any other validation.
if (empty($this->signature)) {
return false;
}
return XHubSignature::validatePayload(
$secret,
$this->rawRequestContent,
$this->signature
);
}
/**
* Logs the request
*/
protected function logGateRequest () {
$this->logRequest([
'delivery' => $this->delivery,
'event' => $this->event,
]);
}
///
/// Payload processing
///
protected function onPayload () {
$this->initializeReport();
Event::fire(new GitHubPayloadEvent(
$this->door,
$this->event,
$this->payload
));
}
}
diff --git a/app/Http/Controllers/Gate/JenkinsGateController.php b/app/Http/Controllers/Gate/JenkinsGateController.php
index 901f0b1..596566a 100644
--- a/app/Http/Controllers/Gate/JenkinsGateController.php
+++ b/app/Http/Controllers/Gate/JenkinsGateController.php
@@ -1,86 +1,88 @@
<?php
namespace Nasqueron\Notifications\Http\Controllers\Gate;
+use Nasqueron\Notifications\Events\JenkinsPayloadEvent;
+
+use Symfony\Component\HttpFoundation\Response;
+
use Event;
use Request;
-use Nasqueron\Notifications\Events\JenkinsPayloadEvent;
-
class JenkinsGateController extends GateController {
///
/// Private members
///
/**
* The request content, as a structured data
*
* @var \stdClass
*/
private $payload;
/**
* The request content
*
* @var string
*/
private $rawRequestContent;
///
/// Constants
///
/**
* The name of the service this gate accepts payload from.
*/
const SERVICE_NAME = 'Jenkins';
///
/// Request processing
///
/**
* Handles POST requests
*
* @param Request $request the HTTP request
- * @return \Illuminate\Http\Response
+ * @return \Symfony\Component\HttpFoundation\Response
*/
- public function onPost ($door) {
+ public function onPost ($door) : Response {
// Parses the request and check if it's legit
$this->door = $door;
$this->extractPayload();
// Process the request
$this->logRequest();
$this->onPayload();
// Output
return parent::renderReport();
}
/**
* Extracts payload from the request
*/
protected function extractPayload () {
$request = Request::instance();
$this->rawRequestContent = $request->getContent();
$this->payload = json_decode($this->rawRequestContent);
}
///
/// Payload processing
///
protected function onPayload () {
$this->initializeReport();
Event::fire(new JenkinsPayloadEvent(
$this->door,
$this->payload
));
}
}
diff --git a/app/Http/Controllers/Gate/PhabricatorGateController.php b/app/Http/Controllers/Gate/PhabricatorGateController.php
index 5f9a58e..d69a25a 100644
--- a/app/Http/Controllers/Gate/PhabricatorGateController.php
+++ b/app/Http/Controllers/Gate/PhabricatorGateController.php
@@ -1,75 +1,78 @@
<?php
namespace Nasqueron\Notifications\Http\Controllers\Gate;
+use Nasqueron\Notifications\Events\PhabricatorPayloadEvent;
+
+use Symfony\Component\HttpFoundation\Response;
+
use Event;
use Request;
-use Nasqueron\Notifications\Events\PhabricatorPayloadEvent;
-
class PhabricatorGateController extends GateController {
///
/// Private members
///
/**
* The request content, as a structured data
*
* @var array
*/
private $payload;
///
/// Constants
///
/**
* The name of the service this gate accepts payload from.
*/
const SERVICE_NAME = 'Phabricator';
///
/// Requests processing
///
/**
* Handles POST requests
*
* @param Request $request the HTTP request
+ * @return \Symfony\Component\HttpFoundation\Response
*/
- public function onPost ($door) {
+ public function onPost ($door) : Response {
$this->door = $door;
if (!$this->doesServiceExist()) {
abort(404, 'Unknown Phabricator instance.');
}
$this->extractPayload();
$this->logRequest();
$this->onPayload();
return parent::renderReport();
}
/**
* Extracts payload from the request
*/
protected function extractPayload () {
$this->payload = Request::all();
}
///
/// Payload processing
///
protected function onPayload () {
$this->initializeReport();
Event::fire(new PhabricatorPayloadEvent(
$this->door,
$this->payload
));
}
}
diff --git a/app/Http/Middleware/EncryptCookies.php b/app/Http/Middleware/EncryptCookies.php
index 3f095eb..38b485a 100644
--- a/app/Http/Middleware/EncryptCookies.php
+++ b/app/Http/Middleware/EncryptCookies.php
@@ -1,17 +1,17 @@
<?php
namespace Nasqueron\Notifications\Http\Middleware;
use Illuminate\Cookie\Middleware\EncryptCookies as BaseEncrypter;
class EncryptCookies extends BaseEncrypter
{
/**
* The names of the cookies that should not be encrypted.
*
- * @var array
+ * @var string[]
*/
protected $except = [
//
];
}
diff --git a/app/Http/Middleware/VerifyCsrfToken.php b/app/Http/Middleware/VerifyCsrfToken.php
index 3d98198..a6160ec 100644
--- a/app/Http/Middleware/VerifyCsrfToken.php
+++ b/app/Http/Middleware/VerifyCsrfToken.php
@@ -1,17 +1,17 @@
<?php
namespace Nasqueron\Notifications\Http\Middleware;
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as BaseVerifier;
class VerifyCsrfToken extends BaseVerifier
{
/**
* The URIs that should be excluded from CSRF verification.
*
- * @var array
+ * @var string[]
*/
protected $except = [
'gate/*',
];
}
diff --git a/app/Jobs/FireDockerHubNotification.php b/app/Jobs/FireDockerHubNotification.php
index cde4752..1b035ec 100644
--- a/app/Jobs/FireDockerHubNotification.php
+++ b/app/Jobs/FireDockerHubNotification.php
@@ -1,55 +1,53 @@
<?php
namespace Nasqueron\Notifications\Jobs;
use Nasqueron\Notifications\Notifications\DockerHubNotification;
use Nasqueron\Notifications\Events\DockerHubPayloadEvent;
use Nasqueron\Notifications\Events\NotificationEvent;
use Nasqueron\Notifications\Jobs\Job;
use Event;
class FireDockerHubNotification extends Job {
/**
* @var DockerHubPayloadEvent;
*/
private $event;
/**
* Initializes a new instance of FireDockerHubNotification
*
* @param DockerHubPayloadEvent $event The event to notify
*/
public function __construct (DockerHubPayloadEvent $event) {
$this->event = $event;
}
///
/// Task
///
/**
* Executes the job.
- *
- * @return void
*/
- public function handle() {
+ public function handle() : void {
$notification = $this->createNotification();
Event::fire(new NotificationEvent($notification));
}
/**
* Creates a DockerHub notification
*
* @param DockerHubPayloadEvent $event
* @return \Nasqueron\Notifications\Notification The notification
*/
- protected function createNotification() {
+ protected function createNotification() : DockerHubNotification {
return new DockerHubNotification(
$this->event->door, // project
$this->event->event, // event type
$this->event->payload // raw content
);
}
}
diff --git a/app/Jobs/FireGitHubNotification.php b/app/Jobs/FireGitHubNotification.php
index 4c0bc9e..234e90e 100644
--- a/app/Jobs/FireGitHubNotification.php
+++ b/app/Jobs/FireGitHubNotification.php
@@ -1,55 +1,53 @@
<?php
namespace Nasqueron\Notifications\Jobs;
use Nasqueron\Notifications\Notifications\GitHubNotification;
use Nasqueron\Notifications\Events\GitHubPayloadEvent;
use Nasqueron\Notifications\Events\NotificationEvent;
use Nasqueron\Notifications\Jobs\Job;
use Event;
class FireGitHubNotification extends Job {
/**
* @var GitHubPayloadEvent;
*/
private $event;
/**
* Initializes a new instance of FireGitHubNotification
*
* @param GitHubPayloadEvent $event The event to notify
*/
public function __construct (GitHubPayloadEvent $event) {
$this->event = $event;
}
///
/// Task
///
/**
* Executes the job.
- *
- * @return void
*/
- public function handle() {
+ public function handle() : void {
$notification = $this->createNotification();
Event::fire(new NotificationEvent($notification));
}
/**
* Creates a GitHub notification
*
* @param GitHubPayloadEvent $event
* @return \Nasqueron\Notifications\Notification The notification
*/
- protected function createNotification() {
+ protected function createNotification() : GitHubNotification {
return new GitHubNotification(
$this->event->door, // project
$this->event->event, // event type
$this->event->payload // raw content
);
}
}
diff --git a/app/Jobs/FireJenkinsNotification.php b/app/Jobs/FireJenkinsNotification.php
index 051991c..48f1d0d 100644
--- a/app/Jobs/FireJenkinsNotification.php
+++ b/app/Jobs/FireJenkinsNotification.php
@@ -1,56 +1,56 @@
<?php
namespace Nasqueron\Notifications\Jobs;
use Nasqueron\Notifications\Notifications\JenkinsNotification;
use Nasqueron\Notifications\Events\JenkinsPayloadEvent;
use Nasqueron\Notifications\Events\NotificationEvent;
use Nasqueron\Notifications\Jobs\Job;
use Event;
class FireJenkinsNotification extends Job {
/**
* @var JenkinsPayloadEvent;
*/
private $event;
/**
* Initializes a new instance of FireJenkinsNotification
*
* @param JenkinsPayloadEvent $event The event to notify
*/
public function __construct (JenkinsPayloadEvent $event) {
$this->event = $event;
}
///
/// Task
///
/**
* Executes the job.
*
* @return void
*/
- public function handle() {
+ public function handle() : void {
$notification = $this->createNotification();
if ($notification->shouldNotify()) {
Event::fire(new NotificationEvent($notification));
}
}
/**
* Creates a Jenkins notification
*
* @param JenkinsPayloadEvent $event
* @return \Nasqueron\Notifications\Notification The notification
*/
- protected function createNotification() {
+ protected function createNotification() : JenkinsNotification {
return new JenkinsNotification(
$this->event->door, // project
$this->event->payload // raw content
);
}
}
diff --git a/app/Jobs/FirePhabricatorNotification.php b/app/Jobs/FirePhabricatorNotification.php
index 593c347..b513bad 100644
--- a/app/Jobs/FirePhabricatorNotification.php
+++ b/app/Jobs/FirePhabricatorNotification.php
@@ -1,54 +1,52 @@
<?php
namespace Nasqueron\Notifications\Jobs;
use Nasqueron\Notifications\Notifications\PhabricatorNotification;
use Nasqueron\Notifications\Events\PhabricatorPayloadEvent;
use Nasqueron\Notifications\Events\NotificationEvent;
use Nasqueron\Notifications\Jobs\Job;
use Event;
class FirePhabricatorNotification extends Job {
/**
* @var PhabricatorPayloadEvent;
*/
private $event;
/**
* Initializes a new instance of FirePhabricatorNotification
*
* @param PhabricatorPayloadEvent $event The event to notify
*/
public function __construct (PhabricatorPayloadEvent $event) {
$this->event = $event;
}
///
/// Task
///
/**
* Executes the job.
- *
- * @return void
*/
- public function handle() {
+ public function handle() : void {
$notification = $this->createNotification();
Event::fire(new NotificationEvent($notification));
}
/**
* Creates a Phabricator notification
*
* @param PhabricatorPayloadEvent $event
* @return \Nasqueron\Notifications\Notification The notification
*/
- protected function createNotification() {
+ protected function createNotification() : PhabricatorNotification {
return new PhabricatorNotification(
$this->event->door, // Project
$this->event->story // Story
);
}
}
diff --git a/app/Jobs/NotifyNewCommitsToDiffusion.php b/app/Jobs/NotifyNewCommitsToDiffusion.php
index 256ef14..336f4df 100644
--- a/app/Jobs/NotifyNewCommitsToDiffusion.php
+++ b/app/Jobs/NotifyNewCommitsToDiffusion.php
@@ -1,196 +1,196 @@
<?php
namespace Nasqueron\Notifications\Jobs;
use Nasqueron\Notifications\Actions\ActionError;
use Nasqueron\Notifications\Actions\NotifyNewCommitsAction;
use Nasqueron\Notifications\Events\ReportEvent;
use Nasqueron\Notifications\Phabricator\PhabricatorAPI as API;
use Nasqueron\Notifications\Phabricator\PhabricatorAPIException;
use Event;
use Log;
use PhabricatorAPI;
/**
* This class allows to notify Phabricator of new commits, so daemons can pull
* these new commits and add them into Diffusion.
*/
class NotifyNewCommitsToDiffusion extends Job {
///
/// Private members
///
/**
* The clone URL of the repository
*
* @var string
*/
private $repository;
/**
* @var \Nasqueron\Notifications\Phabricator\PhabricatorAPI
*/
private $api;
/**
* @var string
*/
private $callSign;
/**
* @var NotifyNewCommitsAction
*/
private $actionToReport;
/**
* @var string
*/
private $sourceProject;
///
/// Constructor
///
/**
* Initializes a new instance of NotifyNewCommitsToDiffusion.
*/
public function __construct ($sourceProject, $repository) {
$this->sourceProject = $sourceProject;
$this->repository = $repository;
}
///
/// Task
///
/**
* Executes the job.
*
* @return void
*/
- public function handle () {
+ public function handle () : void {
if (!$this->fetchRequirements()) {
return;
}
$this->initializeReport();
$this->notifyPhabricator();
$this->sendReport();
}
/**
* Initializes the actions report.
*/
- private function initializeReport () {
+ private function initializeReport () : void {
$this->actionToReport = new NotifyNewCommitsAction($this->callSign);
}
/**
* Notifies Phabricator to pull from the repository.
*/
- private function notifyPhabricator () {
+ private function notifyPhabricator () : void {
try {
$this->callDiffusionLookSoon();
} catch (PhabricatorAPIException $ex) {
$actionError = new ActionError($ex);
$this->actionToReport->attachError($actionError);
Log::error($ex);
}
}
/**
* Fires a report event with the actions report.
*/
- private function sendReport () {
+ private function sendReport () : void {
$event = new ReportEvent($this->actionToReport);
Event::fire($event);
}
///
/// Helper methods to find correct Phabricator instance and get the API
///
/**
* Gets the relevant Phabricator project for the specified source project.
*
* @return string The Phabricator project name
*/
- private function getPhabricatorProject () {
+ private function getPhabricatorProject () : string {
return $this->sourceProject;
}
///
/// Helper methods to populate object members
///
/**
* Fetches API and call sign.
*
* @return bool true if all requirement have been fetched ; otherwise, false.
*/
- private function fetchRequirements () {
+ private function fetchRequirements () : bool {
return $this->fetchAPI() && $this->fetchCallSign();
}
/**
* Fetches the Phabricator API to use for the current source project.
*
* @return bool true if an API instance has been fetch ; otherwise, false.
*/
- private function fetchAPI () {
+ private function fetchAPI () : bool {
$project = $this->getPhabricatorProject();
$this->api = PhabricatorAPI::getForProject($project);
return $this->api !== null;
}
/**
* Fetches the call sign matching the repository.
*
* @return bool true if a call sign have been found ; otherwise, false.
*/
- private function fetchCallSign () {
+ private function fetchCallSign () : bool {
$this->callSign = $this->getCallSign();
return $this->callSign !== "";
}
///
/// Helper methods to query Phabricator API
///
/**
* Gets the call sign matching the repository URL.
*
* @return string the repository call sign "OPS", or "" if not in Phabricator
*/
- private function getCallSign () {
+ private function getCallSign () : string {
$reply = $this->api->call(
'repository.query',
[ 'remoteURIs[0]' => $this->repository ]
);
if (!count($reply)) {
return "";
}
return API::getFirstResult($reply)->callsign;
}
/**
* Calls the diffusion.looksoon API method.
*
* @throws PhabricatorAPIException
*/
- private function callDiffusionLookSoon () {
+ private function callDiffusionLookSoon () : void {
$this->api->call(
'diffusion.looksoon',
[ 'callsigns[0]' => $this->callSign ]
);
}
}
diff --git a/app/Jobs/SendMessageToBroker.php b/app/Jobs/SendMessageToBroker.php
index b86715e..9e739a9 100644
--- a/app/Jobs/SendMessageToBroker.php
+++ b/app/Jobs/SendMessageToBroker.php
@@ -1,108 +1,109 @@
<?php
namespace Nasqueron\Notifications\Jobs;
use Nasqueron\Notifications\Actions\ActionError;
use Nasqueron\Notifications\Actions\AMQPAction;
use Nasqueron\Notifications\Events\ReportEvent;
use Nasqueron\Notifications\Jobs\Job;
use Broker;
use Event;
use Log;
class SendMessageToBroker extends Job {
///
/// Private members
///
/**
* The routing key, for topic exchange
*
* @var string
*/
private $routingKey = '';
/**
* The message to send
*
* @var string
*/
private $message = '';
/**
* The target exchange
*
* @var string
*/
private $target = '';
/**
* If not null, an exception thrown during the task
*
* @var \Exception
*/
private $exception;
///
/// Constructor
///
/**
- * Create a new job instance.
+ * Creates a new job instance.
*
- * @param string $routingKey the routing key, for topic exchange
- * @param string $message the message to send
+ * @param string $target The queue or exchange to send the message to
+ * @param string $routingKey The routing key, for topic exchange
+ * @param string $message The message to send
*
* @return void
*/
- public function __construct ($target, $routingKey, $message) {
+ public function __construct (string $target, string $routingKey, string $message) {
$this->target = $target;
$this->routingKey = $routingKey;
$this->message = $message;
}
///
/// Task
///
/**
* Executes the job.
*
* @return void
*/
- public function handle() {
+ public function handle() : void {
$this->sendMessage();
$this->report();
}
/**
- * Sends the message to the broker
+ * Sends the message to the broker.
*/
- protected function sendMessage () {
+ protected function sendMessage () : void {
try {
Broker::setExchangeTarget($this->target, "topic", true)
->routeTo($this->routingKey)
->sendMessage($this->message);
} catch (\Exception $ex) {
$this->exception = $ex;
Log::error($ex);
}
}
/**
- * Prepares a report and fires a report event
+ * Prepares a report and fires a report event.
*/
- protected function report () {
+ protected function report () : void {
$actionToReport = new AMQPAction(
"publish",
$this->target,
$this->routingKey
);
if ($this->exception !== null) {
$actionToReport->attachError(new ActionError($this->exception));
}
Event::fire(new ReportEvent($actionToReport));
}
}
diff --git a/app/Jobs/TriggerDockerHubBuild.php b/app/Jobs/TriggerDockerHubBuild.php
index 959dce1..7d3a5bb 100644
--- a/app/Jobs/TriggerDockerHubBuild.php
+++ b/app/Jobs/TriggerDockerHubBuild.php
@@ -1,86 +1,86 @@
<?php
namespace Nasqueron\Notifications\Jobs;
use Nasqueron\Notifications\Actions\ActionError;
use Nasqueron\Notifications\Actions\TriggerDockerHubBuildAction;
use Nasqueron\Notifications\Events\ReportEvent;
use DockerHub;
use Event;
use Exception;
/**
* This class allows to trigger a new Docker Hub build.
*/
class TriggerDockerHubBuild extends Job {
///
/// Private members
///
/**
* @var string The Docker image to trigger a build for
*/
private $image;
/**
* @var TriggerDockerHubBuildAction
*/
private $actionToReport;
///
/// Constructor
///
/**
* Initializes a new instance of TriggerDockerHubBuild.
*/
public function __construct ($image) {
$this->image = $image;
}
///
/// Task
///
/**
* Executes the job.
*
* @return void
*/
- public function handle () {
+ public function handle () : void {
$this->initializeReport();
$this->triggerBuild();
$this->sendReport();
}
/**
* Initializes the actions report.
*/
- private function initializeReport () {
+ private function initializeReport () : void {
$this->actionToReport = new TriggerDockerHubBuildAction($this->image);
}
/**
* Triggers a new Docker Hub build.
*/
- private function triggerBuild () {
+ private function triggerBuild () : void {
try {
DockerHub::build($this->image);
} catch (Exception $ex) {
$actionError = new ActionError($ex);
$this->actionToReport->attachError($actionError);
}
}
/**
* Fires a report event with the actions report.
*/
- private function sendReport () {
+ private function sendReport () : void {
$event = new ReportEvent($this->actionToReport);
Event::fire($event);
}
}
diff --git a/app/Listeners/AMQPEventListener.php b/app/Listeners/AMQPEventListener.php
index 4399faf..2b7a12e 100644
--- a/app/Listeners/AMQPEventListener.php
+++ b/app/Listeners/AMQPEventListener.php
@@ -1,140 +1,138 @@
<?php
namespace Nasqueron\Notifications\Listeners;
use Nasqueron\Notifications\Events\GitHubPayloadEvent;
use Nasqueron\Notifications\Events\NotificationEvent;
use Nasqueron\Notifications\Analyzers\GitHub\GitHubPayloadAnalyzer;
use Nasqueron\Notifications\Jobs\SendMessageToBroker;
use Nasqueron\Notifications\Notification;
use Config;
class AMQPEventListener {
///
/// GitHub events
///
/**
* Gets routing key, to allow consumers to select the topic they subscribe to.
*
* @param GitHubPayloadEvent $event the payload event
*/
- protected static function getGitHubEventRoutingKey (GitHubPayloadEvent $event) {
+ protected static function getGitHubEventRoutingKey (GitHubPayloadEvent $event) : string {
$key = [
strtolower($event->door),
self::getGroup($event),
$event->event
];
return implode('.', $key);
}
- protected static function getAnalyzer (GitHubPayloadEvent $event) {
+ protected static function getAnalyzer (GitHubPayloadEvent $event) : GitHubPayloadAnalyzer {
return new GitHubPayloadAnalyzer(
$event->door,
$event->event,
$event->payload
);
}
/**
* Gets the group for a specific payload
*
* @return string the group, central part of the routing key
*/
- protected static function getGroup (GitHubPayloadEvent $event) {
+ protected static function getGroup (GitHubPayloadEvent $event) : string {
$analyzer = self::getAnalyzer($event);
return $analyzer->getGroup();
}
/**
* Handles a GitHub payload event.
*
* @param GitHubPayloadEvent $event
- * @return void
*/
- public function onGitHubPayload(GitHubPayloadEvent $event) {
+ public function onGitHubPayload(GitHubPayloadEvent $event) : void {
$this->sendRawPayload($event);
}
/**
* This is our gateway GitHub Webhooks -> Broker
*
* @param GitHubPayloadEvent $event
*/
- protected function sendRawPayload(GitHubPayloadEvent $event) {
+ protected function sendRawPayload(GitHubPayloadEvent $event) : void {
$target = Config::get('broker.targets.github_events');
$routingKey = static::getGitHubEventRoutingKey($event);
$message = json_encode($event->payload);
$job = new SendMessageToBroker($target, $routingKey, $message);
$job->handle();
}
///
/// Notifications
///
/**
* Handles a notification event.
*
* @param NotificationEvent $event
- * @return void
*/
- public function onNotification(NotificationEvent $event) {
+ public function onNotification(NotificationEvent $event) : void {
$this->sendNotification($event);
}
/**
* Gets routing key, to allow consumers to select the topic they subscribe to.
*
* @param NotificationEvent $event
*/
- protected static function getNotificationRoutingKey (Notification $notification) {
+ protected static function getNotificationRoutingKey (Notification $notification) : string {
$key = [
$notification->project,
$notification->group,
$notification->service,
$notification->type
];
return strtolower(implode('.', $key));
}
/**
* This is our gateway specialized for distilled notifications
*
* @param NotificationEvent $event
*/
- protected function sendNotification(NotificationEvent $event) {
+ protected function sendNotification(NotificationEvent $event) : void {
$notification = $event->notification;
$target = Config::get('broker.targets.notifications');
$routingKey = static::getNotificationRoutingKey($notification);
$message = json_encode($notification);
$job = new SendMessageToBroker($target, $routingKey, $message);
$job->handle();
}
///
/// Events listening
///
/**
* Register the listeners for the subscriber.
*
* @param \Illuminate\Events\Dispatcher $events
*/
- public function subscribe (\Illuminate\Events\Dispatcher $events) {
+ public function subscribe (\Illuminate\Events\Dispatcher $events) : void {
$class = 'Nasqueron\Notifications\Listeners\AMQPEventListener';
$events->listen(
'Nasqueron\Notifications\Events\GitHubPayloadEvent',
"$class@onGitHubPayload"
);
$events->listen(
'Nasqueron\Notifications\Events\NotificationEvent',
"$class@onNotification"
);
}
}
diff --git a/app/Listeners/DockerHubListener.php b/app/Listeners/DockerHubListener.php
index 10cbf3b..5c8874d 100644
--- a/app/Listeners/DockerHubListener.php
+++ b/app/Listeners/DockerHubListener.php
@@ -1,81 +1,81 @@
<?php
namespace Nasqueron\Notifications\Listeners;
use Nasqueron\Notifications\Events\GitHubPayloadEvent;
use Nasqueron\Notifications\Jobs\TriggerDockerHubBuild;
use Illuminate\Events\Dispatcher;
use DockerHub;
/**
* Listens to events Docker Hub is interested by.
*/
class DockerHubListener {
///
/// GitHub → Phabricator
///
/**
* Handles payload events.
*
* @param GitHubPayloadEvent $event The GitHub payload event
*/
- public function onGitHubPayload (GitHubPayloadEvent $event) {
+ public function onGitHubPayload (GitHubPayloadEvent $event) : void {
if ($this->shouldNotify($event)) {
$this->notifyNewCommits($event);
}
}
/**
* Determines if the event should be notified to Docker Hub.
* We're interested by push events, for repos with Docker images
* we've a token to trigger a build.
*
* @param GitHubPayloadEvent $event The GitHub payload event
* @return bool
*/
- public function shouldNotify (GitHubPayloadEvent $event) {
+ public function shouldNotify (GitHubPayloadEvent $event) : bool {
return $event->event === 'push'
&& DockerHub::hasToken($this->getRepository($event));
}
/**
* Notifies Phabricator there are new commits to pull.
*
* @param GitHubPayloadEvent $event The GitHub payload event
*/
- public function notifyNewCommits (GitHubPayloadEvent $event) {
+ public function notifyNewCommits (GitHubPayloadEvent $event) : void {
$job = new TriggerDockerHubBuild($this->getRepository($event));
$job->handle();
}
/**
* Extracts repository fullname (e.g. acme/foo) from event.
*
* @var string
*/
- private function getRepository (GitHubPayloadEvent $event) {
+ private function getRepository (GitHubPayloadEvent $event) : string {
return $event->payload->repository->full_name;
}
///
/// Events listening
///
/**
* Registers the listeners for the subscriber.
*
* @param Dispatcher $events
*/
- public function subscribe (Dispatcher $events) {
+ public function subscribe (Dispatcher $events) : void {
$class = DockerHubListener::class;
$events->listen(
GitHubPayloadEvent::class,
"$class@onGitHubPayload"
);
}
}
diff --git a/app/Listeners/LastPayloadSaver.php b/app/Listeners/LastPayloadSaver.php
index 69bdf5c..3045596 100644
--- a/app/Listeners/LastPayloadSaver.php
+++ b/app/Listeners/LastPayloadSaver.php
@@ -1,53 +1,53 @@
<?php
namespace Nasqueron\Notifications\Listeners;
use Nasqueron\Notifications\Events\Event;
class LastPayloadSaver {
///
/// Events handling
///
/**
* Handles payload events
*/
- public function onPayload (Event $event) {
+ public function onPayload (Event $event) : void {
self::savePayload($event->payload);
}
/**
* Saves payload to log file
*
- * @param string $payload The payload to save
+ * @param mixed $payload The payload to save
*/
- public static function savePayload ($payload) {
+ public static function savePayload ($payload) : void {
$filename = storage_path('logs/payload.json');
$content = json_encode($payload);
file_put_contents($filename, $content);
}
///
/// Events listening
///
/**
* Register the listeners for the subscriber.
*
* @param \Illuminate\Events\Dispatcher $events
*/
- public function subscribe (\Illuminate\Events\Dispatcher $events) {
+ public function subscribe (\Illuminate\Events\Dispatcher $events) : void {
$ns = 'Nasqueron\Notifications\Events';
$class = 'Nasqueron\Notifications\Listeners\LastPayloadSaver';
$eventsToListen = [
'DockerHubPayloadEvent',
'GitHubPayloadEvent',
'JenkinsPayloadEvent',
'PhabricatorPayloadEvent',
];
foreach ($eventsToListen as $event) {
$events->listen("$ns\\$event", "$class@onPayload");
}
}
}
diff --git a/app/Listeners/NotificationListener.php b/app/Listeners/NotificationListener.php
index 6df38ad..d743766 100644
--- a/app/Listeners/NotificationListener.php
+++ b/app/Listeners/NotificationListener.php
@@ -1,94 +1,94 @@
<?php
namespace Nasqueron\Notifications\Listeners;
use Nasqueron\Notifications\Events\DockerHubPayloadEvent;
use Nasqueron\Notifications\Events\GitHubPayloadEvent;
use Nasqueron\Notifications\Events\JenkinsPayloadEvent;
use Nasqueron\Notifications\Events\PhabricatorPayloadEvent;
use Nasqueron\Notifications\Jobs\FireDockerHubNotification;
use Nasqueron\Notifications\Jobs\FireGitHubNotification;
use Nasqueron\Notifications\Jobs\FireJenkinsNotification;
use Nasqueron\Notifications\Jobs\FirePhabricatorNotification;
class NotificationListener {
///
/// Distill services' payloads into notifications
///
/**
* Handles a Docker Hub payload event.
*
* @param DockerHubPayloadEvent $event
* @return void
*/
- public function onDockerHubPayload(DockerHubPayloadEvent $event) {
+ public function onDockerHubPayload(DockerHubPayloadEvent $event) : void {
$job = new FireDockerHubNotification($event);
$job->handle();
}
/**
* Handles a GitHub payload event.
*
* @param GitHubPayloadEvent $event
* @return void
*/
- public function onGitHubPayload(GitHubPayloadEvent $event) {
+ public function onGitHubPayload(GitHubPayloadEvent $event) : void {
$job = new FireGitHubNotification($event);
$job->handle();
}
/**
* Handles a Phabricator payload event.
*
* @param PhabricatorPayloadEvent $event
* @return void
*/
- public function onPhabricatorPayload(PhabricatorPayloadEvent $event) {
+ public function onPhabricatorPayload(PhabricatorPayloadEvent $event) : void {
$job = new FirePhabricatorNotification($event);
$job->handle();
}
/**
* Handles a Jenkins payload event.
*
* @param JenkinsPayloadEvent $event
* @return void
*/
- public function onJenkinsPayload (JenkinsPayloadEvent $event) {
+ public function onJenkinsPayload (JenkinsPayloadEvent $event) : void {
$job = new FireJenkinsNotification($event);
$job->handle();
}
///
/// Events listening
///
/**
* Register the listeners for the subscriber.
*
* @param \Illuminate\Events\Dispatcher $events
*/
- public function subscribe (\Illuminate\Events\Dispatcher $events) {
+ public function subscribe (\Illuminate\Events\Dispatcher $events) : void {
$class = 'Nasqueron\Notifications\Listeners\NotificationListener';
$events->listen(
'Nasqueron\Notifications\Events\DockerHubPayloadEvent',
"$class@onDockerHubPayload"
);
$events->listen(
'Nasqueron\Notifications\Events\GitHubPayloadEvent',
"$class@onGitHubPayload"
);
$events->listen(
'Nasqueron\Notifications\Events\JenkinsPayloadEvent',
"$class@onJenkinsPayload"
);
$events->listen(
'Nasqueron\Notifications\Events\PhabricatorPayloadEvent',
"$class@onPhabricatorPayload"
);
}
}
diff --git a/app/Listeners/PhabricatorListener.php b/app/Listeners/PhabricatorListener.php
index 47a03f3..d7acc7d 100644
--- a/app/Listeners/PhabricatorListener.php
+++ b/app/Listeners/PhabricatorListener.php
@@ -1,59 +1,59 @@
<?php
namespace Nasqueron\Notifications\Listeners;
use Nasqueron\Notifications\Events\GitHubPayloadEvent;
use Nasqueron\Notifications\Jobs\NotifyNewCommitsToDiffusion;
use Illuminate\Events\Dispatcher;
/**
* Listens to events Phabricator is interested by.
*/
class PhabricatorListener {
///
/// GitHub → Phabricator
///
/**
* Handles payload events.
*
* @param GitHubPayloadEvent $event The GitHub payload event
*/
- public function onGitHubPayload (GitHubPayloadEvent $event) {
+ public function onGitHubPayload (GitHubPayloadEvent $event) : void {
if ($event->event === 'push') {
$this->notifyNewCommits($event);
}
}
/**
* Notifies Phabricator there are new commits to pull.
*
* @param GitHubPayloadEvent $event The GitHub payload event
*/
- public function notifyNewCommits (GitHubPayloadEvent $event) {
+ public function notifyNewCommits (GitHubPayloadEvent $event) : void {
$job = new NotifyNewCommitsToDiffusion(
$event->door,
$event->payload->repository->clone_url
);
$job->handle();
}
///
/// Events listening
///
/**
* Registers the listeners for the subscriber.
*
* @param Dispatcher $events
*/
- public function subscribe (Dispatcher $events) {
+ public function subscribe (Dispatcher $events) : void {
$class = PhabricatorListener::class;
$events->listen(
GitHubPayloadEvent::class,
"$class@onGitHubPayload"
);
}
}
diff --git a/app/Notifications/DockerHubNotification.php b/app/Notifications/DockerHubNotification.php
index 6cff427..744fc07 100644
--- a/app/Notifications/DockerHubNotification.php
+++ b/app/Notifications/DockerHubNotification.php
@@ -1,87 +1,88 @@
<?php
namespace Nasqueron\Notifications\Notifications;
+use Nasqueron\Notifications\Analyzers\DockerHub\BaseEvent;
use Nasqueron\Notifications\Notification;
use InvalidArgumentException;
/**
* A Docker Hub notification.
*
* As we always sort them to the 'docker' group, and the registry only fires
* one kind of event, this is pretty straightforward without any need for
* configuration files or analyser class.
*
* HOW TO IMPLEMENT PAYLOADS SORT PER REPOSITORY?
*
* If you want to extend this to sort Docker images through some rules, we
* suggest you add a feature request to Docker to include source repository
* for the image, then call the GitHubPayloadAnalyzer with this repo instead
* of implementing a new one. This will allows to avoid to maintain two sets
* of configuration, one for the GitHub repos, one for the Docker repos.
*
* Even without that, you can probably be safe with a class or a method to map
* GitHub and Docker names, either because they are the same, either because
* there is a prefix: e.g. nasqueron/arcanist and nasqueron/docker-arcanist.
*/
class DockerHubNotification extends Notification {
- public function __construct ($project, $event, $payload) {
+ public function __construct (string $project, string $event, \stdClass $payload) {
// Straightforward properties
$this->service = "DockerHub";
$this->project = $project;
$this->type = $event;
$this->rawContent = $payload;
$this->group = "docker";
// Properties from the payload
$this->analyzeByEvent();
}
///
/// Analyze by event
///
/**
* Fills properties from event payload.
*/
- public function analyzeByEvent () {
+ public function analyzeByEvent () : void {
$analyzer = $this->getAnalyzer();
$this->rawContent = $analyzer->getPayload();
$this->text = $analyzer->getText();
$this->link = $analyzer->getLink();
}
/**
* Gets analyzer class name for the current event.
*
* @return string
*/
- private function getAnalyzerClassName () {
+ private function getAnalyzerClassName () : string {
return "Nasqueron\Notifications\Analyzers\DockerHub\\"
. ucfirst($this->type)
. "Event";
}
/**
* Gets analyzer for the current event.
*
* @return \Nasqueron\Notifications\Analyzers\DockerHub\BaseEvent
*/
- private function getAnalyzer () {
+ private function getAnalyzer () : BaseEvent {
$class = $this->getAnalyzerClassName();
if (!class_exists($class)) {
throw new InvalidArgumentException(
"Event $this->type doesn't have a matching $class class."
);
}
return new $class($this->rawContent);
}
}
diff --git a/app/Notifications/GitHubNotification.php b/app/Notifications/GitHubNotification.php
index 5b44b5c..3099ceb 100644
--- a/app/Notifications/GitHubNotification.php
+++ b/app/Notifications/GitHubNotification.php
@@ -1,69 +1,69 @@
<?php
namespace Nasqueron\Notifications\Notifications;
use Nasqueron\Notifications\Analyzers\GitHub\GitHubPayloadAnalyzer;
use Nasqueron\Notifications\Notification;
class GitHubNotification extends Notification {
/**
* @var GitHubPayloadAnalyzer
*/
private $analyzer = null;
- public function __construct ($project, $event, $payload) {
+ public function __construct (string $project, string $event, \stdClass $payload) {
// Straightforward properties
$this->service = "GitHub";
$this->project = $project;
$this->type = $event;
$this->rawContent = $payload;
// Analyzes and fills
$this->group = $this->getGroup();
$this->text = $this->getText();
$this->link = $this->getLink();
}
/**
* Gets analyzer
*/
- private function getAnalyzer () {
+ private function getAnalyzer () : GitHubPayloadAnalyzer {
if ($this->analyzer === null) {
$this->analyzer = new GitHubPayloadAnalyzer(
$this->project,
$this->type,
$this->rawContent
);
}
return $this->analyzer;
}
/**
* Gets the target notificatrion group
*
* @return string the target group for the notification
*/
- public function getGroup () {
+ public function getGroup () : string {
return $this->getAnalyzer()->getGroup();
}
/**
* Gets the notification text. Intended to convey a short message (thing Twitter or IRC).
*
* @return string
*/
- public function getText () {
+ public function getText () : string {
return $this->getAnalyzer()->getDescription();
}
/**
* Gets the notification URL. Intended to be a widget or icon link.
*
* @return string
*/
- public function getLink () {
+ public function getLink () : string {
return $this->getAnalyzer()->getLink();
}
}
diff --git a/app/Notifications/JenkinsNotification.php b/app/Notifications/JenkinsNotification.php
index 5f3a640..d65fb64 100644
--- a/app/Notifications/JenkinsNotification.php
+++ b/app/Notifications/JenkinsNotification.php
@@ -1,112 +1,112 @@
<?php
namespace Nasqueron\Notifications\Notifications;
use Nasqueron\Notifications\Analyzers\Jenkins\JenkinsPayloadAnalyzer;
use Nasqueron\Notifications\Notification;
/**
* A Jenkins notification.
*
* This handles the JSON payloads sent by the following plugin:
* https://wiki.jenkins-ci.org/display/JENKINS/Notification+Plugin
*/
class JenkinsNotification extends Notification {
/**
* @var \Nasqueron\Notifications\Analyzers\Jenkins\JenkinsPayloadAnalyzer
*/
private $analyzer = null;
/**
* Initializes a new instance of the JenkinsNotification class.
*
* @param string $project The project this message is for
* @param mixed $payload The message fired by Jenkins notification plugin
*/
public function __construct ($project, $payload) {
// Straightforward properties
$this->service = "Jenkins";
$this->project = $project;
$this->rawContent = $payload;
// Properties from the payload
$this->group = $this->getGroup();
$this->text = $this->getText();
$this->link = $payload->build->full_url;
$this->type = $this->getType();
}
/**
* Gets the notification type.
*
* @return string
*/
- public function getType () {
+ public function getType () : string {
$build = $this->rawContent->build;
$type = strtolower($build->phase);
if (property_exists($build, 'status')) {
$type .= '.';
$type .= $build->status;
}
return strtolower($type);
}
/**
* Gets the notification text. Intended to convey a short message (thing Twitter or IRC).
*
* @return string
*/
- public function getText () {
+ public function getText () : string {
$name = $this->rawContent->name;
$build = $this->rawContent->build;
$phase = strtolower($build->phase);
$text = "Jenkins job $name has been $phase";
if (property_exists($build, 'status')) {
$status = strtolower($build->status);
$text .= ": $status";
}
return $text;
}
/**
* Gets analyzer
*
* @return \Nasqueron\Notifications\Analyzers\Jenkins\JenkinsPayloadAnalyzer
*/
- private function getAnalyzer () {
+ private function getAnalyzer () : JenkinsPayloadAnalyzer {
if ($this->analyzer === null) {
$this->analyzer = new JenkinsPayloadAnalyzer(
$this->project,
$this->rawContent
);
}
return $this->analyzer;
}
/**
* Gets the notification group.
*
* @return string
*/
- public function getGroup () {
+ public function getGroup () : string {
return $this->getAnalyzer()->getGroup();
}
/**
* Indicates if we should handle this payload to trigger a notification.
*
* @return bool if false, this payload is to be ignored for notifications
*/
- public function shouldNotify () {
+ public function shouldNotify () : bool {
return $this->getAnalyzer()->shouldNotify();
}
}
diff --git a/app/Notifications/PhabricatorNotification.php b/app/Notifications/PhabricatorNotification.php
index d56e407..e3243ea 100644
--- a/app/Notifications/PhabricatorNotification.php
+++ b/app/Notifications/PhabricatorNotification.php
@@ -1,68 +1,68 @@
<?php
namespace Nasqueron\Notifications\Notifications;
use Nasqueron\Notifications\Analyzers\Phabricator\PhabricatorPayloadAnalyzer;
use Nasqueron\Notifications\Notification;
use Nasqueron\Notifications\Phabricator\PhabricatorStory;
class PhabricatorNotification extends Notification {
/**
* @var PhabricatorPayloadAnalyzer
*/
private $analyzer = null;
/**
* Initializes a new PhabricatorNotification instance
*
* @param string $project The project for this notification
* @param PhabricatorStory $payload The story to convert into a notification
*/
- public function __construct ($project, PhabricatorStory $payload) {
+ public function __construct (string $project, PhabricatorStory $payload) {
// Straightforward properties
$this->service = "Phabricator";
$this->project = $project;
$this->rawContent = $payload;
$this->text = $payload->text;
// Analyzes and fills
$this->type = $payload->getObjectType();
$this->group = $this->getGroup();
$this->link = $this->getLink();
}
/**
* Gets analyzer
*
* @return \Nasqueron\Notifications\Analyzers\Phabricator\PhabricatorPayloadAnalyzer
*/
- private function getAnalyzer () {
+ private function getAnalyzer () : PhabricatorPayloadAnalyzer {
if ($this->analyzer === null) {
$this->analyzer = new PhabricatorPayloadAnalyzer(
$this->project,
$this->rawContent
);
}
return $this->analyzer;
}
/**
* Gets the target notificatrion group
*
* @return string the target group for the notification
*/
- public function getGroup () {
+ public function getGroup () : string {
return $this->getAnalyzer()->getGroup();
}
/**
* Gets the notification URL. Intended to be a widget or icon link.
*
* @return string
*/
- public function getLink () {
+ public function getLink () : string {
return "";
}
}
diff --git a/app/Services/Service.php b/app/Services/Service.php
index 52675b2..c43e569 100644
--- a/app/Services/Service.php
+++ b/app/Services/Service.php
@@ -1,38 +1,38 @@
<?php
namespace Nasqueron\Notifications\Services;
class Service {
/**
* @var string
*/
public $gate;
/**
* @var string
*/
public $door;
/**
* @var string
*/
public $instance;
/**
* @var string
*/
public $secret;
/**
* Gets instance name
*
* @return string The instance name or "ø" if omitted
*/
- public function getInstanceName () {
+ public function getInstanceName () : string {
if (!isset($this->instance)) {
return "ø";
}
return $this->instance;
}
}
diff --git a/app/Services/Services.php b/app/Services/Services.php
index 9e2cd03..f93ad45 100644
--- a/app/Services/Services.php
+++ b/app/Services/Services.php
@@ -1,104 +1,104 @@
<?php
namespace Nasqueron\Notifications\Services;
use Storage;
class Services {
///
/// Properties
///
/**
* @var Service[]
*/
public $services = [];
///
/// Constructors
///
/**
* Initializes a new instance of the Services class deserializing a JSON file.
*
- * @param $file the JSON file to deserialize
- * @return Services the deserialized instance
+ * @param string $file The JSON file to deserialize
+ * @return Services The deserialized instance
*/
- public static function loadFromJson ($file) {
+ public static function loadFromJson (string $file) : Services {
$data = json_decode(Storage::get($file));
$mapper = new \JsonMapper();
return $mapper->map($data, new self());
}
///
/// Methods to get a list of services
///
/**
- * Gets the services found in credentials.json
+ * Gets the services found in credentials.json configuration file.
*
- * @return array
+ * @return Service[]
*/
public function get () {
return $this->services;
}
/**
- * Gets all the services for a specific gate
+ * Gets all the services for a specific gate.
*
* @param string $gate The gate (e.g. GitHub)
- * @return array
+ * @return Service[]
*/
- public function getForGate ($gate) {
+ public function getForGate (string $gate) : array {
$services = [];
foreach ($this->services as $service) {
if ($service->gate === $gate) {
$services[] = $service;
}
}
return $services;
}
///
/// Methods to find a service matching criteria
///
/**
* Gets the service for a specific gate and door
*
* @param string $gate The gate (e.g. GitHub)
* @param string $door The door (e.g. Nasqueron)
- * @return \stdClass|null The service information is found; otherwise, null.
+ * @return Service|null The service information is found; otherwise, null.
*/
- public function findServiceByDoor ($gate, $door) {
+ public function findServiceByDoor (string $gate, string $door) : ?Service {
foreach ($this->services as $service) {
if ($service->gate === $gate && $service->door === $door) {
return $service;
}
}
return null;
}
/**
* Finds a service for a specific gate, property and value
*
* @param string $gate The gate (e.g. Phabricator)
* @param string $property The property to check (e.g. instance)
* @param mixed $value The property value to find (e.g. 'http://devcentral.nasqueron.org')
- * @return \stdClass|null The service information is found; otherwise, null.
+ * @return Service|null The service information is found; otherwise, null.
*/
- public function findServiceByProperty ($gate, $property, $value) {
+ public function findServiceByProperty (string $gate, string $property, $value) : ?Service {
foreach ($this->services as $service) {
if ($service->gate === $gate && $service->$property === $value) {
return $service;
}
}
return null;
}
}
diff --git a/tests/Analyzers/GitHub/GitHubPayloadAnalyzerTest.php b/tests/Analyzers/GitHub/GitHubPayloadAnalyzerTest.php
index 92db29c..c072e2b 100644
--- a/tests/Analyzers/GitHub/GitHubPayloadAnalyzerTest.php
+++ b/tests/Analyzers/GitHub/GitHubPayloadAnalyzerTest.php
@@ -1,140 +1,140 @@
<?php
namespace Nasqueron\Notifications\Tests\Analyzers;
use Nasqueron\Notifications\Analyzers\GitHub\GitHubPayloadAnalyzer;
use Nasqueron\Notifications\Tests\TestCase;
class GitHubPayloadAnalyzerTest extends TestCase {
/**
* @var \Nasqueron\Notifications\Analyzers\GitHub\GitHubPayloadAnalyzer
*/
private $unknownEventAnalyzer;
/**
* @var \Nasqueron\Notifications\Analyzers\GitHub\GitHubPayloadAnalyzer
*/
private $pingAnalyzer;
/**
* @var \Nasqueron\Notifications\Analyzers\GitHub\GitHubPayloadAnalyzer
*/
private $pushAnalyzer;
/**
* @var \Nasqueron\Notifications\Analyzers\GitHub\GitHubPayloadAnalyzer
*/
private $pushToMappedRepositoryAnalyzer;
/**
* Prepares the tests
*/
public function setUp () {
parent::setUp();
$this->unknownEventAnalyzer = new GitHubPayloadAnalyzer(
"Acme", // Expected without known config file
"quux",
new \stdClass
);
$this->pingAnalyzer = new GitHubPayloadAnalyzer(
"Nasqueron", // Expected with known config file
"ping",
new \stdClass
);
$filename = __DIR__ . "/../../data/payloads/GitHubEvents/push.json";
$payloadRawContent = file_get_contents($filename);
$payload = json_decode($payloadRawContent);
$this->pushAnalyzer = new GitHubPayloadAnalyzer(
"Nasqueron", // Expected with known config
"push",
$payload
);
$dockerPayload = json_decode($payloadRawContent);
$dockerPayload->repository->name = "docker-someapp";
$this->pushToMappedRepositoryAnalyzer = new GitHubPayloadAnalyzer(
"Nasqueron", // Expected with known config
"push",
$dockerPayload
);
}
///
/// Test constructor
///
/**
- * @expectedException InvalidArgumentException
+ * @expectedException TypeError
*/
public function testConstructorThrowsAnExceptionWhenPayloadIsInvalid () {
new GitHubPayloadAnalyzer(
"Acme",
"push",
"This is not an object deserialized from JSON but a string."
);
}
///
/// Test getConfigurationFileName
///
public function testGetConfigurationFileNameWhenConfigExists () {
$this->assertSame(
"GitHubPayloadAnalyzer/Nasqueron.json",
$this->pingAnalyzer->getConfigurationFileName()
);
}
public function testGetConfigurationFileNameWhenConfigDoesNotExist () {
$this->assertSame(
"GitHubPayloadAnalyzer/default.json",
$this->unknownEventAnalyzer->getConfigurationFileName()
);
}
///
/// Test getItemName
///
public function testGetItemNameWhenEventIsAdministrative () {
$this->assertEmpty($this->pingAnalyzer->getItemName());
}
public function testGetItemNameWhenEventIsRepositoryRelative () {
$this->assertSame("public-repo", $this->pushAnalyzer->getItemName());
}
///
/// Test getGroup
///
public function testGetGroupWhenEventIsAdministrative () {
$this->assertSame("orgz", $this->pingAnalyzer->getGroup());
}
public function testGetGroupOnPushToMappedRepository () {
$this->assertSame("docker", $this->pushToMappedRepositoryAnalyzer->getGroup());
}
public function testGetGroupOnPushToNotMappedRepository () {
$this->assertSame("nasqueron", $this->pushAnalyzer->getGroup());
}
///
/// Test if our fallback is correct when the GitHub event type is unknown
///
public function testDescriptionContainsTypeWhenEventTypeIsUnknown () {
$this->assertContains(
"quux",
$this->unknownEventAnalyzer->getDescription()
);
}
}

File Metadata

Mime Type
text/x-diff
Expires
Sun, Nov 24, 19:56 (6 h, 56 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2258534
Default Alt Text
(135 KB)

Event Timeline