Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
TimoBakx committed Feb 23, 2020
0 parents commit b3313af
Show file tree
Hide file tree
Showing 13 changed files with 958 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/vendor/
composer.lock
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 LinkuNijmegen

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
23 changes: 23 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "linku/feedback",
"description": "Abstract layer between services and output in PHP",
"type": "library",
"license": "MIT",
"require": {
"php": "^7.2.0",
"psr/log": "^1.1"
},
"autoload": {
"psr-4": {
"Linku\\Feedback\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Linku\\Feedback\\Tests\\": "tests/"
}
},
"require-dev": {
"phpunit/phpunit": "^9.0"
}
}
24 changes: 24 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>

<!-- https://phpunit.de/manual/current/en/appendixes.configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.5/phpunit.xsd"
backupGlobals="false"
colors="true"
>
<php>
<ini name="error_reporting" value="-1" />
</php>

<testsuites>
<testsuite name="Linku Feedback Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>src</directory>
</whitelist>
</filter>
</phpunit>
75 changes: 75 additions & 0 deletions src/ChainedFeedback.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
declare(strict_types=1);

namespace Linku\Feedback;

use Throwable;

final class ChainedFeedback implements Feedback
{
/**
* @var Feedback[]
*/
private $chain;

public function __construct(Feedback ...$chain)
{
$this->chain = $chain;
}

public function exception(Throwable $exception): void
{
foreach ($this->chain as $feedback) {
$feedback->exception($exception);
}
}

public function error(string $message): void
{
foreach ($this->chain as $feedback) {
$feedback->error($message);
}
}

public function warning(string $message): void
{
foreach ($this->chain as $feedback) {
$feedback->warning($message);
}
}

public function info(string $message): void
{
foreach ($this->chain as $feedback) {
$feedback->info($message);
}
}

public function success(string $message): void
{
foreach ($this->chain as $feedback) {
$feedback->success($message);
}
}

public function startProcess(int $total = 0): void
{
foreach ($this->chain as $feedback) {
$feedback->startProcess($total);
}
}

public function finishProcess(): void
{
foreach ($this->chain as $feedback) {
$feedback->finishProcess();
}
}

public function advanceProcess(int $steps = 1): void
{
foreach ($this->chain as $feedback) {
$feedback->advanceProcess($steps);
}
}
}
111 changes: 111 additions & 0 deletions src/ClosureFeedback.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php
declare(strict_types=1);

namespace Linku\Feedback;

use Closure;
use Throwable;

final class ClosureFeedback implements Feedback
{
/**
* @var Closure
*/
private $exceptionClosure;

/**
* @var Closure
*/
private $errorClosure;

/**
* @var Closure
*/
private $warningClosure;

/**
* @var Closure
*/
private $infoClosure;

/**
* @var Closure
*/
private $successClosure;

/**
* @var Closure|null
*/
private $startProcessClosure;

/**
* @var Closure|null
*/
private $finishProcessClosure;

/**
* @var Closure|null
*/
private $advanceProcessClosure;

public function __construct(
?Closure $error = null,
?Closure $warning = null,
?Closure $info = null,
?Closure $success = null,
?Closure $exception = null,
?Closure $startProcess = null,
?Closure $finishProcess = null,
?Closure $advanceProcess = null
)
{
$this->errorClosure = $error ?? static function (string $message) {};
$this->warningClosure = $warning ?? static function (string $message) {};
$this->infoClosure = $info ?? static function (string $message) {};
$this->successClosure = $success ?? static function (string $message) {};
$this->exceptionClosure = $exception ?? function (Throwable $exception) { $this->error($exception->getMessage()); };
$this->startProcessClosure = $startProcess ?? static function (int $total) {};
$this->finishProcessClosure = $finishProcess ?? static function () {};
$this->advanceProcessClosure = $advanceProcess ?? static function (int $steps) {};
}

public function exception(Throwable $exception): void
{
($this->exceptionClosure)($exception);
}

public function error(string $message): void
{
($this->errorClosure)($message);
}

public function warning(string $message): void
{
($this->warningClosure)($message);
}

public function info(string $message): void
{
($this->infoClosure)($message);
}

public function success(string $message): void
{
($this->successClosure)($message);
}

public function startProcess(int $total = 0): void
{
($this->startProcessClosure)($total);
}

public function finishProcess(): void
{
($this->finishProcessClosure)();
}

public function advanceProcess(int $steps = 1): void
{
($this->advanceProcessClosure)($steps);
}
}
25 changes: 25 additions & 0 deletions src/Feedback.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
declare(strict_types=1);

namespace Linku\Feedback;

use Throwable;

interface Feedback
{
public function exception(Throwable $exception): void;

public function error(string $message): void;

public function warning(string $message): void;

public function info(string $message): void;

public function success(string $message): void;

public function startProcess(int $total = 0): void;

public function finishProcess(): void;

public function advanceProcess(int $steps = 1): void;
}
60 changes: 60 additions & 0 deletions src/LoggerFeedback.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
declare(strict_types=1);

namespace Linku\Feedback;

use Psr\Log\LoggerInterface;
use Throwable;

final class LoggerFeedback implements Feedback
{
/**
* @var LoggerInterface
*/
private $logger;

public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}

public function exception(Throwable $exception): void
{
$this->logger->error($exception->getMessage());
}

public function error(string $message): void
{
$this->logger->error($message);
}

public function warning(string $message): void
{
$this->logger->warning($message);
}

public function info(string $message): void
{
$this->logger->info($message);
}

public function success(string $message): void
{
$this->logger->info($message);
}

public function startProcess(int $total = 0): void
{
$this->logger->info('Process '.($total?'of '.$total.' ':'').'started');
}

public function finishProcess(): void
{
$this->logger->info('Process stopped');
}

public function advanceProcess(int $steps = 1): void
{
$this->logger->debug('Process advanced with '.$steps);
}
}
41 changes: 41 additions & 0 deletions src/NoFeedback.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
declare(strict_types=1);

namespace Linku\Feedback;

use Throwable;

final class NoFeedback implements Feedback
{
public function error(string $message): void
{
}

public function warning(string $message): void
{
}

public function info(string $message): void
{
}

public function success(string $message): void
{
}

public function exception(Throwable $exception): void
{
}

public function startProcess(int $total = 0): void
{
}

public function finishProcess(): void
{
}

public function advanceProcess(int $steps = 1): void
{
}
}
Loading

0 comments on commit b3313af

Please # to comment.