Simplified some code.

This commit is contained in:
James Cole
2016-01-02 16:32:08 +01:00
parent 265dd37212
commit 294df4a2b3
5 changed files with 106 additions and 150 deletions

View File

@@ -14,6 +14,7 @@ use Navigation;
use Preferences;
use Response;
use Session;
use stdClass;
/**
* Class CategoryController
@@ -115,19 +116,17 @@ class CategoryController extends Controller
return Response::json($cache->get()); // @codeCoverageIgnore
}
$array = $repository->getCategoriesAndExpenses($start, $end);
// sort by callback:
uasort(
$array,
function ($left, $right) {
if ($left['sum'] == $right['sum']) {
return 0;
}
// get data for categories (and "no category"):
$set = $repository->spentForAccountsPerMonth(new Collection, $start, $end);
$outside = $repository->sumSpentNoCategory(new Collection, $start, $end);
return ($left['sum'] < $right['sum']) ? -1 : 1;
}
);
$set = new Collection($array);
// this is a "fake" entry for the "no category" entry.
$entry = new stdClass();
$entry->name = trans('firefly.no_category');
$entry->spent = $outside;
$set->push($entry);
$set = $set->sortBy('spent');
$data = $this->generator->frontpage($set);
$cache->store($data);

View File

@@ -11,6 +11,34 @@ use FireflyIII\User;
*/
class CronController extends Controller
{
/** @var array */
protected $set = [];
/** @var array */
protected $parameters = [];
/**
* CronController constructor.
*/
public function __construct()
{
parent::__construct();
$this->set = [
'blocks' => 'https://api.sendgrid.com/api/blocks.get.json',
'bounces' => 'https://api.sendgrid.com/api/bounces.get.json',
'invalids' => 'https://api.sendgrid.com/api/invalidemails.get.json',
];
$this->parameters = [
'api_user' => env('SENDGRID_USERNAME'),
'api_key' => env('SENDGRID_PASSWORD'),
'date' => 1,
'days' => 7
];
}
/**
* Firefly doesn't have anything that should be in the a cron job, except maybe this one, and it's fairly exceptional.
@@ -25,44 +53,11 @@ class CronController extends Controller
if (strlen(env('SENDGRID_USERNAME')) > 0 && strlen(env('SENDGRID_PASSWORD')) > 0) {
$set = [
'blocks' => 'https://api.sendgrid.com/api/blocks.get.json',
'bounces' => 'https://api.sendgrid.com/api/bounces.get.json',
'invalids' => 'https://api.sendgrid.com/api/invalidemails.get.json',
];
echo '<pre>';
foreach ($set as $name => $URL) {
foreach ($this->set as $name => $url) {
$data = json_decode(file_get_contents($url . '?' . http_build_query($this->parameters)));
$this->processResult($name, $data);
$parameters = [
'api_user' => env('SENDGRID_USERNAME'),
'api_key' => env('SENDGRID_PASSWORD'),
'date' => 1,
'days' => 7
];
$fullURL = $URL . '?' . http_build_query($parameters);
$data = json_decode(file_get_contents($fullURL));
/*
* Loop the result, if any.
*/
if (is_array($data)) {
echo 'Found ' . count($data) . ' entries in the SendGrid ' . $name . ' list.' . "\n";
foreach ($data as $entry) {
$address = $entry->email;
$user = User::where('email', $address)->where('blocked', 0)->first();
if (!is_null($user)) {
echo 'Found a user: ' . $address . ', who is now blocked.' . "\n";
$user->blocked = 1;
$user->blocked_code = 'bounced';
$user->password = 'bounced';
$user->save();
} else {
echo 'Found no user: ' . $address . ', did nothing.' . "\n";
}
}
}
}
echo 'Done!' . "\n";
} else {
@@ -71,4 +66,29 @@ class CronController extends Controller
}
/**
* @param string $name
* @param array $data
*/
protected function processResult($name, array $data)
{
if (is_array($data)) {
echo 'Found ' . count($data) . ' entries in the SendGrid ' . $name . ' list.' . "\n";
foreach ($data as $entry) {
$address = $entry->email;
$user = User::where('email', $address)->where('blocked', 0)->first();
if (!is_null($user)) {
echo 'Found a user: ' . $address . ', who is now blocked.' . "\n";
$user->blocked = 1;
$user->blocked_code = 'bounced';
$user->password = 'bounced';
$user->save();
} else {
echo 'Found no user: ' . $address . ', did nothing.' . "\n";
}
}
}
}
}