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

108 lines
2.5 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 League\CommonMark\CommonMarkConverter;
use Log;
use Requests;
2015-04-07 12:58:49 -05:00
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
*/
public function getFromCache(string $key): string
2015-04-07 12:58:49 -05:00
{
return Cache::get($key);
}
/**
* @param string $language
2016-02-05 02:25:15 -06:00
* @param string $route
2015-04-07 12:58:49 -05:00
*
* @return array
*/
public function getFromGithub(string $language, string $route): array
2015-04-07 12:58:49 -05:00
{
$uri = sprintf('https://raw.githubusercontent.com/JC5/firefly-iii-help/master/%s/%s.md', $language, $route);
2015-06-29 08:23:50 -05:00
$routeIndex = str_replace('.', '-', $route);
$title = trans('help.' . $routeIndex);
$content = [
'text' => '<p>There is no help for this route, or there is no help available in your language.</p>',
2015-06-29 08:23:50 -05:00
'title' => $title,
2015-04-07 12:58:49 -05:00
];
Log::debug('Going to get from Github: ' . $uri);
$result = Requests::get($uri);
Log::debug('Status code was ' . $result->status_code . '.');
if ($result->status_code === 200) {
$content['text'] = $result->body;
2015-04-07 12:58:49 -05:00
}
2015-04-07 12:58:49 -05:00
if (strlen(trim($content['text'])) == 0) {
Log::debug('No actual help text for this route (even though a page was found).');
$content['text'] = '<p>There is no help for this route, or there is no help available in your language.</p>';
2015-04-07 12:58:49 -05:00
}
$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
*/
public function hasRoute(string $route):bool
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
*/
public function inCache(string $route):bool
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
}
}