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

View File

@ -19,15 +19,45 @@ class Journal extends Twig_Extension
{
/**
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @return array
*/
public function getFilters()
{
$filters = [];
$filters = [$this->typeIcon()];
$filters[] = new Twig_SimpleFilter(
'typeIcon', function(TransactionJournal $journal) {
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) {
$cache = new CacheProperties();
$cache->addProperty($journal->id);
@ -62,21 +92,15 @@ class Journal extends Twig_Extension
}, ['is_safe' => ['html']]
);
return $filters;
}
/**
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*
* @return array
* @return Twig_SimpleFunction
*/
public function getFunctions()
protected function invalidJournal()
{
$functions = [];
$functions[] = new Twig_SimpleFunction(
'invalidJournal', function(TransactionJournal $journal) {
return new Twig_SimpleFunction(
'invalidJournal', function (TransactionJournal $journal) {
if (!isset($journal->transactions[1]) || !isset($journal->transactions[0])) {
return true;
}
@ -84,20 +108,27 @@ class Journal extends Twig_Extension
return false;
}
);
}
$functions[] = new Twig_SimpleFunction(
'relevantTags', function(TransactionJournal $journal) {
/**
* @return Twig_SimpleFunction
*/
protected function relevantTags()
{
return new Twig_SimpleFunction(
'relevantTags', function (TransactionJournal $journal) {
$cache = new CacheProperties;
$cache->addProperty('relevantTags');
$cache->addProperty($journal->id);
if($cache->has()) {
if ($cache->has()) {
return $cache->get(); // @codeCoverageIgnore
}
if ($journal->tags->count() == 0) {
$string = App::make('amount')->formatJournal($journal);
$cache->store($string);
return $string;
}
@ -107,9 +138,10 @@ class Journal extends Twig_Extension
// return tag formatted for a "balancing act", even if other
// tags are present.
$amount = App::make('amount')->format($journal->actual_amount, false);
$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>';
$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>';
$cache->store($string);
return $string;
}
@ -119,8 +151,9 @@ class Journal extends Twig_Extension
if ($tag->tagMode == 'advancePayment' && $journal->transactionType->type == 'Deposit') {
$amount = App::make('amount')->formatJournal($journal, false);
$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);
return $string;
}
/*
@ -132,6 +165,7 @@ class Journal extends Twig_Extension
$string = '<a href="' . route('tags.show', [$tag->id]) . '">' . $amount . '</a>';
$cache->store($string);
return $string;
}
@ -140,6 +174,7 @@ class Journal extends Twig_Extension
// return the amount:
$string = App::make('amount')->formatJournal($journal);
$cache->store($string);
return $string;
}
}
@ -148,17 +183,5 @@ class Journal extends Twig_Extension
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';
}
}