| 1: | <?php |
| 2: | namespace Worldline\Connect\Sdk\Logging; |
| 3: | |
| 4: | use Exception; |
| 5: | use UnexpectedValueException; |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | class ResourceLogger implements CommunicatorLogger |
| 13: | { |
| 14: | const DATE_FORMAT_STRING = DATE_ATOM; |
| 15: | |
| 16: | |
| 17: | |
| 18: | |
| 19: | protected $resource; |
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: | public function __construct($resource) |
| 25: | { |
| 26: | if (!is_resource($resource)) { |
| 27: | throw new UnexpectedValueException('resource expected'); |
| 28: | } |
| 29: | $this->resource = $resource; |
| 30: | } |
| 31: | |
| 32: | |
| 33: | |
| 34: | |
| 35: | |
| 36: | |
| 37: | public function log(string $message): void |
| 38: | { |
| 39: | fwrite($this->resource, $this->getDatePrefix() . $message . PHP_EOL); |
| 40: | } |
| 41: | |
| 42: | |
| 43: | |
| 44: | |
| 45: | |
| 46: | |
| 47: | |
| 48: | public function logException(string $message, Exception $exception): void |
| 49: | { |
| 50: | fwrite($this->resource, $this->getDatePrefix() . $message . PHP_EOL . $exception . PHP_EOL); |
| 51: | } |
| 52: | |
| 53: | |
| 54: | |
| 55: | |
| 56: | protected function getDatePrefix(): string |
| 57: | { |
| 58: | return date(static::DATE_FORMAT_STRING) . ' '; |
| 59: | } |
| 60: | } |
| 61: | |