Skip to content

Commit

Permalink
Improve code sustainability.
Browse files Browse the repository at this point in the history
  • Loading branch information
janbarasek committed Jul 13, 2020
1 parent 1e12e5e commit 4b0a6bf
Show file tree
Hide file tree
Showing 12 changed files with 10 additions and 117 deletions.
2 changes: 1 addition & 1 deletion src/ApiExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,4 @@ public function afterCompile(ClassType $class): void
[$application->getName()]
);
}
}
}
12 changes: 2 additions & 10 deletions src/ApiManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function __construct(Container $container, Request $request, Response $re
*/
public function run(string $path, ?array $params = [], ?string $method = null, bool $throw = false): void
{
$params = array_merge($this->safeGetParams($path), $this->getBodyParams($method = $method ?: $this->getMethod()), $params ?? []);
$params = array_merge($this->safeGetParams($path), $this->getBodyParams($method = $method ?: $this->getHttpMethod()), $params ?? []);

if (preg_match('/^api\/v([^\/]+)\/?(.*?)$/', $path, $pathParser)) {
if (($version = (int) $pathParser[1]) < 1 || $version > 999 || !preg_match('#^[+-]?\d+$#D', $pathParser[1])) {
Expand Down Expand Up @@ -143,9 +143,6 @@ public function get(string $path, ?array $params = [], ?string $method = null):
}


/**
* @return Convention
*/
public function getConvention(): Convention
{
return $this->convention;
Expand Down Expand Up @@ -311,12 +308,7 @@ private function invokeActionMethod(Endpoint $endpoint, string $action, string $
}


/**
* Return current HTTP method.
*
* @return string
*/
private function getMethod(): string
private function getHttpMethod(): string
{
if (($method = $_SERVER['REQUEST_METHOD'] ?? 'GET') === 'POST'
&& preg_match('#^[A-Z]+$#D', $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'] ?? '')
Expand Down
33 changes: 1 addition & 32 deletions src/Endpoint/BaseEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ abstract class BaseEndpoint implements Endpoint
private $startupCheck = false;


/**
* @return string
*/
public function __toString(): string
{
return get_class($this);
Expand Down Expand Up @@ -123,10 +120,6 @@ final public function sendJson(array $haystack, int $httpCode = 200): void
}


/**
* @param string $message
* @param int|null $code
*/
final public function sendError(string $message, ?int $code = null): void
{
$this->sendJson([
Expand Down Expand Up @@ -190,11 +183,6 @@ final public function flashMessage(string $message, string $type = 'info'): void
}


/**
* @param string $key
* @param string|null $message
* @param int|null $code
*/
final public function validateDataKey(string $key, ?string $message = null, ?int $code = null): void
{
if (array_key_exists($key, $this->data) === false) {
Expand Down Expand Up @@ -277,9 +265,6 @@ final public function saveState(): void
}


/**
* @return User
*/
final public function getUser(): User
{
static $user;
Expand All @@ -292,9 +277,6 @@ final public function getUser(): User
}


/**
* @return bool
*/
final public function isUserLoggedIn(): bool
{
try {
Expand All @@ -305,18 +287,12 @@ final public function isUserLoggedIn(): bool
}


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


/**
* @return IAuthorizator
*/
final public function getAuthorizator(): IAuthorizator
{
return $this->getUser()->getAuthorizator();
Expand Down Expand Up @@ -359,10 +335,6 @@ final public function linkSafe(string $dest, array $params = []): ?string
}


/**
* @param string|null $namespace
* @return Cache
*/
final public function getCache(?string $namespace = null): Cache
{
static $storage;
Expand All @@ -382,9 +354,6 @@ final public function getCache(?string $namespace = null): Cache
}


/**
* @return ITranslator
*/
final public function getTranslator(): ITranslator
{
static $translator;
Expand All @@ -410,7 +379,7 @@ final public function translate($message, ...$parameters): string


/**
* @return array<mixed>
* @return mixed[]
*/
final public function getParameters(): array
{
Expand Down
2 changes: 1 addition & 1 deletion src/Endpoint/Endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ public function startup(): void;
public function startupCheck(): void;

public function saveState(): void;
}
}
10 changes: 4 additions & 6 deletions src/Endpoint/PingEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,27 @@
namespace Baraja\StructuredApi;


use Nette\Utils\DateTime;

final class PingEndpoint extends BaseEndpoint
{
public function actionDefault(): void
{
$this->sendJson([
'result' => 'PONG',
'ip' => $this->getIp(),
'datetime' => date('Y-m-d H:i:s'),
'datetime' => DateTime::from('now'),
]);
}


/**
* @return string
*/
private function getIp(): string
{
$ip = null;

if (isset($_SERVER['REMOTE_ADDR'])) {
$ip = filter_var($_SERVER['REMOTE_ADDR'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
}

return is_string($ip) ? $ip : '127.0.0.1';
}
}
}
24 changes: 0 additions & 24 deletions src/Entity/Convention.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,12 @@ final class Convention
private $keysToHide = ['password', 'passwd', 'pass', 'pwd', 'creditcard', 'credit card', 'cc', 'pin'];


/**
* @return string
*/
public function getDateTimeFormat(): string
{
return $this->dateTimeFormat;
}


/**
* @param string $dateTimeFormat
*/
public function setDateTimeFormat(string $dateTimeFormat): void
{
if ($dateTimeFormat === '') {
Expand All @@ -49,18 +43,12 @@ public function setDateTimeFormat(string $dateTimeFormat): void
}


/**
* @return int
*/
public function getDefaultErrorCode(): int
{
return $this->defaultErrorCode;
}


/**
* @param int $code
*/
public function setDefaultErrorCode(int $code): void
{
if ($code < 100 || $code > 999) {
Expand All @@ -71,18 +59,12 @@ public function setDefaultErrorCode(int $code): void
}


/**
* @return int
*/
public function getDefaultOkCode(): int
{
return $this->defaultOkCode;
}


/**
* @param int $code
*/
public function setDefaultOkCode(int $code): void
{
if ($code < 100 || $code > 999) {
Expand All @@ -93,18 +75,12 @@ public function setDefaultOkCode(int $code): void
}


/**
* @return bool
*/
public function isRewriteTooStringMethod(): bool
{
return $this->rewriteTooStringMethod;
}


/**
* @param bool $rewriteTooStringMethod
*/
public function setRewriteTooStringMethod(bool $rewriteTooStringMethod): void
{
$this->rewriteTooStringMethod = $rewriteTooStringMethod;
Expand Down
3 changes: 0 additions & 3 deletions src/Entity/ItemsListItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ public function __construct(string $id, array $data = [])
}


/**
* @return string
*/
public function getId(): string
{
return $this->id;
Expand Down
9 changes: 0 additions & 9 deletions src/Entity/StatusCount.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,18 @@ public function __construct(string $key, int $count, ?string $label = null)
}


/**
* @return string
*/
public function getKey(): string
{
return $this->key;
}


/**
* @return string
*/
public function getLabel(): string
{
return $this->label;
}


/**
* @return int
*/
public function getCount(): int
{
return $this->count;
Expand Down
11 changes: 1 addition & 10 deletions src/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@

final class Helpers
{
/**
* @throws \Error
*/
/** @throws \Error */
public function __construct()
{
throw new \Error('Class ' . get_class($this) . ' is static and cannot be instantiated.');
Expand Down Expand Up @@ -49,9 +47,6 @@ public static function getCurrentUrl(): ?string
}


/**
* @return string|null
*/
public static function getBaseUrl(): ?string
{
static $return;
Expand All @@ -76,10 +71,6 @@ public static function getBaseUrl(): ?string
}


/**
* @param string $name
* @return string
*/
public static function formatApiName(string $name): string
{
return (string) preg_replace_callback('/-([a-z])/', function (array $match): string {
Expand Down
9 changes: 0 additions & 9 deletions src/Response/BaseResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,12 @@ final public function __construct(Convention $convention, array $haystack, int $
}


/**
* @return string
*/
public function getContentType(): string
{
return 'text/plain';
}


/**
* @return string
*/
public function __toString(): string
{
return '';
Expand All @@ -75,9 +69,6 @@ final public function getArray(): array
}


/**
* @return int
*/
final public function getHttpCode(): int
{
return $this->httpCode;
Expand Down
6 changes: 0 additions & 6 deletions src/Response/JsonResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,12 @@

final class JsonResponse extends BaseResponse
{
/**
* @return string
*/
public function getContentType(): string
{
return 'application/json';
}


/**
* @return string
*/
public function __toString(): string
{
try {
Expand Down
6 changes: 0 additions & 6 deletions src/Response/ThrowResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,13 @@ final class ThrowResponse extends \RuntimeException
private $response;


/**
* @param BaseResponse $response
*/
public function __construct(BaseResponse $response)
{
parent::__construct('');
$this->response = $response;
}


/**
* @return BaseResponse
*/
public function getResponse(): BaseResponse
{
return $this->response;
Expand Down

0 comments on commit 4b0a6bf

Please sign in to comment.