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

134 lines
3.3 KiB
PHP
Raw Normal View History

2015-04-07 19:58:49 +02:00
<?php
/**
* Help.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
*/
2017-03-24 15:15:12 +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;
use Requests_Exception;
2015-04-07 19:58:49 +02:00
use Route;
/**
* Class Help
*
* @package FireflyIII\Helpers\Help
*/
class Help implements HelpInterface
{
2017-07-23 08:32:51 +02:00
const CACHEKEY = 'help_%s_%s';
2016-11-02 14:33:57 +01: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 19:58:49 +02:00
/**
2016-10-23 17:33:53 +02:00
* @param string $route
* @param string $language
2015-04-07 19:58:49 +02:00
*
* @return string
*/
2016-10-23 17:33:53 +02:00
public function getFromCache(string $route, string $language): string
2015-04-07 19:58:49 +02:00
{
2017-07-23 07:30:05 +02:00
$line = sprintf(self::CACHEKEY, $route, $language);
return Cache::get($line);
2015-04-07 19:58:49 +02:00
}
/**
2016-02-05 09:25:15 +01:00
* @param string $route
2017-03-19 17:54:21 +01:00
* @param string $language
2015-04-07 19:58:49 +02:00
*
2016-10-23 17:33:53 +02:00
* @return string
2015-04-07 19:58:49 +02:00
*/
2017-03-19 17:54:21 +01:00
public function getFromGithub(string $route, string $language): string
2015-04-07 19:58:49 +02: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 14:33:57 +01:00
$opt = ['useragent' => $this->userAgent];
$content = '';
try {
2016-11-02 14:33:57 +01:00
$result = Requests::get($uri, [], $opt);
} catch (Requests_Exception $e) {
Log::error($e);
return '';
2015-04-07 19:58:49 +02:00
}
Log::debug(sprintf('Status code is %d', $result->status_code));
if ($result->status_code === 200) {
$content = trim($result->body);
}
2017-07-23 07:30:05 +02:00
if (strlen($content) > 0) {
Log::debug('Content is longer than zero. Expect something.');
$converter = new CommonMarkConverter();
$content = $converter->convertToHtml($content);
2015-04-07 19:58:49 +02:00
}
return $content;
}
/**
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
*/
2016-12-14 18:59:12 +01:00
public function hasRoute(string $route): bool
2015-04-07 19:58:49 +02:00
{
return Route::has($route);
}
/**
2016-02-05 09:25:15 +01:00
* @param string $route
2016-10-23 17:33:53 +02:00
* @param string $language
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
*/
2016-12-14 18:59:12 +01:00
public function inCache(string $route, string $language): bool
2015-04-07 19:58:49 +02:00
{
2017-07-23 07:30:05 +02: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 19:58:49 +02:00
}
/**
2015-05-14 09:51:54 +02:00
*
2016-02-05 09:25:15 +01:00
* @param string $route
2016-04-09 09:27:04 +02:00
* @param string $language
2016-10-23 17:33:53 +02:00
* @param string $content
2015-04-07 19:58:49 +02:00
*
*/
2016-10-23 17:33:53 +02:00
public function putInCache(string $route, string $language, string $content)
2015-04-07 19:58:49 +02:00
{
2017-07-23 07:30:05 +02:00
$key = sprintf(self::CACHEKEY, $route, $language);
2016-11-02 14:33:57 +01:00
if (strlen($content) > 0) {
Log::debug(sprintf('Will store entry in cache: %s', $key));
Cache::put($key, $content, 10080); // a week.
2017-03-24 15:15:12 +01:00
2016-11-02 14:33:57 +01:00
return;
}
Log::info(sprintf('Will not cache %s because content is empty.', $key));
2015-04-07 19:58:49 +02:00
}
}