Skip to content
This repository has been archived by the owner on Feb 29, 2020. It is now read-only.

Commit

Permalink
Merge pull request #5 from articstudio/develop
Browse files Browse the repository at this point in the history
v1.0.2
  • Loading branch information
marcmascort authored Aug 22, 2017
2 parents 0d58d7b + ed9b333 commit b959d87
Show file tree
Hide file tree
Showing 42 changed files with 397 additions and 92 deletions.
61 changes: 27 additions & 34 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,34 @@
use Articstudio\Bitbucket\ContainerAwareTrait;
use Articstudio\Bitbucket\Container;
use Psr\Container\ContainerInterface as ContainerContract;
use Articstudio\Bitbucket\Exception\App\InvalidArgumentException;
use Articstudio\Bitbucket\Exception\Middleware\NotFoundException as MiddlewareNotFound;
use Articstudio\Bitbucket\Provider\DefaultProviders;
use Articstudio\Bitbucket\Http\Response;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Articstudio\Bitbucket\Exception\App\InvalidArgumentException as AppInvalidArgumentException;
use Articstudio\Bitbucket\Exception\Middleware\NotFoundException as MiddlewareNotFoundException;
use Articstudio\Bitbucket\Exception\Middleware\WrongPayloadException;
use Articstudio\Bitbucket\Exception\Middleware\InvalidHeaderException;
use Articstudio\Bitbucket\Exception\Middleware\InvalidPayloadException;
use Articstudio\Bitbucket\Exception\Middleware\InvalidRepositoryException;
use Articstudio\Bitbucket\Exception\Deploy\NotFoundException as DeployNotFoundException;
use Articstudio\Bitbucket\Exception\Deploy\RuntimeException as DeployRuntimeException;
use Exception;
use Throwable;

class App {

use ContainerAwareTrait;

const VERSION = '1.0.0';

private $response;

public function __construct($container = []) {
if (is_array($container)) {
$container = new Container($container);
}
if (!$container instanceof ContainerContract) {
throw new InvalidArgumentException(sprintf('Expected a "%s"', ContainerContract::class));
throw new AppInvalidArgumentException(sprintf('Expected a "%s"', ContainerContract::class));
}
$container['version'] = self::VERSION;
$this->setContainer($container);
}

Expand Down Expand Up @@ -60,9 +63,9 @@ private function addMiddleware($middleware) {
try {
$middleware = new $middleware($this->container);
} catch (Exception $exception) {
throw new MiddlewareNotFound(sprintf('Middleware error while retrieving "%s"', $middleware), null, $exception);
throw new MiddlewareNotFoundException(sprintf('Middleware error while retrieving "%s"', $middleware), null, $exception);
} catch (Throwable $exception) {
throw new MiddlewareNotFound(sprintf('Middleware error while retrieving "%s"', $middleware), null, $exception);
throw new MiddlewareNotFoundException(sprintf('Middleware error while retrieving "%s"', $middleware), null, $exception);
}
}
$this->container->middle->add($middleware);
Expand All @@ -87,43 +90,33 @@ private function process() {
}

protected function handleFound(ServerRequestInterface $request, ResponseInterface $response) {

//$response->getBody()->write('<h2>Found</h2>');
//$response->getBody()->write('<h4>Headers</h4><pre>' . print_r($this->container->headersInfo->all(), true) . '</pre>');
//$response->getBody()->write('<h4>Payload</h4><pre>' . print_r($this->container->payload, true) . '</pre>');
//$this->container->logger->info('Found', $this->container->payload);
return $response;
return $this->container->handleFound->handle($request, $response);
}

protected function handleException(Exception $exception, ServerRequestInterface $request, ResponseInterface $response) {
$this->container->logger->warning($exception->getMessage(), $exception->getTrace());

$response->getBody()->write('<h2>Exception</h2>');
$response->getBody()->write('<h4>' . $exception->getMessage() . '</h4><pre>' . $exception->getTraceAsString() . '</pre>');
$response->getBody()->write('<h4>Headers</h4><pre>' . print_r($this->container->headersInfo->all(), true) . '</pre>');
$response->getBody()->write('<h4>Payload</h4><pre>' . print_r($this->container->payload, true) . '</pre>');

return $response->withStatus(500, $exception->getMessage());
if ($exception instanceof WrongPayloadException) {
return $this->container->handleInvalid->handle($exception, $request, $response);
} else if ($exception instanceof InvalidHeaderException) {
return $this->container->handleInvalid->handle($exception, $request, $response);
} else if ($exception instanceof InvalidPayloadException) {
return $this->container->handleInvalid->handle($exception, $request, $response);
} else if ($exception instanceof InvalidRepositoryException) {
return $this->container->handleInvalid->handle($exception, $request, $response);
} else if ($exception instanceof DeployNotFoundException) {
return $this->container->handleNotFound->handle($exception, $request, $response);
} else if ($exception instanceof DeployRuntimeException) {
return $this->container->handleNotFound->handle($exception, $request, $response);
}
return $this->container->handleException->handle($exception, $request, $response);
}

protected function handleError(Throwable $exception, ServerRequestInterface $request, ResponseInterface $response) {
$this->container->logger->error($exception->getMessage(), $exception->getTrace());

$response->getBody()->write('<h2>Error</h2>');
$response->getBody()->write('<h4>' . $exception->getMessage() . '</h4><pre>' . $exception->getTraceAsString() . '</pre>');
$response->getBody()->write('<h4>Headers</h4><pre>' . print_r($this->container->headersInfo->all(), true) . '</pre>');
$response->getBody()->write('<h4>Payload</h4><pre>' . print_r($this->container->payload, true) . '</pre>');

return $response->withStatus(500, $exception->getMessage());
return $this->container->handleError->handle($exception, $request, $response);
}

private function respond() {
$response = new Response($this->response);
$response->respond();

/* echo '<pre>';
print_r($this->container);
echo '</pre>'; */
}

}
4 changes: 2 additions & 2 deletions src/Change.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Articstudio\Bitbucket;

use Articstudio\Bitbucket\Collection;
use DomainException;
use Articstudio\Bitbucket\Exception\Middleware\InvalidPayloadException;

class Change extends Collection {

Expand All @@ -18,7 +18,7 @@ public function parse() {

public function validate() {
if ($this->get('new')->isEmpty() || !$this->get('new')->has('type') || !$this->get('new')->has('name')) {
throw new DomainException('Incomplete payload push change');
throw new InvalidPayloadException('Incomplete payload push change');
}
}

Expand Down
9 changes: 6 additions & 3 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
use Articstudio\Bitbucket\Exception\Container\Exception as ContainerException;
use Articstudio\Bitbucket\Collection;
use Articstudio\Bitbucket\Provider\AppProvider;
use Articstudio\Bitbucket\Exception\TrowByTrait;
use Exception;
use InvalidArgumentException;

class Container extends PimpleContainer implements ContainerContract {

use Exception\ParentCatchTrait;
use TrowByTrait;

public function __construct(array $values = array()) {
parent::__construct($values);
Expand All @@ -31,8 +34,8 @@ public function get($id) {
}
try {
return $this->offsetGet($id);
} catch (\InvalidArgumentException $exception) {
if ($this->isParentException($exception, 'offsetGet')) {
} catch (Exception $exception) {
if ($this->thrownByParent($exception, InvalidArgumentException::class, 'offsetGet')) {
throw new ContainerException(sprintf('Container error while retrieving "%s"', $id), null, $exception);
} else {
throw $exception;
Expand Down
6 changes: 4 additions & 2 deletions src/ContainerAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@

namespace Articstudio\Bitbucket;

use Psr\Container\ContainerInterface as ContainerContract;

trait ContainerAwareTrait {

protected $container;

public function getContainer() {
public function getContainer(): ContainerContract {
return $this->container;
}

public function setContainer(Container $container) {
public function setContainer(ContainerContract $container) {
$this->container = $container;
}

Expand Down
14 changes: 14 additions & 0 deletions src/Contract/FoundHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Articstudio\Bitbucket\Contract;

use Psr\Container\ContainerInterface as ContainerContract;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;

interface FoundHandler {

public function __construct(ContainerContract $container);

public function handle(ServerRequestInterface $request, ResponseInterface $response);
}
5 changes: 3 additions & 2 deletions src/Contract/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

namespace Articstudio\Bitbucket\Contract;

use Pimple\ServiceProviderInterface;
use Articstudio\Bitbucket\Container;

interface ServiceProvider extends ServiceProviderInterface {
interface ServiceProvider {

public function register(Container $container);
}
15 changes: 15 additions & 0 deletions src/Contract/ThrowableHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Articstudio\Bitbucket\Contract;

use Psr\Container\ContainerInterface as ContainerContract;
use Throwable;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;

interface ThrowableHandler {

public function __construct(ContainerContract $container);

public function handle(Throwable $throwable, ServerRequestInterface $request, ResponseInterface $response);
}
4 changes: 2 additions & 2 deletions src/Deploy.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
namespace Articstudio\Bitbucket;

use Articstudio\Bitbucket\ContainerAwareTrait;
use Pimple\Container;
use Articstudio\Bitbucket\Container;
use Articstudio\Bitbucket\Change;
use Articstudio\Bitbucket\Exception\Deploy\NotFoundException;
use Articstudio\Bitbucket\Exception\Deploy\RuntimeException;
use Articstudio\Bitbucket\System\Filesystem\Directory;
use RuntimeException;
use Monolog\Logger;
use Monolog\Handler\NativeMailerHandler;

Expand Down
9 changes: 9 additions & 0 deletions src/Exception/Deploy/RuntimeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Articstudio\Bitbucket\Exception\Deploy;

use RuntimeException;

class RuntimeException extends RuntimeException {

}
9 changes: 9 additions & 0 deletions src/Exception/Middleware/InvalidHeaderException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Articstudio\Bitbucket\Exception\Middleware;

use InvalidArgumentException;

class InvalidHeaderException extends InvalidArgumentException {

}
9 changes: 9 additions & 0 deletions src/Exception/Middleware/InvalidPayloadException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Articstudio\Bitbucket\Exception\Middleware;

use InvalidArgumentException;

class InvalidPayloadException extends InvalidArgumentException {

}
9 changes: 9 additions & 0 deletions src/Exception/Middleware/InvalidRepositoryException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Articstudio\Bitbucket\Exception\Middleware;

use InvalidArgumentException;

class InvalidRepositoryException extends InvalidArgumentException {

}
9 changes: 9 additions & 0 deletions src/Exception/Middleware/WrongPayloadException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Articstudio\Bitbucket\Exception\Middleware;

use InvalidArgumentException;

class WrongPayloadException extends InvalidArgumentException {

}
21 changes: 0 additions & 21 deletions src/Exception/ParentCatchTrait.php

This file was deleted.

24 changes: 24 additions & 0 deletions src/Exception/TrowByTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Articstudio\Bitbucket\Exception;

use Throwable;

trait TrowByTrait {

protected final function thrownByParent(Throwable $exception, $concrete = null, $method = null) {
return $this->thrownBy($exception, parent::class, $concrete, $method);
}

protected final function thrownBy(Throwable $exception, $by, $concrete = null, $method = null) {
$trace = $exception->getTrace()[0];
if ($concrete && !($exception instanceof $concrete)) {
return false;
}
if ($method && $trace['function'] !== $method) {
return false;
}
return $trace['class'] === $by;
}

}
16 changes: 16 additions & 0 deletions src/Handler/AbstractHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Articstudio\Bitbucket\Handler;

use Articstudio\Bitbucket\ContainerAwareTrait;
use Psr\Container\ContainerInterface as ContainerContract;

class AbstractHandler {

use ContainerAwareTrait;

public function __construct(ContainerContract $container) {
$this->setContainer($container);
}

}
20 changes: 20 additions & 0 deletions src/Handler/ErrorHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Articstudio\Bitbucket\Handler;

use Articstudio\Bitbucket\Handler\AbstractHandler;
use Articstudio\Bitbucket\Contract\ThrowableHandler as ThrowableHandlerContract;
use Throwable;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;

class ErrorHandler extends AbstractHandler implements ThrowableHandlerContract {

public function handle(Throwable $throwable, ServerRequestInterface $request, ResponseInterface $response) {
$this->container->logger->error($throwable->getMessage(), $throwable->getTrace());
$response->getBody()->write('<h2>Error</h2>');
$response->getBody()->write('<h4>' . $throwable->getMessage() . '</h4><pre>' . $throwable->getTraceAsString() . '</pre>');
return $response->withStatus(500, $throwable->getMessage());
}

}
22 changes: 22 additions & 0 deletions src/Handler/ExceptionHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Articstudio\Bitbucket\Handler;

use Articstudio\Bitbucket\Handler\AbstractHandler;
use Articstudio\Bitbucket\Contract\ThrowableHandler as ThrowableHandlerContract;
use Throwable;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;

class ExceptionHandler extends AbstractHandler implements ThrowableHandlerContract {

public function handle(Throwable $throwable, ServerRequestInterface $request, ResponseInterface $response) {
$this->container->logger->warning($throwable->getMessage(), $throwable->getTrace());
$response->getBody()->write('<h2>Exception</h2>');
$response->getBody()->write('<h4>' . $throwable->getMessage() . '</h4><pre>' . $throwable->getTraceAsString() . '</pre>');
$response->getBody()->write('<h4>Headers</h4><pre>' . print_r($this->container->headersInfo->all(), true) . '</pre>');
$response->getBody()->write('<h4>Payload</h4><pre>' . print_r($this->container->payload, true) . '</pre>');
return $response->withStatus(500, $throwable->getMessage());
}

}
Loading

0 comments on commit b959d87

Please sign in to comment.