Skip to content
This repository has been archived by the owner on Apr 4, 2022. It is now read-only.

Commit

Permalink
Removed Request dependency and changed to static calls
Browse files Browse the repository at this point in the history
  • Loading branch information
bouiboui committed Feb 21, 2016
1 parent 3820977 commit d8a6b9f
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 128 deletions.
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
56 changes: 2 additions & 54 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

83 changes: 47 additions & 36 deletions src/Tissue.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
51 changes: 15 additions & 36 deletions tests/TissueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -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());
}
}

Expand All @@ -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());
}
}

Expand All @@ -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());
}
}

Expand All @@ -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');
Expand Down

0 comments on commit d8a6b9f

Please sign in to comment.