Cleanup code.

This commit is contained in:
James Cole 2015-06-06 17:40:41 +02:00
parent a0f34a7ce1
commit 1a1f127993
2 changed files with 174 additions and 78 deletions

View File

@ -28,33 +28,94 @@ class General extends Twig_Extension
*/ */
public function getFilters() public function getFilters()
{ {
$filters = []; return [
$this->formatAmount(),
$this->formatTransaction(),
$this->formatAmountPlain(),
$this->formatJournal(),
$this->balance(),
$this->getAccountRole()
];
$filters[] = new Twig_SimpleFilter( }
/**
* {@inheritDoc}
*/
public function getFunctions()
{
return [
$this->getCurrencyCode(),
$this->getCurrencySymbol(),
$this->phpdate(),
$this->env(),
$this->activeRoute()
];
}
/**
* {@inheritDoc}
*/
public function getName()
{
return 'FireflyIII\Support\Twig\General';
}
/**
* @return Twig_SimpleFilter
*/
protected function formatAmount()
{
return new Twig_SimpleFilter(
'formatAmount', function ($string) { 'formatAmount', function ($string) {
return App::make('amount')->format($string); return App::make('amount')->format($string);
}, ['is_safe' => ['html']] }, ['is_safe' => ['html']]
); );
}
$filters[] = new Twig_SimpleFilter( /**
* @return Twig_SimpleFilter
*/
protected function formatTransaction()
{
return new Twig_SimpleFilter(
'formatTransaction', function (Transaction $transaction) { 'formatTransaction', function (Transaction $transaction) {
return App::make('amount')->formatTransaction($transaction); return App::make('amount')->formatTransaction($transaction);
}, ['is_safe' => ['html']] }, ['is_safe' => ['html']]
); );
}
$filters[] = new Twig_SimpleFilter( /**
* @return Twig_SimpleFilter
*/
protected function formatAmountPlain()
{
return new Twig_SimpleFilter(
'formatAmountPlain', function ($string) { 'formatAmountPlain', function ($string) {
return App::make('amount')->format($string, false); return App::make('amount')->format($string, false);
}, ['is_safe' => ['html']] }, ['is_safe' => ['html']]
); );
}
$filters[] = new Twig_SimpleFilter( /**
* @return Twig_SimpleFilter
*/
protected function formatJournal()
{
return new Twig_SimpleFilter(
'formatJournal', function ($journal) { 'formatJournal', function ($journal) {
return App::make('amount')->formatJournal($journal); return App::make('amount')->formatJournal($journal);
}, ['is_safe' => ['html']] }, ['is_safe' => ['html']]
); );
}
$filters[] = new Twig_SimpleFilter( /**
* @return Twig_SimpleFilter
*/
protected function balance()
{
return new Twig_SimpleFilter(
'balance', function (Account $account = null) { 'balance', function (Account $account = null) {
if (is_null($account)) { if (is_null($account)) {
return 'NULL'; return 'NULL';
@ -64,50 +125,74 @@ class General extends Twig_Extension
return App::make('steam')->balance($account, $date); return App::make('steam')->balance($account, $date);
} }
); );
}
// should be a function but OK /**
$filters[] = new Twig_SimpleFilter( * @return Twig_SimpleFilter
*/
protected function getAccountRole()
{
return new Twig_SimpleFilter(
'getAccountRole', function ($name) { 'getAccountRole', function ($name) {
return Config::get('firefly.accountRoles.' . $name); return Config::get('firefly.accountRoles.' . $name);
} }
); );
return $filters;
} }
/** /**
* {@inheritDoc} * @return Twig_SimpleFunction
*/ */
public function getFunctions() protected function getCurrencyCode()
{ {
$functions = []; return new Twig_SimpleFunction(
$functions[] = new Twig_SimpleFunction(
'getCurrencyCode', function () { 'getCurrencyCode', function () {
return App::make('amount')->getCurrencyCode(); return App::make('amount')->getCurrencyCode();
} }
); );
}
$functions[] = new Twig_SimpleFunction( /**
* @return Twig_SimpleFunction
*/
protected function getCurrencySymbol()
{
return new Twig_SimpleFunction(
'getCurrencySymbol', function () { 'getCurrencySymbol', function () {
return App::make('amount')->getCurrencySymbol(); return App::make('amount')->getCurrencySymbol();
} }
); );
}
$functions[] = new Twig_SimpleFunction( /**
* @return Twig_SimpleFunction
*/
protected function phpdate()
{
return new Twig_SimpleFunction(
'phpdate', function ($str) { 'phpdate', function ($str) {
return date($str); return date($str);
} }
); );
}
/**
$functions[] = new Twig_SimpleFunction( * @return Twig_SimpleFunction
*/
protected function env()
{
return new Twig_SimpleFunction(
'env', function ($name, $default) { 'env', function ($name, $default) {
return env($name, $default); return env($name, $default);
} }
); );
}
$functions[] = new Twig_SimpleFunction( /**
* @return Twig_SimpleFunction
*/
protected function activeRoute()
{
return new Twig_SimpleFunction(
'activeRoute', function ($context) { 'activeRoute', function ($context) {
$args = func_get_args(); $args = func_get_args();
$route = $args[1]; $route = $args[1];
@ -133,18 +218,6 @@ class General extends Twig_Extension
return 'not-xxx-at-all'; return 'not-xxx-at-all';
}, ['needs_context' => true] }, ['needs_context' => true]
); );
return $functions;
}
/**
* {@inheritDoc}
*/
public function getName()
{
return 'FireflyIII\Support\Twig\General';
} }
} }

View File

@ -19,14 +19,44 @@ class Journal extends Twig_Extension
{ {
/** /**
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @return array * @return array
*/ */
public function getFilters() public function getFilters()
{ {
$filters = []; $filters = [$this->typeIcon()];
$filters[] = new Twig_SimpleFilter( return $filters;
}
/**
* @return array
*/
public function getFunctions()
{
$functions = [
$this->invalidJournal(),
$this->relevantTags()
];
return $functions;
}
/**
* Returns the name of the extension.
*
* @return string The extension name
*/
public function getName()
{
return 'FireflyIII\Support\Twig\Journals';
}
/**
* @return Twig_SimpleFilter
*/
protected function typeIcon()
{
return new Twig_SimpleFilter(
'typeIcon', function (TransactionJournal $journal) { 'typeIcon', function (TransactionJournal $journal) {
$cache = new CacheProperties(); $cache = new CacheProperties();
@ -62,20 +92,14 @@ class Journal extends Twig_Extension
}, ['is_safe' => ['html']] }, ['is_safe' => ['html']]
); );
return $filters;
} }
/** /**
* @SuppressWarnings(PHPMD.CyclomaticComplexity) * @return Twig_SimpleFunction
*
* @return array
*/ */
public function getFunctions() protected function invalidJournal()
{ {
$functions = []; return new Twig_SimpleFunction(
$functions[] = new Twig_SimpleFunction(
'invalidJournal', function (TransactionJournal $journal) { 'invalidJournal', function (TransactionJournal $journal) {
if (!isset($journal->transactions[1]) || !isset($journal->transactions[0])) { if (!isset($journal->transactions[1]) || !isset($journal->transactions[0])) {
return true; return true;
@ -84,8 +108,14 @@ class Journal extends Twig_Extension
return false; return false;
} }
); );
}
$functions[] = new Twig_SimpleFunction( /**
* @return Twig_SimpleFunction
*/
protected function relevantTags()
{
return new Twig_SimpleFunction(
'relevantTags', function (TransactionJournal $journal) { 'relevantTags', function (TransactionJournal $journal) {
$cache = new CacheProperties; $cache = new CacheProperties;
$cache->addProperty('relevantTags'); $cache->addProperty('relevantTags');
@ -98,6 +128,7 @@ class Journal extends Twig_Extension
if ($journal->tags->count() == 0) { if ($journal->tags->count() == 0) {
$string = App::make('amount')->formatJournal($journal); $string = App::make('amount')->formatJournal($journal);
$cache->store($string); $cache->store($string);
return $string; return $string;
} }
@ -110,6 +141,7 @@ class Journal extends Twig_Extension
$string = '<a href="' . route('tags.show', [$tag->id]) . '" class="label label-success" title="' . $amount $string = '<a href="' . route('tags.show', [$tag->id]) . '" class="label label-success" title="' . $amount
. '"><i class="fa fa-fw fa-refresh"></i> ' . $tag->tag . '</a>'; . '"><i class="fa fa-fw fa-refresh"></i> ' . $tag->tag . '</a>';
$cache->store($string); $cache->store($string);
return $string; return $string;
} }
@ -121,6 +153,7 @@ class Journal extends Twig_Extension
$string = '<a href="' . route('tags.show', [$tag->id]) . '" class="label label-success" title="' . $amount $string = '<a href="' . route('tags.show', [$tag->id]) . '" class="label label-success" title="' . $amount
. '"><i class="fa fa-fw fa-sort-numeric-desc"></i> ' . $tag->tag . '</a>'; . '"><i class="fa fa-fw fa-sort-numeric-desc"></i> ' . $tag->tag . '</a>';
$cache->store($string); $cache->store($string);
return $string; return $string;
} }
/* /*
@ -132,6 +165,7 @@ class Journal extends Twig_Extension
$string = '<a href="' . route('tags.show', [$tag->id]) . '">' . $amount . '</a>'; $string = '<a href="' . route('tags.show', [$tag->id]) . '">' . $amount . '</a>';
$cache->store($string); $cache->store($string);
return $string; return $string;
} }
@ -140,6 +174,7 @@ class Journal extends Twig_Extension
// return the amount: // return the amount:
$string = App::make('amount')->formatJournal($journal); $string = App::make('amount')->formatJournal($journal);
$cache->store($string); $cache->store($string);
return $string; return $string;
} }
} }
@ -148,17 +183,5 @@ class Journal extends Twig_Extension
return 'TODO: ' . $journal->amount; return 'TODO: ' . $journal->amount;
} }
); );
return $functions;
}
/**
* Returns the name of the extension.
*
* @return string The extension name
*/
public function getName()
{
return 'FireflyIII\Support\Twig\Journals';
} }
} }