Skip to content

Commit

Permalink
Updated tests. Added log channel and log level.
Browse files Browse the repository at this point in the history
  • Loading branch information
laurentmuller committed Jun 5, 2024
1 parent 8ce73b6 commit af1b05c
Show file tree
Hide file tree
Showing 47 changed files with 1,534 additions and 204 deletions.
18 changes: 9 additions & 9 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 15 additions & 14 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,22 @@
->bind('Twig\Extra\Markdown\MarkdownInterface $markdown', service('twig.markdown.default'));

// make classes in src available to be used as services
$services->load('App\\', __DIR__ . '/../src/*')
$path = __DIR__ . '/../src/';
$services->load('App\\', $path . '*')
->exclude([
__DIR__ . '/../src/Kernel.php',
__DIR__ . '/../src/Calendar',
__DIR__ . '/../src/Entity',
__DIR__ . '/../src/Enums',
__DIR__ . '/../src/Faker',
__DIR__ . '/../src/Migrations',
__DIR__ . '/../src/Model',
__DIR__ . '/../src/Pdf',
__DIR__ . '/../src/Report',
__DIR__ . '/../src/Spreadsheet',
__DIR__ . '/../src/Traits',
__DIR__ . '/../src/Util',
__DIR__ . '/../src/Word',
$path . 'Kernel.php',
$path . 'Calendar',
$path . 'Entity',
$path . 'Enums',
$path . 'Faker',
$path . 'Migrations',
$path . 'Model',
$path . 'Pdf',
$path . 'Report',
$path . 'Spreadsheet',
$path . 'Traits',
$path . 'Util',
$path . 'Word',
]);

// custom line and date formatter for monolog
Expand Down
36 changes: 21 additions & 15 deletions src/Database/AbstractDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function __construct(protected string $filename, bool $readonly = false,
*/
public function __toString(): string
{
return $this->filename;
return $this->getFilename();
}

/**
Expand All @@ -97,6 +97,12 @@ public function beginTransaction(): bool
return false;
}

/**
* Closes the database connection.
*
* All opened statements are also closed.
* If a transaction is active, then it is canceled (rollback).
*/
public function close(): bool
{
// close statements
Expand Down Expand Up @@ -251,15 +257,17 @@ abstract protected function createSchema(): void;
*/
protected function executeAndFetch(\SQLite3Stmt $stmt, int $mode = \SQLITE3_ASSOC): array
{
$rows = [];
$result = $stmt->execute();
if ($result instanceof \SQLite3Result) {
while ($row = $result->fetchArray($mode)) {
$rows[] = $row;
}
$result->finalize();
if (!$result instanceof \SQLite3Result) {
return [];
}

$rows = [];
while ($row = $result->fetchArray($mode)) {
$rows[] = $row;
}
$result->finalize();

return $rows;
}

Expand All @@ -276,18 +284,16 @@ protected function executeAndFetch(\SQLite3Stmt $stmt, int $mode = \SQLITE3_ASSO
*/
protected function getStatement(string $query): ?\SQLite3Stmt
{
if (!isset($this->statements[$query])) {
$statement = $this->prepare($query);
if (false !== $statement) {
$this->statements[$query] = $statement;

return $statement;
}
if (isset($this->statements[$query])) {
return $this->statements[$query];
}

$statement = $this->prepare($query);
if (false === $statement) {
return null;
}

return $this->statements[$query];
return $this->statements[$query] = $statement;
}

/**
Expand Down
7 changes: 6 additions & 1 deletion src/Database/OpenWeatherDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* latitude: float,
* longitude: float}
*/
class OpenWeatherDatabase extends AbstractDatabase
class OpenWeatherDatabase extends AbstractDatabase implements \Countable
{
/**
* SQL statement to create the city table.
Expand Down Expand Up @@ -84,6 +84,11 @@ class OpenWeatherDatabase extends AbstractDatabase
LIMIT :limit
SQL;

public function count(): int
{
return $this->getRecordsCount('city');
}

/**
* Delete all cities.
*
Expand Down
114 changes: 6 additions & 108 deletions src/Entity/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@
namespace App\Entity;

use App\Interfaces\ComparableInterface;
use App\Traits\LogChannelTrait;
use App\Traits\LogLevelTrait;
use App\Utils\FormatUtils;
use App\Utils\StringUtils;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\SqlFormatter\SqlFormatter;
use Psr\Log\LogLevel;
use Symfony\Component\Validator\Constraints as Assert;

/**
Expand All @@ -28,34 +29,23 @@
*/
class Log extends AbstractEntity implements ComparableInterface
{
use LogChannelTrait;
use LogLevelTrait;

/**
* The user extra field name.
*/
final public const USER_FIELD = 'user';

/**
* The long application channel name.
*/
private const APP_CHANNEL_LONG = 'application';

/**
* The short application channel name.
*/
private const APP_CHANNEL_SHORT = 'app';

/**
* The doctrine channel name.
*/
private const DOCTRINE_CHANNEL = 'doctrine';

#[Assert\NotBlank]
#[Assert\Length(max: 50)]
#[ORM\Column(length: 50)]
private string $channel = self::APP_CHANNEL_LONG;

#[ORM\Column(nullable: true)]
private ?array $context = null;

#[Assert\NotNull]
#[ORM\Column(type: Types::DATETIME_IMMUTABLE)]
private \DateTimeInterface $createdAt;

Expand All @@ -67,11 +57,6 @@ class Log extends AbstractEntity implements ComparableInterface

private ?string $formattedDate = null;

#[Assert\NotBlank]
#[Assert\Length(max: 50)]
#[ORM\Column(length: 50)]
private string $level = LogLevel::INFO;

#[ORM\Column(type: Types::TEXT)]
private string $message = '';

Expand Down Expand Up @@ -111,32 +96,6 @@ public function formatMessage(SqlFormatter $formatter): string
return $message;
}

public function getChannel(bool $capitalize = false): string
{
return $capitalize ? StringUtils::capitalize($this->channel) : $this->channel;
}

/**
* Gets the channel's icon.
*
* @psalm-api
*/
public function getChannelIcon(): string
{
return match ($this->channel) {
'application' => 'fa-fw fa-solid fa-laptop-code',
'cache' => 'fa-fw fa-solid fa-hard-drive',
'console' => 'fa-fw fa-regular fa-keyboard',
'doctrine' => 'fa-fw fa-solid fa-database',
'mailer' => 'fa-fw fa-regular fa-envelope',
'php' => 'fa-fw fa-solid fa-code',
'request' => 'fa-fw fa-solid fa-code-pull-request',
'security' => 'fa-fw fa-solid fa-key',
'deprecation' => 'fa-solid fa-bug',
default => 'fa-fw fa-solid fa-file',
};
}

public function getContext(): ?array
{
return $this->context;
Expand Down Expand Up @@ -166,42 +125,6 @@ public function getFormattedDate(): string
return $this->formattedDate;
}

public function getLevel(bool $capitalize = false): string
{
return $capitalize ? StringUtils::capitalize($this->level) : $this->level;
}

/**
* @psalm-api
*/
public function getLevelColor(): string
{
return match ($this->level) {
LogLevel::ALERT,
LogLevel::CRITICAL,
LogLevel::EMERGENCY,
LogLevel::ERROR => 'danger',
LogLevel::WARNING => 'warning',
LogLevel::DEBUG => 'secondary',
default => 'info'
};
}

/**
* @psalm-api
*/
public function getLevelIcon(): string
{
return match ($this->level) {
LogLevel::ALERT,
LogLevel::CRITICAL,
LogLevel::EMERGENCY,
LogLevel::ERROR => 'fa-fw fa-solid fa-circle-exclamation',
LogLevel::WARNING => 'fa-fw fa-solid fa-triangle-exclamation',
default => 'fa-fw fa-solid fa-circle-info',
};
}

public function getMessage(): string
{
return $this->message;
Expand Down Expand Up @@ -233,24 +156,6 @@ public static function instance(?int $id = null): self
return $log;
}

public function isChannel(): bool
{
return StringUtils::isString($this->channel);
}

public function isLevel(): bool
{
return StringUtils::isString($this->level);
}

public function setChannel(string $channel): self
{
$channel = \strtolower($channel);
$this->channel = self::APP_CHANNEL_SHORT === $channel ? self::APP_CHANNEL_LONG : $channel;

return $this;
}

public function setContext(?array $context): self
{
$this->context = $context;
Expand All @@ -275,13 +180,6 @@ public function setExtra(?array $extra): self
return $this;
}

public function setLevel(string $level): self
{
$this->level = \strtolower($level);

return $this;
}

public function setMessage(string $message): self
{
$this->message = \trim($message);
Expand Down
Loading

0 comments on commit af1b05c

Please sign in to comment.