firefly-iii/app/Handlers/Events/VersionCheckEventHandler.php

94 lines
3.3 KiB
PHP
Raw Normal View History

2017-12-28 04:38:40 -06:00
<?php
/**
* VersionCheckEventHandler.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
2018-07-07 00:48:10 -05:00
/** @noinspection MultipleReturnStatementsInspection */
/** @noinspection NullPointerExceptionInspection */
2017-12-28 04:38:40 -06:00
declare(strict_types=1);
namespace FireflyIII\Handlers\Events;
2018-07-25 23:10:17 -05:00
use FireflyConfig;
2017-12-28 12:03:15 -06:00
use FireflyIII\Events\RequestedVersionCheckStatus;
2018-07-08 00:59:58 -05:00
use FireflyIII\Helpers\Update\UpdateTrait;
use FireflyIII\Models\Configuration;
2018-03-30 15:40:20 -05:00
use FireflyIII\Repositories\User\UserRepositoryInterface;
2017-12-28 04:38:40 -06:00
use FireflyIII\User;
use Log;
2018-08-06 12:14:30 -05:00
2017-12-28 04:38:40 -06:00
/**
* Class VersionCheckEventHandler
*/
class VersionCheckEventHandler
{
2018-07-08 00:59:58 -05:00
use UpdateTrait;
2017-12-28 12:03:15 -06:00
2017-12-28 04:38:40 -06:00
/**
2018-07-07 00:48:10 -05:00
* Checks with GitHub to see if there is a new version.
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
2017-12-28 12:03:15 -06:00
* @param RequestedVersionCheckStatus $event
2017-12-28 04:38:40 -06:00
*/
public function checkForUpdates(RequestedVersionCheckStatus $event): void
2017-12-28 04:38:40 -06:00
{
Log::debug('Now in checkForUpdates()');
2017-12-28 04:38:40 -06:00
// in Sandstorm, cannot check for updates:
2018-03-30 15:40:20 -05:00
$sandstorm = 1 === (int)getenv('SANDSTORM');
2018-07-07 00:48:10 -05:00
if (true === $sandstorm) {
Log::debug('This is Sandstorm instance, done.');
2018-03-30 15:40:20 -05:00
return; // @codeCoverageIgnore
2017-12-28 04:38:40 -06:00
}
2018-03-30 15:40:20 -05:00
/** @var UserRepositoryInterface $repository */
$repository = app(UserRepositoryInterface::class);
2017-12-28 04:38:40 -06:00
/** @var User $user */
$user = $event->user;
2018-03-30 15:40:20 -05:00
if (!$repository->hasRole($user, 'owner')) {
Log::debug('User is not admin, done.');
return;
}
/** @var Configuration $lastCheckTime */
$lastCheckTime = FireflyConfig::get('last_update_check', time());
2017-12-28 04:38:40 -06:00
$now = time();
2018-03-30 15:40:20 -05:00
$diff = $now - $lastCheckTime->data;
Log::debug(sprintf('Last check time is %d, current time is %d, difference is %d', $lastCheckTime->data, $now, $diff));
2018-03-30 15:40:20 -05:00
if ($diff < 604800) {
2018-01-21 04:09:55 -06:00
Log::debug(sprintf('Checked for updates less than a week ago (on %s).', date('Y-m-d H:i:s', $lastCheckTime->data)));
2017-12-28 04:38:40 -06:00
return;
2017-12-28 04:38:40 -06:00
}
// last check time was more than a week ago.
Log::debug('Have not checked for a new version in a week!');
2018-07-07 00:48:10 -05:00
$latestRelease = $this->getLatestRelease();
$versionCheck = $this->versionCheck($latestRelease);
2018-08-06 12:14:30 -05:00
$resultString = $this->parseResult($versionCheck, $latestRelease);
2018-07-08 00:59:58 -05:00
if (0 !== $versionCheck && '' !== $resultString) {
2018-07-07 00:48:10 -05:00
// flash info
2018-07-08 00:59:58 -05:00
session()->flash('info', $resultString);
2018-07-07 00:48:10 -05:00
}
FireflyConfig::set('last_update_check', time());
}
2018-03-05 12:35:58 -06:00
}