Verified Commit 06f4bd59 authored by Elias Häußler's avatar Elias Häußler 🐛
Browse files

[FEATURE] Provide registry to register and unregister services

parent ad4371aa
......@@ -28,6 +28,12 @@ use Composer\EventDispatcher\EventSubscriberInterface;
use Composer\IO\IOInterface;
use Composer\Plugin\PluginInterface;
use EliasHaeussler\ComposerUpdateCheck\Event\PostUpdateCheckEvent;
use EliasHaeussler\ComposerUpdateReporter\Exception\InvalidServiceException;
use EliasHaeussler\ComposerUpdateReporter\Service\Email;
use EliasHaeussler\ComposerUpdateReporter\Service\GitLab;
use EliasHaeussler\ComposerUpdateReporter\Service\Mattermost;
use EliasHaeussler\ComposerUpdateReporter\Service\Slack;
use EliasHaeussler\ComposerUpdateReporter\Service\Teams;
/**
* Plugin.
......@@ -43,8 +49,14 @@ class Plugin implements PluginInterface, EventSubscriberInterface
*/
private $reporter;
/**
* {@inheritDoc}
*
* @throws InvalidServiceException
*/
public function activate(Composer $composer, IOInterface $io): void
{
$this->registerDefaultServices();
$this->reporter = new Reporter($composer);
}
......@@ -76,4 +88,22 @@ class Plugin implements PluginInterface, EventSubscriberInterface
$this->reporter->setOptions($event->getOptions());
$this->reporter->report($event->getUpdateCheckResult());
}
/**
* @throws InvalidServiceException
*/
private function registerDefaultServices(): void
{
$defaultServices = [
Email::class,
GitLab::class,
Mattermost::class,
Slack::class,
Teams::class,
];
foreach ($defaultServices as $className) {
Registry::registerService($className);
}
}
}
<?php
declare(strict_types=1);
namespace EliasHaeussler\ComposerUpdateReporter;
/*
* This file is part of the Composer package "eliashaeussler/composer-update-reporter".
*
* Copyright (C) 2021 Elias Häußler <elias@haeussler.dev>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use EliasHaeussler\ComposerUpdateReporter\Exception\InvalidServiceException;
use EliasHaeussler\ComposerUpdateReporter\Service\ServiceInterface;
/**
* Registry.
*
* @author Elias Häußler <elias@haeussler.dev>
* @license GPL-3.0-or-later
*/
final class Registry
{
/**
* @var string[]
*/
private static $services = [];
/**
* @throws InvalidServiceException
*/
public static function registerService(string $className): void
{
if (!in_array(ServiceInterface::class, class_implements($className), true)) {
throw InvalidServiceException::create($className);
}
if (!in_array($className, self::$services, true)) {
self::$services[] = $className;
}
}
public static function unregisterService(string $className): void
{
$key = array_search($className, self::$services, true);
if (false !== $key) {
unset(self::$services[$key]);
}
}
/**
* @return string[]
*/
public static function getServices(): array
{
return self::$services;
}
}
......@@ -30,13 +30,7 @@ use EliasHaeussler\ComposerUpdateCheck\IO\Style;
use EliasHaeussler\ComposerUpdateCheck\IO\Verbosity;
use EliasHaeussler\ComposerUpdateCheck\Options;
use EliasHaeussler\ComposerUpdateCheck\Package\UpdateCheckResult;
use EliasHaeussler\ComposerUpdateReporter\Exception\InvalidServiceException;
use EliasHaeussler\ComposerUpdateReporter\Service\Email;
use EliasHaeussler\ComposerUpdateReporter\Service\GitLab;
use EliasHaeussler\ComposerUpdateReporter\Service\Mattermost;
use EliasHaeussler\ComposerUpdateReporter\Service\ServiceInterface;
use EliasHaeussler\ComposerUpdateReporter\Service\Slack;
use EliasHaeussler\ComposerUpdateReporter\Service\Teams;
/**
* Reporter.
......@@ -61,11 +55,6 @@ class Reporter
*/
private $options;
/**
* @var string[]
*/
private $registeredServices;
/**
* @var array<string, mixed>
*/
......@@ -76,7 +65,6 @@ class Reporter
$this->composer = $composer;
$this->behavior = $this->getDefaultBehavior();
$this->options = new Options();
$this->registeredServices = $this->getDefaultServices();
$this->configuration = $this->resolveConfiguration();
}
......@@ -102,7 +90,7 @@ class Reporter
}
/** @var ServiceInterface $registeredService */
foreach ($this->registeredServices as $registeredService) {
foreach (Registry::getServices() as $registeredService) {
if (!$registeredService::isEnabled($this->configuration)) {
continue;
}
......@@ -131,45 +119,6 @@ class Reporter
$this->options = $options;
}
/**
* @throws InvalidServiceException
*/
public function registerService(string $service): self
{
if (!in_array(ServiceInterface::class, class_implements($service), true)) {
throw InvalidServiceException::create($service);
}
if (!in_array($service, $this->registeredServices, true)) {
$this->registeredServices[] = $service;
}
return $this;
}
public function unregisterService(string $service): self
{
if (($key = array_search($service, $this->registeredServices, true)) !== false) {
unset($this->registeredServices[$key]);
}
return $this;
}
/**
* @return string[]
*/
private function getDefaultServices(): array
{
return [
Email::class,
GitLab::class,
Mattermost::class,
Slack::class,
Teams::class,
];
}
private function getDefaultBehavior(): OutputBehavior
{
return new OutputBehavior(
......
<?php
declare(strict_types=1);
namespace EliasHaeussler\ComposerUpdateReporter\Tests\Unit;
/*
* This file is part of the Composer package "eliashaeussler/composer-update-reporter".
*
* Copyright (C) 2021 Elias Häußler <elias@haeussler.dev>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use EliasHaeussler\ComposerUpdateReporter\Exception\InvalidServiceException;
use EliasHaeussler\ComposerUpdateReporter\Registry;
use EliasHaeussler\ComposerUpdateReporter\Tests\Unit\Fixtures\Service\DummyService;
/**
* RegistryTest.
*
* @author Elias Häußler <elias@haeussler.dev>
* @license GPL-3.0-or-later
*/
class RegistryTest extends AbstractTestCase
{
/**
* @test
*/
public function registerServiceThrowsExceptionIfInvalidServiceIsGiven(): void
{
$this->expectException(InvalidServiceException::class);
$this->expectExceptionCode(1600814017);
Registry::registerService(\Exception::class);
}
/**
* @test
*
* @throws InvalidServiceException
*/
public function unregisterServiceRemovesServiceFromListOfRegisteredServices(): void
{
Registry::registerService(DummyService::class);
Registry::unregisterService(DummyService::class);
static::assertNotContains(DummyService::class, Registry::getServices());
}
protected function tearDown(): void
{
parent::tearDown();
// Reset registry state
foreach (Registry::getServices() as $service) {
Registry::unregisterService($service);
}
}
}
......@@ -30,15 +30,10 @@ use EliasHaeussler\ComposerUpdateCheck\IO\OutputBehavior;
use EliasHaeussler\ComposerUpdateCheck\IO\Style;
use EliasHaeussler\ComposerUpdateCheck\IO\Verbosity;
use EliasHaeussler\ComposerUpdateCheck\Options;
use EliasHaeussler\ComposerUpdateCheck\Package\OutdatedPackage;
use EliasHaeussler\ComposerUpdateCheck\Package\UpdateCheckResult;
use EliasHaeussler\ComposerUpdateReporter\Exception\InvalidServiceException;
use EliasHaeussler\ComposerUpdateReporter\Registry;
use EliasHaeussler\ComposerUpdateReporter\Reporter;
use EliasHaeussler\ComposerUpdateReporter\Service\Email;
use EliasHaeussler\ComposerUpdateReporter\Service\GitLab;
use EliasHaeussler\ComposerUpdateReporter\Service\Mattermost;
use EliasHaeussler\ComposerUpdateReporter\Service\Slack;
use EliasHaeussler\ComposerUpdateReporter\Service\Teams;
use EliasHaeussler\ComposerUpdateReporter\Tests\Unit\Fixtures\Service\DummyService;
/**
......@@ -69,15 +64,10 @@ class ReporterTest extends AbstractTestCase
$this->goToTestDirectory();
DummyService::$enabled = true;
Registry::registerService(DummyService::class);
$this->composer = Factory::create(new NullIO());
$this->subject = (new Reporter($this->composer))
->unregisterService(Email::class)
->unregisterService(GitLab::class)
->unregisterService(Mattermost::class)
->unregisterService(Slack::class)
->unregisterService(Teams::class)
->registerService(DummyService::class);
$this->subject = new Reporter($this->composer);
}
/**
......@@ -140,31 +130,6 @@ class ReporterTest extends AbstractTestCase
static::assertSame('foo/baz', DummyService::$customProjectName);
}
/**
* @test
*/
public function registerServiceThrowsExceptionIfInvalidServiceIsGiven(): void
{
$this->expectException(InvalidServiceException::class);
$this->expectExceptionCode(1600814017);
$this->subject->registerService(\Exception::class);
}
/**
* @test
*/
public function unregisterServiceRemovesServiceFromListOfRegisteredServices(): void
{
$this->subject->unregisterService(DummyService::class);
$this->subject->report(new UpdateCheckResult([
new OutdatedPackage('foo/baz', '1.0.0', '1.0.5'),
]));
static::assertFalse(DummyService::$reportWasExecuted);
}
protected function tearDown(): void
{
$this->goBackToInitialDirectory();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment