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

100 lines
2.2 KiB
PHP
Raw Normal View History

2015-04-07 12:58:49 -05:00
<?php
2016-02-05 05:08:25 -06:00
declare(strict_types = 1);
2015-04-07 12:58:49 -05:00
namespace FireflyIII\Helpers\Help;
use Cache;
use ErrorException;
use League\CommonMark\CommonMarkConverter;
use Log;
use Route;
/**
* Class Help
*
* @package FireflyIII\Helpers\Help
*/
class Help implements HelpInterface
{
/**
* @codeCoverageIgnore
2015-05-14 02:51:54 -05:00
*
2016-02-05 02:25:15 -06:00
* @param string $key
2015-04-07 12:58:49 -05:00
*
* @return string
*/
2016-02-05 02:25:15 -06:00
public function getFromCache(string $key)
2015-04-07 12:58:49 -05:00
{
return Cache::get($key);
}
/**
2015-05-13 14:32:21 -05:00
* @codeCoverageIgnore
2015-05-14 02:51:54 -05:00
*
2016-02-05 02:25:15 -06:00
* @param string $route
2015-04-07 12:58:49 -05:00
*
* @return array
*/
2016-02-05 02:25:15 -06:00
public function getFromGithub(string $route)
2015-04-07 12:58:49 -05:00
{
2015-06-29 08:23:50 -05:00
$uri = 'https://raw.githubusercontent.com/JC5/firefly-iii-help/master/en/' . e($route) . '.md';
$routeIndex = str_replace('.', '-', $route);
$title = trans('help.' . $routeIndex);
$content = [
2015-04-07 12:58:49 -05:00
'text' => '<p>There is no help for this route!</p>',
2015-06-29 08:23:50 -05:00
'title' => $title,
2015-04-07 12:58:49 -05:00
];
try {
$content['text'] = file_get_contents($uri);
} catch (ErrorException $e) {
Log::error(trim($e->getMessage()));
}
if (strlen(trim($content['text'])) == 0) {
$content['text'] = '<p>There is no help for this route.</p>';
}
$converter = new CommonMarkConverter();
$content['text'] = $converter->convertToHtml($content['text']);
return $content;
}
/**
* @codeCoverageIgnore
2015-05-14 02:51:54 -05:00
*
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-02-05 02:25:15 -06:00
public function hasRoute(string $route)
2015-04-07 12:58:49 -05:00
{
return Route::has($route);
}
/**
* @codeCoverageIgnore
2015-05-14 02:51:54 -05:00
*
2016-02-05 02:25:15 -06:00
* @param string $route
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-02-05 02:25:15 -06:00
public function inCache(string $route)
2015-04-07 12:58:49 -05:00
{
2015-06-29 08:23:50 -05:00
return Cache::has('help.' . $route . '.title') && Cache::has('help.' . $route . '.text');
2015-04-07 12:58:49 -05:00
}
/**
* @codeCoverageIgnore
2015-05-14 02:51:54 -05:00
*
2016-02-05 02:25:15 -06:00
* @param string $route
* @param array $content
2015-04-07 12:58:49 -05:00
*
2015-06-29 08:23:50 -05:00
* @internal param $title
2015-04-07 12:58:49 -05:00
*/
2016-02-05 02:25:15 -06:00
public function putInCache(string $route, array $content)
2015-04-07 12:58:49 -05:00
{
2015-06-29 08:23:50 -05:00
Cache::put('help.' . $route . '.text', $content['text'], 10080); // a week.
Cache::put('help.' . $route . '.title', $content['title'], 10080);
2015-04-07 12:58:49 -05:00
}
}