Skip to content

Commit 5e5ac90

Browse files
committed
wip
1 parent a56b506 commit 5e5ac90

File tree

6 files changed

+158
-37
lines changed

6 files changed

+158
-37
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ LaravelDeveloper::exceptionToDevSlack(
7373
);
7474
```
7575

76+
Or using helper:
77+
78+
```php
79+
slack(new Exception('wew'));
80+
```
81+
7682
### Send message to slack
7783

7884
Use this to send any message to your dev slack:
@@ -83,6 +89,12 @@ use Binarcode\LaravelDeveloper\LaravelDeveloper;
8389
LaravelDeveloper::messageToDevSlack('Hey, we have troubles ;-)');
8490
```
8591

92+
Or using helper:
93+
94+
```php
95+
slack('Hey there!');
96+
```
97+
8698
### Send anything to slack
8799

88100
Obviously, you can send any kind of message to the slack channel. The `toDevSlack` method accept an instance of `DevNotificationDto`:
@@ -112,6 +124,12 @@ try {
112124

113125
Under the hood, the package will store an entry in the `exception_logs` of your database with the exception.
114126

127+
Or using helper (but this will also send a slack notification):
128+
129+
```php
130+
slack($e)->persist()
131+
```
132+
115133
### Slack notification
116134

117135
If you want to send the notification to the slack webhook, just call the

src/Dtos/DevNotificationDto.php

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function __construct(array $data = [])
3636

3737
public function hasAttachment(): bool
3838
{
39-
return ! is_null($this->attachment_title) || ! is_null($this->attachment_content);
39+
return !is_null($this->attachment_title) || !is_null($this->attachment_content);
4040
}
4141

4242
public function getAttachmentTitle(): ?string
@@ -51,25 +51,48 @@ public function getAttachmentContent(): ?string
5151

5252
public static function makeWithMessage(string $message): self
5353
{
54-
return new static([
55-
'message' => $message,
56-
]);
54+
return tap(new static, fn(self $dto) => $dto->setMessage($message));
5755
}
5856

5957
public static function makeFromException(Throwable $t): self
6058
{
61-
return new static([
62-
'message' => $t->getMessage(),
63-
]);
59+
return (new static())->setException($t);
60+
}
61+
62+
public function setException(Throwable $t): self
63+
{
64+
$this->message = $t->getMessage();
65+
66+
return $this;
67+
}
68+
69+
public function setMessage(string $message): self
70+
{
71+
$this->message = $message;
72+
73+
return $this;
6474
}
6575

6676
public static function makeFromExceptionLog(ExceptionLog $log): self
6777
{
68-
return new static([
69-
'message' => $log->name,
70-
'attachment_title' => $log->identifier,
71-
'attachment_link' => $log->getUrl(),
72-
]);
78+
return tap(new static, fn(self $dto) => $dto
79+
->setMessage($log->name)
80+
->setTitle($log->identifier)
81+
->setAttachmentLink($log->getUrl()));
82+
}
83+
84+
protected function setTitle(string $title): self
85+
{
86+
$this->attachment_title = $title;
87+
88+
return $this;
89+
}
90+
91+
protected function setAttachmentLink($title = null): self
92+
{
93+
$this->attachment_link = $title;
94+
95+
return $this;
7396
}
7497

7598
public function jsonSerialize()

src/LaravelDeveloper.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,16 @@
55
use Binarcode\LaravelDeveloper\Dtos\DevNotificationDto;
66
use Binarcode\LaravelDeveloper\Models\ExceptionLog;
77
use Binarcode\LaravelDeveloper\Notifications\DevNotification;
8+
use Binarcode\LaravelDeveloper\Notifications\Slack;
89
use Illuminate\Notifications\Notification;
910
use Illuminate\Support\Facades\Notification as NotificationFacade;
1011
use Throwable;
1112

1213
class LaravelDeveloper
1314
{
14-
/**
15-
* @var callable
16-
*/
17-
protected static $notifyUsing;
18-
1915
public static function notifyDev(Notification $notification)
2016
{
21-
if (is_callable($cb = static::$notifyUsing)) {
17+
if (is_callable($cb = Slack::$notifyUsing)) {
2218
return call_user_func($cb, $notification);
2319
}
2420

@@ -82,6 +78,6 @@ public function routeNotificationForSlack($notification)
8278

8379
public static function notifyUsing(?callable $notificator)
8480
{
85-
static::$notifyUsing = $notificator;
81+
Slack::$notifyUsing = $notificator;
8682
}
8783
}

src/Notifications/Slack.php

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
namespace Binarcode\LaravelDeveloper\Notifications;
4+
5+
use Binarcode\LaravelDeveloper\Dtos\DevNotificationDto;
6+
use Binarcode\LaravelDeveloper\Models\ExceptionLog;
7+
use Illuminate\Support\Collection;
8+
use Illuminate\Support\Facades\Notification as NotificationFacade;
9+
use Throwable;
10+
11+
class Slack
12+
{
13+
/**
14+
* @var callable
15+
*/
16+
public static $notifyUsing;
17+
18+
protected Collection $queue;
19+
20+
protected bool $persist = false;
21+
22+
public function __construct($args = null)
23+
{
24+
$this->queue = collect($args)->flatten();
25+
}
26+
27+
public static function make(...$args)
28+
{
29+
return new static($args);
30+
}
31+
32+
public function __destruct()
33+
{
34+
$this->queue->each(function ($item) {
35+
$this->send($item);
36+
});
37+
}
38+
39+
public function persist($persist = true): self
40+
{
41+
$this->persist = $persist;
42+
43+
return $this;
44+
}
45+
46+
private function send($item)
47+
{
48+
/**
49+
* @var string $class
50+
*/
51+
$class = config('developer.notification', DevNotification::class);
52+
53+
$dto = new DevNotificationDto;
54+
55+
if ($item instanceof Throwable) {
56+
if ($this->persist) {
57+
ExceptionLog::makeFromException($item)->save();
58+
}
59+
60+
$dto->setException($item);
61+
}
62+
63+
if (is_string($item)) {
64+
$dto->setMessage($item);
65+
}
66+
67+
if ($item instanceof ExceptionLog) {
68+
if ($this->persist) {
69+
$item->save();
70+
}
71+
72+
$dto = $dto::makeFromExceptionLog($item);
73+
}
74+
75+
$notification = new $class($dto);
76+
77+
if (is_callable($cb = static::$notifyUsing)) {
78+
return call_user_func($cb, $notification);
79+
}
80+
81+
NotificationFacade::route('slack', config('developer.slack_dev_hook'))->notify(
82+
$notification
83+
);
84+
85+
return $this;
86+
}
87+
88+
public static function notifyUsing(?callable $notificator)
89+
{
90+
Slack::$notifyUsing = $notificator;
91+
}
92+
}

src/helpers.php

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use Binarcode\LaravelDeveloper\LaravelDeveloper;
4+
use Binarcode\LaravelDeveloper\Notifications\Slack;
45
use Binarcode\LaravelDeveloper\Profiling\ServerMemory;
56
use Binarcode\LaravelDeveloper\Profiling\ServerTiming;
67

@@ -43,18 +44,6 @@ function measure_timing($callable = null, string $key = 'action')
4344
if (! function_exists('slack')) {
4445
function slack(...$args)
4546
{
46-
$instance = new LaravelDeveloper;
47-
48-
collect($args)->each(function ($item) use ($instance) {
49-
if (is_string($item)) {
50-
$instance::messageToDevSlack($item);
51-
}
52-
53-
if ($item instanceof Throwable) {
54-
$instance::exceptionToDevSlack($item);
55-
}
56-
});
57-
58-
return $instance;
47+
return Slack::make($args);
5948
}
6049
}

tests/Helpers/SlackHelperTest.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Binarcode\LaravelDeveloper\LaravelDeveloper;
66
use Binarcode\LaravelDeveloper\Notifications\DevNotification;
7+
use Binarcode\LaravelDeveloper\Notifications\Slack;
78
use Binarcode\LaravelDeveloper\Tests\TestCase;
89
use Exception;
910
use Illuminate\Notifications\AnonymousNotifiable;
@@ -15,16 +16,16 @@ public function test_slack_helper_returns_laravel_developer_instance()
1516
{
1617
Notification::fake();
1718

18-
$this->assertInstanceOf(LaravelDeveloper::class, slack());
19-
$this->assertInstanceOf(LaravelDeveloper::class, slack('message'));
20-
$this->assertInstanceOf(LaravelDeveloper::class, slack(new Exception()));
19+
$this->assertInstanceOf(Slack::class, slack());
20+
$this->assertInstanceOf(Slack::class, slack('message'));
21+
$this->assertInstanceOf(Slack::class, slack(new Exception()));
2122
}
2223

2324
public function test_slack_helper_can_send_message_to_slack()
2425
{
2526
Notification::fake();
2627

27-
$this->assertInstanceOf(LaravelDeveloper::class, slack('message'));
28+
$this->assertInstanceOf(Slack::class, slack('message'));
2829

2930
Notification::assertSentTo(new AnonymousNotifiable, DevNotification::class);
3031
}
@@ -33,7 +34,9 @@ public function test_slack_helper_can_send_throwable_to_slack()
3334
{
3435
Notification::fake();
3536

36-
$this->assertInstanceOf(LaravelDeveloper::class, slack(new Exception()));
37+
$this->assertInstanceOf(Slack::class, slack(new Exception('wrong'))->persist());
38+
39+
$this->assertDatabaseCount('exception_logs', 1);
3740

3841
Notification::assertSentTo(new AnonymousNotifiable, DevNotification::class);
3942
}

0 commit comments

Comments
 (0)