Skip to content

Commit 76fbefd

Browse files
author
Holger Lösken
committed
Add better notification support
1 parent 6e42368 commit 76fbefd

File tree

8 files changed

+212
-56
lines changed

8 files changed

+212
-56
lines changed

config/self-update.php

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,18 +92,37 @@
9292

9393
/*
9494
|--------------------------------------------------------------------------
95-
| Mail To Settings
95+
| Notifications
9696
|--------------------------------------------------------------------------
9797
|
98-
| Configure if fired events should be logged
98+
| Specify for which events you want to get notifications. Out of the box you can use 'mail'.
9999
|
100100
*/
101101

102-
'mail_to' => [
103-
'address' => env('SELF_UPDATER_MAILTO_ADDRESS', ''),
104-
'name' => env('SELF_UPDATER_MAILTO_NAME', ''),
105-
'subject_update_available' => env('SELF_UPDATER_MAILTO_UPDATE_AVAILABLE_SUBJECT', 'Update available'),
106-
'subject_update_succeeded' => env('SELF_UPDATER_MAILTO_UPDATE_SUCCEEDED_SUBJECT', 'Update succeeded'),
102+
'notifications' => [
103+
'notifications' => [
104+
\Codedge\Updater\Notifications\Notifications\UpdateSucceeded::class => ['mail'],
105+
\Codedge\Updater\Notifications\Notifications\UpdateFailed::class => ['mail'],
106+
\Codedge\Updater\Notifications\Notifications\UpdateAvailable::class => ['mail'],
107+
],
108+
109+
/*
110+
* Here you can specify the notifiable to which the notifications should be sent. The default
111+
* notifiable will use the variables specified in this config file.
112+
*/
113+
'notifiable' => \Codedge\Updater\Notifications\Notifiable::class,
114+
115+
'mail' => [
116+
'to' => [
117+
'address' => env('SELF_UPDATER_MAILTO_ADDRESS', ''),
118+
'name' => env('SELF_UPDATER_MAILTO_NAME', ''),
119+
],
120+
121+
'from' => [
122+
'address' => env('SELF_UPDATER_MAIL_FROM_ADDRESS', ''),
123+
'name' => env('SELF_UPDATER_MAIL_FROM_NAME', ''),
124+
],
125+
]
107126
],
108127

109128
/*
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Codedge\Updater\Notifications;
4+
5+
use Illuminate\Notifications\Notification;
6+
7+
abstract class BaseNotification extends Notification
8+
{
9+
public function via(): array
10+
{
11+
$notificationChannels = config('self-update.notifications.notifications.'.static::class);
12+
13+
return array_filter($notificationChannels);
14+
}
15+
}

src/Notifications/EventHandler.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Codedge\Updater\Notifications;
4+
5+
use Codedge\Updater\Events\UpdateAvailable;
6+
use Codedge\Updater\Events\UpdateFailed;
7+
use Codedge\Updater\Events\UpdateSucceeded;
8+
use Exception;
9+
use Illuminate\Contracts\Config\Repository;
10+
use Illuminate\Contracts\Events\Dispatcher;
11+
use Illuminate\Notifications\Notification;
12+
13+
final class EventHandler
14+
{
15+
/** @var Repository */
16+
protected $config;
17+
18+
public function __construct(Repository $config)
19+
{
20+
$this->config = $config;
21+
}
22+
23+
public function subscribe(Dispatcher $events)
24+
{
25+
$events->listen($this->allEventClasses(), function ($event) {
26+
$notification = $this->determineNotification($event);
27+
$notifiable = $this->determineNotifiable();
28+
$notifiable->notify($notification);
29+
});
30+
}
31+
32+
protected function determineNotifiable()
33+
{
34+
$notifiableClass = $this->config->get('self-update.notifications.notifiable');
35+
36+
return app($notifiableClass);
37+
}
38+
39+
protected function determineNotification($event): Notification
40+
{
41+
$eventName = class_basename($event);
42+
43+
$notificationClass = collect($this->config->get('self-update.notifications.notifications'))
44+
->keys()
45+
->first(function ($notificationClass) use ($eventName) {
46+
$notificationName = class_basename($notificationClass);
47+
48+
return $notificationName === $eventName;
49+
});
50+
51+
if (! $notificationClass) {
52+
throw new Exception('Notification could not be sent.');
53+
}
54+
55+
return app($notificationClass)->setEvent($event);
56+
}
57+
58+
protected function allEventClasses(): array
59+
{
60+
return [
61+
UpdateAvailable::class,
62+
UpdateSucceeded::class,
63+
UpdateFailed::class,
64+
];
65+
}
66+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Codedge\Updater\Notifications\Notifications;
4+
5+
use Codedge\Updater\Notifications\BaseNotification;
6+
use Codedge\Updater\Events\UpdateAvailable as UpdateAvailableEvent;
7+
use Illuminate\Notifications\Messages\MailMessage;
8+
9+
final class UpdateAvailable extends BaseNotification
10+
{
11+
/**
12+
* @var UpdateAvailableEvent
13+
*/
14+
protected $event;
15+
16+
public function toMail(): MailMessage
17+
{
18+
return (new MailMessage())
19+
->from(config('self-update.notifications.mail.from.address', config('mail.from.address')), config('self-update.notifications.mail.from.name', config('mail.from.name')))
20+
->subject(config('app.name') . ': Update available');
21+
}
22+
23+
public function setEvent(UpdateAvailableEvent $event)
24+
{
25+
$this->event = $event;
26+
27+
return $this;
28+
}
29+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Codedge\Updater\Notifications\Notifications;
4+
5+
use Codedge\Updater\Notifications\BaseNotification;
6+
use Codedge\Updater\Events\UpdateFailed as UpdateFailedEvent;
7+
use Illuminate\Notifications\Messages\MailMessage;
8+
9+
final class UpdateFailed extends BaseNotification
10+
{
11+
/**
12+
* @var UpdateFailedEvent
13+
*/
14+
protected $event;
15+
16+
public function toMail(): MailMessage
17+
{
18+
return (new MailMessage())
19+
->from(config('self-update.notifications.mail.from.address', config('mail.from.address')), config('self-update.notifications.mail.from.name', config('mail.from.name')))
20+
->subject(config('app.name') . ': Update failed');
21+
}
22+
23+
public function setEvent(UpdateFailedEvent $event)
24+
{
25+
$this->event = $event;
26+
27+
return $this;
28+
}
29+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Codedge\Updater\Notifications\Notifications;
4+
5+
use Codedge\Updater\Notifications\BaseNotification;
6+
use Codedge\Updater\Events\UpdateSucceeded as UpdateSucceededEvent;
7+
use Illuminate\Notifications\Messages\MailMessage;
8+
9+
final class UpdateSucceeded extends BaseNotification
10+
{
11+
/**
12+
* @var UpdateSucceededEvent
13+
*/
14+
protected $event;
15+
16+
public function toMail(): MailMessage
17+
{
18+
return (new MailMessage())
19+
->from(config('self-update.notifications.mail.from.address', config('mail.from.address')), config('self-update.notifications.mail.from.name', config('mail.from.name')))
20+
->subject(config('app.name') . ': Update succeeded');
21+
}
22+
23+
public function setEvent(UpdateSucceededEvent $event)
24+
{
25+
$this->event = $event;
26+
27+
return $this;
28+
}
29+
}

src/UpdaterServiceProvider.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Codedge\Updater\Contracts\GithubRepositoryTypeContract;
77
use Codedge\Updater\Models\Release;
88
use Codedge\Updater\Models\UpdateExecutor;
9+
use Codedge\Updater\Notifications\EventHandler;
910
use Codedge\Updater\SourceRepositoryTypes\GithubRepositoryType;
1011
use Codedge\Updater\SourceRepositoryTypes\GithubRepositoryTypes\GithubBranchType;
1112
use Codedge\Updater\SourceRepositoryTypes\GithubRepositoryTypes\GithubTagType;
@@ -60,6 +61,8 @@ public function register()
6061
{
6162
$this->mergeConfigFrom(__DIR__.'/../config/self-update.php', 'self-update');
6263

64+
$this->app['events']->subscribe(EventHandler::class);
65+
6366
$this->registerCommands();
6467
$this->registerManager();
6568
}

tests/TestCase.php

Lines changed: 15 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -39,56 +39,22 @@ abstract class TestCase extends Orchestra
3939
*/
4040
protected function getEnvironmentSetUp($app)
4141
{
42-
$app['config']->set('self-update', [
43-
'default' => 'github',
44-
'version_installed' => '',
45-
'repository_types' => [
46-
'github' => [
47-
'type' => 'github',
48-
'repository_vendor' => 'laravel',
49-
'repository_name' => 'laravel',
50-
'repository_url' => '',
51-
'download_path' => self::DOWNLOAD_PATH,
52-
'private_access_token' => '',
53-
'use_branch' => '',
54-
],
55-
'http' => [
56-
'type' => 'http',
57-
'repository_url' => 'https://github.com/invoiceninja/invoiceninja/releases',
58-
'pkg_filename_format' => env('SELF_UPDATER_PKG_FILENAME_FORMAT', 'v_VERSION_'),
59-
'download_path' => self::DOWNLOAD_PATH,
60-
'private_access_token' => '',
61-
],
42+
$app['config']->set('self-update.repository_types', [
43+
'github' => [
44+
'type' => 'github',
45+
'repository_vendor' => 'laravel',
46+
'repository_name' => 'laravel',
47+
'repository_url' => '',
48+
'download_path' => self::DOWNLOAD_PATH,
49+
'private_access_token' => '',
50+
'use_branch' => '',
6251
],
63-
'exclude_folders' => [
64-
'__MACOSX',
65-
'node_modules',
66-
'bootstrap/cache',
67-
'bower',
68-
'storage/app',
69-
'storage/framework',
70-
'storage/logs',
71-
'storage/self-update',
72-
'vendor',
73-
],
74-
'log_events' => false,
75-
'mail_to' => [
76-
'address' => '',
77-
'name' => '',
78-
],
79-
'artisan_commands' => [
80-
'pre_update' => [
81-
// 'updater:pre-update' => [
82-
// 'class' => SamplePreUpdate::class,
83-
// 'params' => []
84-
// ]
85-
],
86-
'post_update' => [
87-
// 'updater:post-update' => [
88-
// 'class' => SamplePostUpdate::class,
89-
// 'params' => []
90-
// ]
91-
],
52+
'http' => [
53+
'type' => 'http',
54+
'repository_url' => 'https://github.com/invoiceninja/invoiceninja/releases',
55+
'pkg_filename_format' => env('SELF_UPDATER_PKG_FILENAME_FORMAT', 'v_VERSION_'),
56+
'download_path' => self::DOWNLOAD_PATH,
57+
'private_access_token' => '',
9258
],
9359
]);
9460

0 commit comments

Comments
 (0)