Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Homework #1

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
charset = utf-8
indent_style = space
indent_size = 4
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
.idea
application.log
/vendor/
composer.lock
40 changes: 28 additions & 12 deletions Logger.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,39 @@
<?php

declare(strict_types=1);

require 'vendor/autoload.php';

use Homework\Logger\AbstractHandler;
use Homework\Logger\HandlerFactory;
use Homework\Logger\Type;

class Logger
{
public static function get()
{
return new Logger();
}
private static $instance = null;

public function logError($message)
public static function get(): AbstractHandler
{
$logFile = fopen('application.log', 'w');
fwrite($logFile, 'ERROR: ' . $message);
fclose($logFile);
if (self::$instance == null) {
self::$instance = HandlerFactory::get(self::getHandlerType());
}

return self::$instance;
}

public function logSuccess($msg)
/**
* Get handler type.
*
* @return Type
*/
private static function getHandlerType(): Type
{
$logFile = fopen('application.log', 'a');
fwrite($logFile, 'SUCCESS: ' . $msg);
if (count($_SERVER['argv']) > 1) {
if ($_SERVER['argv'][1] === '-c' || $_SERVER['argv'][1] === '--console') {
return Type::CONSOLE;
}
}

return Type::FILE;
}
}
}
35 changes: 35 additions & 0 deletions Logger/AbstractHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Homework\Logger;

/**
* Abstract class for handlers.
*/
abstract class AbstractHandler
{
/**
* Add error entity to log.
*
* @param string $message
*
* @return boolean
*/
public function logError(string $message): bool
{
return $this->log(Status::ERROR, $message);
}

/**
* Add success entity to log.
*
* @param string $message
*
* @return boolean
*/
public function logSuccess(string $message): bool
{
return $this->log(Status::SUCCESS, $message);
}
}
34 changes: 34 additions & 0 deletions Logger/ConsoleHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Homework\Logger;

/**
* Console logger handler.
*/
class ConsoleHandler extends AbstractHandler implements HandlerInterface
{
use ValidationTrait;

/**
* Add log entity to console.
*
* @param Status $type
* @param string $message
*
* @return boolean
*/
public function log(Status $type, string $message): bool
{
if (!$this->validate($message)) {
return false;
}

$file = fopen('php://stdout', 'a');

fwrite($file, sprintf("%s: %s\n", $type->getLabel(), $message));

return fclose($file);
}
}
68 changes: 68 additions & 0 deletions Logger/FileHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

declare(strict_types=1);

namespace Homework\Logger;

use Exception;

/**
* File logger handler.
*/
class FileHandler extends AbstractHandler implements HandlerInterface
{
use ValidationTrait;

public function __construct(private string $LOG_FILE = __DIR__ . '/../application.log')
{
}

/**
* Add log entity to file.
*
* @param Status $type
* @param string $message
*
* @return boolean
*/
public function log(Status $type, string $message): bool
{
if (!$this->createFile()) {
return false;
}

if (!$this->validate($message)) {
return false;
}

$file = fopen($this->LOG_FILE, 'a');

fwrite($file, sprintf("%s: %s\n", $type->getLabel(), $message));

return fclose($file);
}

/**
* Check if file exists or not.
*
* @return boolean
*/
private function fileExists(): bool
{
return file_exists($this->LOG_FILE) && is_file($this->LOG_FILE) && is_writable($this->LOG_FILE);
}

/**
* Create file if not exists.
*
* @return boolean
*/
private function createFile(): bool
{
if (!$this->fileExists() && !fopen($this->LOG_FILE, 'c')) {
throw new Exception('Cannot create file');
}

return $this->fileExists();
}
}
26 changes: 26 additions & 0 deletions Logger/HandlerFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Homework\Logger;

/**
* Handler factory.
*/
class HandlerFactory
{
/**
* Get handler instance.
*
* @param Type $type
*
* @return AbstractHandler
*/
public static function get(Type $type = Type::FILE): AbstractHandler
{
return match ($type) {
Type::FILE => new FileHandler(),
Type::CONSOLE => new ConsoleHandler()
};
}
}
21 changes: 21 additions & 0 deletions Logger/HandlerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Homework\Logger;

/**
* Interface for handlers.
*/
interface HandlerInterface
{
/**
* Add entity to log.
*
* @param Status $type
* @param string $message
*
* @return boolean
*/
public function log(Status $type, string $message): bool;
}
27 changes: 27 additions & 0 deletions Logger/Status.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Homework\Logger;

/**
* Log entity status.
*/
enum Status
{
case ERROR;
case SUCCESS;

/**
* Get text label for status.
*
* @return string
*/
public function getLabel(): string
{
return match ($this) {
Status::ERROR => 'Error',
Status::SUCCESS => 'Success'
};
}
}
14 changes: 14 additions & 0 deletions Logger/Type.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace Homework\Logger;

/**
* Handler types.
*/
enum Type
{
case FILE;
case CONSOLE;
}
35 changes: 35 additions & 0 deletions Logger/ValidationTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Homework\Logger;

/**
* Validation trait
*/
trait ValidationTrait
{
/**
* Validate log message.
*
* @param string $message
*
* @return boolean
*/
public function validate(string $message): bool
{
if (!is_string($message)) {
return false;
}

if (mb_check_encoding($message, 'UTF-8') !== true) {
return false;
}

if (trim($message) === '') {
return false;
}

return true;
}
}
22 changes: 22 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "catsand/homework",
"description": "Test task for Mobily",
"type": "project",
"license": "MIT",
"authors": [
{
"name": "catsAND",
"email": "[email protected]"
}
],
"autoload": {
"psr-4": {"Homework\\": "./"}
},
"require": {
"php": "^8.1",
"ext-mbstring": "*"
},
"require-dev": {
"phpunit/phpunit": "^9.5"
}
}
3 changes: 3 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
require_once 'Logger.php';


// Do not change this file


function process()
{
for ($i = 0; $i < 3; $i++) {
Expand Down
Loading