Skip to content
Permalink
Browse files

Merge pull request #17 from supergrecko/logger

Create PSR logger
  • Loading branch information...
supergrecko committed Jun 25, 2019
2 parents e82bee2 + 001e349 commit f267fac0f0e88983933a55988cce40ca14ae6de8
@@ -16,7 +16,8 @@
"nesbot/carbon": "2.9.1",
"psr/simple-cache": "1.0.1",
"league/flysystem": "^1.0",
"illuminate/support": "5.8.*"
"illuminate/support": "5.8.*",
"psr/log": "^1.1"
},
"require-dev": {
"phpunit/phpunit": "7.5",
@@ -1,5 +1,5 @@
---
title: <?php echo e($page->name); ?>
title: <?php echo e($page->name); ?>

extends: _layouts.documentation
section: content
@@ -21,4 +21,4 @@ public function <?php echo e($name); ?>( <?php echo e(count($state->params) > 0
```

<?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
<?php /**PATH D:\Projects\PHP\LeagueQuest\src\RiotQuest\Codegen\assets/layout.blade.php ENDPATH**/ ?>
<?php /**PATH D:\Projects\PHP\LeagueQuest\src\RiotQuest\Codegen\assets/layout.blade.php ENDPATH**/ ?>
@@ -6,13 +6,14 @@
use League\Flysystem\FileExistsException;
use League\Flysystem\FileNotFoundException;
use League\Flysystem\Filesystem;
use Psr\SimpleCache\CacheInterface;
use RiotQuest\Contracts\LeagueException;
/**
* Class Cache
* @package RiotQuest\Components\Framework\Cache
*/
class Cache
class Cache implements CacheInterface
{
/**
@@ -164,4 +165,33 @@ private function hash($sub): string
}
/**
* @param string $key
* @return bool|void
* @throws FileNotFoundException
*/
public function delete($key)
{
@$this->fs->delete($this->hash($key));
}
/**
* @return bool|void
*/
public function clear()
{
$this->fs->deleteDir('/');
}
/**
* @param iterable $keys
* @return bool|void
* @throws FileNotFoundException
*/
public function deleteMultiple($keys)
{
foreach ($keys as $key) {
$this->delete($key);
}
}
}
@@ -11,6 +11,7 @@
use RiotQuest\Components\Cache\Cache;
use RiotQuest\Components\Cache\RateLimitCache;
use RiotQuest\Components\Cache\RequestCache;
use RiotQuest\Components\Logger\Logger;
use RiotQuest\Components\RateLimit\Manager;
use RiotQuest\Contracts\LeagueException;
use Symfony\Component\Dotenv\Dotenv;
@@ -51,6 +52,11 @@ class Application
*/
protected $limits = [];
/**
* @var Logger
*/
protected $logger;
/**
* @var string
*/
@@ -110,6 +116,8 @@ public function load(): void
'limits' => new RateLimitCache()
];
$this->logger = new Logger();
call_user_func([BaseProvider::class, 'onEnable']);
call_user_func([Provider::class, 'boot']);
$this->manager = new Manager();
@@ -168,6 +176,16 @@ public function register(string $region, string $endpoint, string $key, $limits)
$this->getManager()->registerCall($region, 'default', $key, $this->keys[strtoupper($key)]->getLimits());
}
/**
* @param $level
* @param $message
* @param array $context
*/
public static function log($level, $message, $context = []): void
{
self::getInstance()->getLogger()->log($level, $message, $context);
}
/**
* @param $target
* @return Token
@@ -177,6 +195,14 @@ private function getKey($target): Token
return new Token($_ENV["RIOTQUEST_{$target}_KEY"], $target, $_ENV["RIOTQUEST_{$target}_LIMIT"]);
}
/**
* @return Logger
*/
public function getLogger(): Logger
{
return $this->logger;
}
/**
* @return Manager
*/
@@ -232,7 +258,7 @@ public function getCache(string $key = 'generic'): Cache
}
/**
* @return Token[]
* @return Token[]
*/
public function getKeys(): array
{
@@ -54,7 +54,7 @@ public static function setProvider(string $provider): void
* @param int $ttl
* @return Champion
*/
public static function champion(string $region, $ttl = 3600): Champion
public static function champion(string $region, $ttl = 3600): Champion
{
return new Champion($region, $ttl);
}
@@ -79,6 +79,15 @@ public function with(string $key, $value): self
return $this;
}
/**
* @param string $key
* @return mixed
*/
private function get(string $key)
{
return $this->variables->get($key);
}
/**
* @return mixed|null
* @throws LeagueException
@@ -88,13 +97,15 @@ public function send()
try {
$type = $this->validate();
Application::log('INFO', 'Accessing {endpoint} at {url}', ['endpoint' => $this->get('function'), 'url' => $this->get('destination')]);
switch ($type) {
case self::FROM_API:
return $this->fromAPI();
case self::FROM_CACHE:
return $this->fromCache();
}
throw new LeagueException("ERROR (code 1): Internal Service Error. Please report this error by opening an issue on GitHub.");
} catch (GuzzleException $ex) {
throw new LeagueException("ERROR (code 2): Internal Service Error. Please report this error by opening an issue on GitHub.");
@@ -114,27 +125,27 @@ public function send()
*/
private function validate(): int
{
if (!($new = (Utils::resolveRegion($this->variables->get('region'))))) {
if (!($new = (Utils::resolveRegion($this->get('region'))))) {
throw new LeagueException('ERROR (code 6): Specified region could not be resolved.');
}
$this->variables->put('region', $new);
$this->variables->put('destination', Str::replaceFirst(
'{region}',
$this->variables->get('region'),
$this->variables->get('destination')
$this->get('region'),
$this->get('destination')
));
$this->variables->put('destination', Str::replaceArray(
'{?}',
$this->variables->get('arguments'),
$this->variables->get('destination')
$this->get('arguments'),
$this->get('destination')
));
$cache = Application::getInstance()->getCache('request');
if ($cache->has($this->variables->get('destination')) && $this->variables->get('ttl') !== false) {
if ($cache->has($this->get('destination')) && $this->get('ttl') !== false) {
return self::FROM_CACHE;
} else {
return self::FROM_API;
@@ -149,9 +160,9 @@ private function validate(): int
private function fromCache()
{
$cache = Application::getInstance()->getCache('request');
$collection = Utils::$responses[$this->variables->get('function')];
$collection = Utils::$responses[$this->get('function')];
$items = json_decode($cache->get($this->variables->get('destination')), 1);
$items = json_decode($cache->get($this->get('destination')), 1);
return $this->respond($collection, $items);
}
@@ -166,19 +177,19 @@ private function fromCache()
*/
private function fromAPI()
{
$region = $this->variables->get('region');
$function = $this->variables->get('function');
$key = $this->variables->get('key');
$region = $this->get('region');
$function = $this->get('function');
$key = $this->get('key');
if (Application::getInstance()->hittable($region, $function, $key)) {
/** @var Token $token */
$token = Application::getInstance()->getKeys()[$this->variables->get('key')];
$token = Application::getInstance()->getKeys()[$this->get('key')];
$res = (new Client())->request(
$this->variables->get('method'),
$this->variables->get('destination'),
$this->get('method'),
$this->get('destination'),
[
'body' => json_encode($this->variables->get('body')),
'body' => json_encode($this->get('body')),
'headers' => [
'Content-Type' => 'application/json',
'X-Riot-Token' => $token->getKey()
@@ -198,13 +209,17 @@ private function fromAPI()
throw new LeagueException("ERROR (code 7): API Error. Status Code: " . $res->getStatusCode(), 0, null, $items);
}
Application::log('INFO', 'Sent HTTP request to API at {url}.', [
'url' => $this->get('destination'),
]);
if (!in_array($function, Application::$rules['CACHE_NONE'])) {
$cache = Application::getInstance()->getCache('request');
if (in_array($function, Application::$rules['CACHE_PERMANENT'])) {
$cache->set($this->variables->get('destination'), json_encode($items));
$cache->set($this->get('destination'), json_encode($items));
} else {
$cache->set($this->variables->get('destination'), json_encode($items), $this->variables->get('ttl'));
$cache->set($this->get('destination'), json_encode($items), $this->get('ttl'));
}
}
@@ -213,7 +228,7 @@ private function fromAPI()
return $this->respond($collection, $items);
}
throw new LeagueException('ERROR (code 8): Rate Limit wiuld be exceeded by making this call.');
throw new LeagueException('ERROR (code 8): Rate Limit would be exceeded by making this call.');
}
/**
@@ -225,7 +240,7 @@ private function respond($collection, $items)
{
if ($collection !== false) {
$template = Utils::loadTemplate($collection);
return Utils::traverse($items, $template, $this->variables->get('region'));
return Utils::traverse($items, $template, $this->get('region'));
}
return $collection ? $items : ($items[0] ?? null);

0 comments on commit f267fac

Please sign in to comment.
You can’t perform that action at this time.