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

108 lines
2.5 KiB
PHP
Raw Normal View History

2015-04-07 19:58:49 +02:00
<?php
2016-02-05 12:08:25 +01:00
declare(strict_types = 1);
2015-04-07 19:58:49 +02:00
namespace FireflyIII\Helpers\Help;
use Cache;
use League\CommonMark\CommonMarkConverter;
use Log;
use Requests;
2015-04-07 19:58:49 +02:00
use Route;
/**
* Class Help
*
* @package FireflyIII\Helpers\Help
*/
class Help implements HelpInterface
{
/**
* @codeCoverageIgnore
2015-05-14 09:51:54 +02:00
*
2016-02-05 09:25:15 +01:00
* @param string $key
2015-04-07 19:58:49 +02:00
*
* @return string
*/
public function getFromCache(string $key): string
2015-04-07 19:58:49 +02:00
{
return Cache::get($key);
}
/**
* @param string $language
2016-02-05 09:25:15 +01:00
* @param string $route
2015-04-07 19:58:49 +02:00
*
* @return array
*/
public function getFromGithub(string $language, string $route): array
2015-04-07 19:58:49 +02:00
{
$uri = sprintf('https://raw.githubusercontent.com/JC5/firefly-iii-help/master/%s/%s.md', $language, $route);
2015-06-29 15:23:50 +02: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 15:23:50 +02:00
'title' => $title,
2015-04-07 19:58:49 +02: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 19:58:49 +02:00
}
2015-04-07 19:58:49 +02: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 19:58:49 +02:00
}
$converter = new CommonMarkConverter();
$content['text'] = $converter->convertToHtml($content['text']);
return $content;
}
/**
* @codeCoverageIgnore
2015-05-14 09:51:54 +02:00
*
2016-02-05 09:25:15 +01:00
* @param string $route
2015-05-03 12:58:55 +02:00
*
* @return bool
2015-04-07 19:58:49 +02:00
*/
public function hasRoute(string $route):bool
2015-04-07 19:58:49 +02:00
{
return Route::has($route);
}
/**
* @codeCoverageIgnore
2015-05-14 09:51:54 +02:00
*
2016-02-05 09:25:15 +01:00
* @param string $route
2015-04-07 19:58:49 +02:00
*
2015-06-29 15:23:50 +02:00
* @return bool
2015-04-07 19:58:49 +02:00
*/
public function inCache(string $route):bool
2015-04-07 19:58:49 +02:00
{
2015-06-29 15:23:50 +02:00
return Cache::has('help.' . $route . '.title') && Cache::has('help.' . $route . '.text');
2015-04-07 19:58:49 +02:00
}
/**
* @codeCoverageIgnore
2015-05-14 09:51:54 +02:00
*
2016-02-05 09:25:15 +01:00
* @param string $route
* @param array $content
2015-04-07 19:58:49 +02:00
*
2015-06-29 15:23:50 +02:00
* @internal param $title
2015-04-07 19:58:49 +02:00
*/
2016-02-05 09:25:15 +01:00
public function putInCache(string $route, array $content)
2015-04-07 19:58:49 +02:00
{
2015-06-29 15:23:50 +02:00
Cache::put('help.' . $route . '.text', $content['text'], 10080); // a week.
Cache::put('help.' . $route . '.title', $content['title'], 10080);
2015-04-07 19:58:49 +02:00
}
}