Skip to content

Commit

Permalink
BaseEndpoint: Implements new helpers like in Presenter
Browse files Browse the repository at this point in the history
  • Loading branch information
janbarasek committed Jan 5, 2020
1 parent e880b05 commit f4ccd41
Showing 1 changed file with 135 additions and 0 deletions.
135 changes: 135 additions & 0 deletions src/Endpoint/BaseEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,15 @@
namespace Baraja\StructuredApi;


use Baraja\Doctrine\EntityManager;
use Nette\Application\LinkGenerator;
use Nette\Application\UI\InvalidLinkException;
use Nette\Caching\Cache;
use Nette\Caching\IStorage;
use Nette\DI\Container;
use Nette\DI\Extensions\InjectExtension;
use Nette\Security\IIdentity;
use Nette\Security\User;
use Nette\SmartObject;

abstract class BaseEndpoint
Expand Down Expand Up @@ -54,6 +61,16 @@ public function startup(): void
$this->startupCheck = true;
}

/**
* Get current endpoint name.
*
* @return string
*/
final public function getName(): string
{
return preg_replace('/^(?:.*\\\\)?([A-Z0-9][a-z0-9]+)Endpoint$/', '$1', get_class($this));
}

/**
* @param mixed[] $haystack
*/
Expand All @@ -75,6 +92,21 @@ final public function sendError(string $message, int $code = 500): void
]);
}

/**
* @param mixed[] $data
* @param string $message
* @param int $code
*/
final public function sendOk(array $data = [], string $message, int $code = 200): void
{
$this->sendJson([
'state' => 'ok',
'message' => $message,
'code' => $code,
'data' => $data,
]);
}

/**
* @throws RuntimeStructuredApiException
*/
Expand All @@ -90,4 +122,107 @@ final public function saveState(): void
// TODO: Implement me!
}

/**
* @return EntityManager
*/
final public function em(): EntityManager
{
static $em;

if ($em === null) {
$em = $this->container->getByType(EntityManager::class);
}

return $em;
}

/**
* @return User
*/
final public function getUser(): User
{
static $user;

if ($user === null) {
$user = $this->container->getByType(User::class);
}

return $user;
}

/**
* @return bool
*/
final public function isUserLoggedIn(): bool
{
try {
return $this->getUser()->isLoggedIn();
} catch (\Throwable $e) {
return false;
}
}

/**
* @return IIdentity|null
*/
final public function getUserEntity(): ?IIdentity
{
return $this->getUser()->getIdentity();
}

/**
* @param string $dest
* @param mixed[] $params
* @return string
* @throws InvalidLinkException
*/
final public function link(string $dest, array $params = []): string
{
static $linkGenerator;

if ($linkGenerator === null) {
$linkGenerator = $this->container->getByType(LinkGenerator::class);
}

return $linkGenerator->link(trim($dest, ':'), $params);
}

/**
* @return Cache
*/
final public function getCache(?string $namespace = null): Cache
{
static $storage;
static $cache = [];
$name = 'api---' . strtolower($namespace ?? $this->getName());

if ($storage === null) {
$storage = $this->container->getByType(IStorage::class);
}

if (isset($cache[$name]) === false) {
$cache[$name] = new Cache($storage, $name);
}

return $cache[$name];
}

/**
* @return mixed
*/
final public function getParameters(): array
{
return $this->container->getParameters();
}

/**
* @param string $key
* @param mixed|null $defaultValue
* @return mixed|null
*/
final public function getParameter(string $key, $defaultValue = null)
{
return $this->container->getParameters()[$key] ?? $defaultValue;
}

}

0 comments on commit f4ccd41

Please sign in to comment.