diff --git a/composer.json b/composer.json index 4de62ce..416d9d3 100644 --- a/composer.json +++ b/composer.json @@ -16,8 +16,7 @@ ], "require": { "knplabs/github-api": "1.5.1", - "symfony/yaml": "v3.0.2", - "symfony/http-foundation": "v3.0.2" + "symfony/yaml": "v3.0.2" }, "require-dev": { "phpunit/phpunit": "5.2.9" diff --git a/composer.lock b/composer.lock index 15ad41a..d2c53c0 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,8 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "793fe5f6ab4f2bdc02e579329f9b8040", - "content-hash": "3f1dafcd2e88e9a721dcab2f116a2daf", + "hash": "e938a6ea04425773ef04d1afb70508a2", + "content-hash": "9e1071f6bce85fedc142de29bcc8ada1", "packages": [ { "name": "guzzle/guzzle", @@ -223,58 +223,6 @@ "homepage": "https://symfony.com", "time": "2016-01-13 10:28:07" }, - { - "name": "symfony/http-foundation", - "version": "v3.0.2", - "source": { - "type": "git", - "url": "https://github.com/symfony/http-foundation.git", - "reference": "9344a87ceedfc50354a39653e54257ee9aa6a77d" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/symfony/http-foundation/zipball/9344a87ceedfc50354a39653e54257ee9aa6a77d", - "reference": "9344a87ceedfc50354a39653e54257ee9aa6a77d", - "shasum": "" - }, - "require": { - "php": ">=5.5.9" - }, - "require-dev": { - "symfony/expression-language": "~2.8|~3.0" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "3.0-dev" - } - }, - "autoload": { - "psr-4": { - "Symfony\\Component\\HttpFoundation\\": "" - }, - "exclude-from-classmap": [ - "/Tests/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Fabien Potencier", - "email": "fabien@symfony.com" - }, - { - "name": "Symfony Community", - "homepage": "https://symfony.com/contributors" - } - ], - "description": "Symfony HttpFoundation Component", - "homepage": "https://symfony.com", - "time": "2016-02-02 13:44:19" - }, { "name": "symfony/yaml", "version": "v3.0.2", diff --git a/src/Tissue.php b/src/Tissue.php index 6fda096..1aeca7d 100644 --- a/src/Tissue.php +++ b/src/Tissue.php @@ -5,72 +5,83 @@ use Github\Exception\InvalidArgumentException; use Github\Exception\MissingArgumentException; -use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Yaml\Exception\ParseException; use Symfony\Component\Yaml\Yaml; -define('CONFIG_PATH', dirname(__DIR__) . '/config/config.yaml'); - /** * Class Tissue * @package bouiboui\Tissue */ class Tissue { + /** @var array Configuration to be overwritten by the contents of config/config.yaml */ - private $config = []; + private static $config = []; + private static $configPath; /** - * ThrowController constructor. - * @param string $configPath - * @throws \ErrorException - * @throws ParseException + * Change config path (useful for tests) + * @param $configPath */ - public function __construct($configPath = CONFIG_PATH) + public static function setConfigPath($configPath) { - if (!file_exists($configPath) || !is_readable($configPath)) { - throw new \ErrorException('Config file not found or unreadable.'); - } - $config = Yaml::parse(file_get_contents($configPath))['tissue']; - - if (['you', 'repo'] !== array_keys($config) || - ['username', 'password'] !== array_keys($config['you']) || - ['author', 'name'] !== array_keys($config['repo']) - ) { - throw new \ErrorException('Invalid config file.'); - } - $this->config = $config; + static::$configPath = $configPath; } /** * Create an issue from the sent request - * @param Request $request + * @param null $message + * @param null $code + * @param null $severity + * @param null $path + * @param null $lineno + * @param null $trace * @return array + * @throws \ErrorException * @throws InvalidArgumentException * @throws MissingArgumentException - * @throws \ErrorException + * @throws ParseException */ - public function throwIssue(Request $request) + public static function create($message = null, $code = null, $severity = null, $path = null, $lineno = null, $trace = null) { - $code = $request->get('code'); - $filename = $request->get('filename'); - $lineno = $request->get('line'); - $message = $request->get('message'); - $severity = $request->get('severity'); - $trace = $request->get('trace'); + static::loadConfig(); - if ('' === trim($message . $code . $severity . $filename . $lineno . $trace)) { + if (null === array_unique([$message, $code, $severity, $path, $lineno, $trace])) { throw new \ErrorException('At least one parameter must be set.'); } - $issue = new GithubIssue($message, $code, $severity, $filename, $lineno, $trace); + $issue = new GithubIssue($message, $code, $severity, $path, $lineno, $trace); return $issue->commit( - $this->config['you']['username'], - $this->config['you']['password'], - $this->config['repo']['author'], - $this->config['repo']['name'] + static::$config['you']['username'], + static::$config['you']['password'], + static::$config['repo']['author'], + static::$config['repo']['name'] ); } + + /** + * Loads configuration + * @throws \ErrorException + * @throws ParseException + */ + static private function loadConfig() + { + if (null === static::$configPath) { + static::$configPath = dirname(__DIR__) . '/config/config.yaml'; + } + if (!file_exists(static::$configPath) || !is_readable(static::$configPath)) { + throw new \ErrorException('Config file not found or unreadable.'); + } + $config = Yaml::parse(file_get_contents(static::$configPath))['tissue']; + + if (['you', 'repo'] !== array_keys($config) || + ['username', 'password'] !== array_keys($config['you']) || + ['author', 'name'] !== array_keys($config['repo']) + ) { + throw new \ErrorException('Invalid config file.'); + } + static::$config = $config; + } } \ No newline at end of file diff --git a/tests/TissueTest.php b/tests/TissueTest.php index 850eb96..abf6d83 100644 --- a/tests/TissueTest.php +++ b/tests/TissueTest.php @@ -3,7 +3,6 @@ namespace bouiboui\Tissue; use Github\Exception\ErrorException; -use Symfony\Component\HttpFoundation\Request; define('TEST_CONFIG_PATH', dirname(__DIR__) . '/config/config.test.yaml'); @@ -25,15 +24,8 @@ public function testPartialIssueOne() } catch (\ErrorException $e) { - $c = new Tissue(TEST_CONFIG_PATH); - $r = Request::create('localhost', 'GET', [ - 'code' => $e->getCode(), - 'filename' => $e->getFile(), - 'message' => $e->getMessage(), - 'severity' => $e->getSeverity(), - ]); - - $c->throwIssue($r); + Tissue::setConfigPath(TEST_CONFIG_PATH); + Tissue::create($e->getMessage(), $e->getCode(), $e->getSeverity(), $e->getFile()); } } @@ -49,14 +41,8 @@ public function testPartialIssueTwo() } catch (\ErrorException $e) { - $c = new Tissue(TEST_CONFIG_PATH); - $r = Request::create('localhost', 'GET', [ - 'line' => $e->getLine(), - 'message' => $e->getMessage(), - 'trace' => $e->getTraceAsString(), - ]); - - $c->throwIssue($r); + Tissue::setConfigPath(TEST_CONFIG_PATH); + Tissue::create($e->getMessage(), null, null, null, $e->getLine(), $e->getTraceAsString()); } } @@ -72,13 +58,8 @@ public function testPartialIssueThree() } catch (\ErrorException $e) { - $c = new Tissue(TEST_CONFIG_PATH); - $r = Request::create('localhost', 'GET', [ - 'filename' => $e->getFile(), - 'trace' => $e->getTraceAsString(), - ]); - - $c->throwIssue($r); + Tissue::setConfigPath(TEST_CONFIG_PATH); + Tissue::create(null, null, null, $e->getFile(), null, $e->getTraceAsString()); } } @@ -94,17 +75,15 @@ public function testValidRequest() } catch (\ErrorException $e) { - $c = new Tissue(TEST_CONFIG_PATH); - $r = Request::create('localhost', 'GET', [ - 'code' => $e->getCode(), - 'filename' => $e->getFile(), - 'line' => $e->getLine(), - 'message' => $e->getMessage(), - 'severity' => $e->getSeverity(), - 'trace' => $e->getTraceAsString(), - ]); - - $result = $c->throwIssue($r); + Tissue::setConfigPath(TEST_CONFIG_PATH); + $result = Tissue::create( + $e->getMessage(), + $e->getCode(), + $e->getSeverity(), + $e->getFile(), + $e->getLine(), + $e->getTraceAsString() + ); static::assertNotNull($result, 'null result received'); static::assertTrue(array_key_exists('duplicate', $result), 'duplicate parameter missing');