Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze committed Nov 19, 2021
1 parent c9d8373 commit c4fb047
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 32 deletions.
42 changes: 22 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,7 @@
[![Tests](https://github.com/ohdearapp/health-check-report/actions/workflows/run-tests.yml/badge.svg?branch=main)](https://github.com/ohdearapp/health-check-report/actions/workflows/run-tests.yml)
[![Total Downloads](https://img.shields.io/packagist/dt/ohdearapp/health-check-report.svg?style=flat-square)](https://packagist.org/packages/ohdearapp/health-check-report)

---
This package can be used as to scaffold a framework agnostic package. Follow these steps to get started:

1. Press the "Use template" button at the top of this repo to create a new repo with the contents of this skeleton
2. Run "php ./configure.php" to run a script that will replace all placeholders throughout all the files
3. Have fun creating your package.
4. If you need help creating a package, consider picking up our <a href="https://laravelpackage.training">Laravel Package Training</a> video course.
---

This is where your description should go. Try and limit it to a paragraph or two. Consider adding a small example.

## Support us

[<img src="https://github-ads.s3.eu-central-1.amazonaws.com/health-check-report.jpg?t=1" width="419px" />](https://spatie.be/github-ad-click/health-check-report)

We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).
Using this package you can build up the JSON that Oh Dear expects for the health check.

## Installation

Expand All @@ -33,9 +16,28 @@ composer require ohdearapp/health-check-report

## Usage

Here's an example that shows how you can create the JSON that Oh Dear expects for the health check.

```php
$skeleton = new OhDear\HealthCheckReport();
echo $skeleton->echoPhrase('Hello, OhDear!');
$lines = [
new Line(
'name',
'message',
'ok',
['name' => 'value']
)
];

$report = new Report(
finishedAt: new DateTimeImmutable('2001-01-01 00:00:00'),
lines: $lines,
);
```

This will output this JSON:

```
{"finishedAt":"2001-01-01 00:00:00","lines":[{"name":"name","message":"message","status":"ok","meta":{"name":"value"}}]}
```

## Testing
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.0",
"pestphp/pest": "^1.20",
"spatie/pest-plugin-snapshots": "^1.1",
"spatie/ray": "^1.28"
},
"autoload": {
Expand Down
54 changes: 54 additions & 0 deletions src/Line.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace OhDear\HealthCheckReport;

class Line
{
public static function make(
string $name,
string $message = '',
string $status = '',
array $meta = [],
): self {
return new static(...func_get_args());
}

public function __construct(
protected string $name,
protected string $message = '',
protected string $status = '',
protected array $meta = [],
) {
}

public function message(string $message): self
{
$this->message = $message;

return $this;
}

public function status(string $status): self
{
$this->status = $status;

return $this;
}

public function meta(array $meta): self
{
$this->meta = $meta;

return $this;
}

public function toArray(): array
{
return [
'name' => $this->name,
'message' => $this->message,
'status' => $this->status,
'meta' => $this->meta,
];
}
}
40 changes: 40 additions & 0 deletions src/Report.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace OhDear\HealthCheckReport;

use DateTime;
use DateTimeInterface;

class Report
{
protected DateTimeInterface $finishedAt;

/** @var array<int, Line> */
protected array $lines;

/**
* @param \DateTimeInterface|null $finishedAt
* @param array<int, Line> $lines
*/
public function __construct(DateTimeInterface $finishedAt = null, array $lines = [])
{
$this->finishedAt = $finishedAt ?? new DateTime();

$this->lines = $lines;
}

public function addLine(Line $lines)
{
$this->lines[] = $lines;
}

public function toJson(): string
{
$lines = array_map(fn (Line $line) => $line->toArray(), $this->lines);

return json_encode([
'finishedAt' => $this->finishedAt->format('Y-m-d H:i:s'),
'lines' => $lines,
]);
}
}
7 changes: 0 additions & 7 deletions src/SkeletonClass.php

This file was deleted.

5 changes: 0 additions & 5 deletions tests/ExampleTest.php

This file was deleted.

23 changes: 23 additions & 0 deletions tests/ReportTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

use OhDear\HealthCheckReport\Line;
use OhDear\HealthCheckReport\Report;
use function Spatie\Snapshots\assertMatchesSnapshot;

it('can create create report', function () {
$lines = [
new Line(
'name',
'message',
'ok',
['name' => 'value']
)
];

$report = new Report(
finishedAt: new DateTimeImmutable('2001-01-01 00:00:00'),
lines: $lines,
);

assertMatchesSnapshot($report->toJson());
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"finishedAt":"2001-01-01 00:00:00","lines":[{"name":"name","message":"message","status":"ok","meta":{"name":"value"}}]}

0 comments on commit c4fb047

Please sign in to comment.