firefly-iii/app/Helpers/Help/Help.php

139 lines
3.7 KiB
PHP
Raw Normal View History

2015-04-07 12:58:49 -05:00
<?php
/**
* Help.php
2017-10-21 01:40:00 -05:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 01:40:00 -05:00
* This file is part of Firefly III.
*
2017-10-21 01:40:00 -05:00
* 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
2017-12-17 07:41:58 -06:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
2017-03-24 09:15:12 -05:00
declare(strict_types=1);
2015-04-07 12:58:49 -05:00
namespace FireflyIII\Helpers\Help;
use Cache;
2018-04-02 07:42:07 -05:00
use Exception;
2015-04-07 12:58:49 -05:00
use League\CommonMark\CommonMarkConverter;
use Log;
use Requests;
2015-04-07 12:58:49 -05:00
use Route;
/**
2017-11-15 05:25:49 -06:00
* Class Help.
2015-04-07 12:58:49 -05:00
*/
class Help implements HelpInterface
{
2017-12-17 07:30:53 -06:00
/**
*
*/
public const CACHEKEY = 'help_%s_%s';
2016-11-02 08:33:57 -05:00
/** @var string */
protected $userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36';
2015-04-07 12:58:49 -05:00
/**
2016-10-23 10:33:53 -05:00
* @param string $route
* @param string $language
2015-04-07 12:58:49 -05:00
*
* @return string
*/
2016-10-23 10:33:53 -05:00
public function getFromCache(string $route, string $language): string
2015-04-07 12:58:49 -05:00
{
2017-07-23 00:30:05 -05:00
$line = sprintf(self::CACHEKEY, $route, $language);
return Cache::get($line);
2015-04-07 12:58:49 -05:00
}
/**
2016-02-05 02:25:15 -06:00
* @param string $route
2017-03-19 11:54:21 -05:00
* @param string $language
2015-04-07 12:58:49 -05:00
*
2016-10-23 10:33:53 -05:00
* @return string
2015-04-07 12:58:49 -05:00
*/
2017-03-19 11:54:21 -05:00
public function getFromGithub(string $route, string $language): string
2015-04-07 12:58:49 -05:00
{
$uri = sprintf('https://raw.githubusercontent.com/firefly-iii/help/master/%s/%s.md', $language, $route);
Log::debug(sprintf('Trying to get %s...', $uri));
2016-11-02 08:33:57 -05:00
$opt = ['useragent' => $this->userAgent];
$content = '';
try {
2016-11-02 08:33:57 -05:00
$result = Requests::get($uri, [], $opt);
2018-04-02 07:42:07 -05:00
} catch (Exception $e) {
Log::error($e);
return '';
2015-04-07 12:58:49 -05:00
}
Log::debug(sprintf('Status code is %d', $result->status_code));
2017-11-15 05:25:49 -06:00
if (200 === $result->status_code) {
$content = trim($result->body);
}
2017-07-23 00:30:05 -05:00
2018-04-27 23:23:13 -05:00
if (\strlen($content) > 0) {
Log::debug('Content is longer than zero. Expect something.');
$converter = new CommonMarkConverter();
$content = $converter->convertToHtml($content);
2015-04-07 12:58:49 -05:00
}
return $content;
}
/**
2016-02-05 02:25:15 -06:00
* @param string $route
2015-05-03 05:58:55 -05:00
*
* @return bool
2015-04-07 12:58:49 -05:00
*/
2016-12-14 11:59:12 -06:00
public function hasRoute(string $route): bool
2015-04-07 12:58:49 -05:00
{
return Route::has($route);
}
/**
2016-02-05 02:25:15 -06:00
* @param string $route
2016-10-23 10:33:53 -05:00
* @param string $language
2015-04-07 12:58:49 -05:00
*
2015-06-29 08:23:50 -05:00
* @return bool
2015-04-07 12:58:49 -05:00
*/
2016-12-14 11:59:12 -06:00
public function inCache(string $route, string $language): bool
2015-04-07 12:58:49 -05:00
{
2017-07-23 00:30:05 -05:00
$line = sprintf(self::CACHEKEY, $route, $language);
$result = Cache::has($line);
if ($result) {
Log::debug(sprintf('Cache has this entry: %s', 'help.' . $route . '.' . $language));
}
if (!$result) {
Log::debug(sprintf('Cache does not have this entry: %s', 'help.' . $route . '.' . $language));
}
return $result;
2015-04-07 12:58:49 -05:00
}
/**
2016-02-05 02:25:15 -06:00
* @param string $route
2016-04-09 02:27:04 -05:00
* @param string $language
2016-10-23 10:33:53 -05:00
* @param string $content
2015-04-07 12:58:49 -05:00
*/
2016-10-23 10:33:53 -05:00
public function putInCache(string $route, string $language, string $content)
2015-04-07 12:58:49 -05:00
{
2017-07-23 00:30:05 -05:00
$key = sprintf(self::CACHEKEY, $route, $language);
2018-04-27 23:23:13 -05:00
if (\strlen($content) > 0) {
2016-11-02 08:33:57 -05:00
Log::debug(sprintf('Will store entry in cache: %s', $key));
Cache::put($key, $content, 10080); // a week.
2017-03-24 09:15:12 -05:00
2016-11-02 08:33:57 -05:00
return;
}
Log::info(sprintf('Will not cache %s because content is empty.', $key));
2015-04-07 12:58:49 -05:00
}
}