Skip to content

Commit b4b4e40

Browse files
committed
feat: add the database notification accessor
1 parent 6101cff commit b4b4e40

9 files changed

Lines changed: 85 additions & 10 deletions

File tree

docker-compose.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,14 @@ networks:
1515
services:
1616
mysql:
1717
container_name: bowphp_mysql
18-
image: mysql/mysql-server:8.0
18+
image: mysql:8.3.0
1919
restart: unless-stopped
2020
ports:
2121
- "3306:3306"
2222
environment:
2323
MYSQL_DATABASE: test_db
2424
MYSQL_ROOT_PASSWORD: password
25-
MYSQL_ROOT_HOST: "%"
26-
MYSQL_ALLOW_EMPTY_PASSWORD: "no"
25+
MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
2726
command: --default-authentication-plugin=mysql_native_password
2827
volumes:
2928
- mysql_data:/var/lib/mysql

src/Database/Barry/Model.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,9 +279,9 @@ public static function latest(): ?Model
279279
/**
280280
* Get first rows
281281
*
282-
* @return array|object
282+
* @return array|object|null
283283
*/
284-
public static function first(): array|object
284+
public static function first(): array|object|null
285285
{
286286
return static::query()->first();
287287
}
@@ -451,7 +451,6 @@ public static function create(array $data): Model
451451

452452
// Override the olds model attributes
453453
$model->setAttributes($data);
454-
$model->persist();
455454

456455
return $model;
457456
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Bow\Database\Notification;
4+
5+
use Bow\Database\Barry\Model;
6+
use Bow\Database\Database;
7+
8+
class DatabaseNotification extends Model
9+
{
10+
/**
11+
* Cast data as json
12+
*
13+
* @var array|string[]
14+
*/
15+
protected array $casts = [
16+
'data' => 'array',
17+
];
18+
19+
public function __construct(array $attributes = [])
20+
{
21+
parent::__construct($attributes);
22+
23+
$this->table = config('notification.table') ?: 'notifications';
24+
}
25+
26+
/**
27+
* Mark notification as read
28+
*
29+
* @return bool|int
30+
*/
31+
public function markAsRead(): bool|int
32+
{
33+
return $this->update(['read_at' => app_now()]);
34+
}
35+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Bow\Database\Notification;
4+
5+
use Bow\Database\Exception;
6+
use Bow\Database\Exception\ConnectionException;
7+
8+
trait WithNotification
9+
{
10+
/**
11+
* @throws ConnectionException|Exception\QueryBuilderException
12+
*/
13+
public function notifications()
14+
{
15+
return (new DatabaseNotification())
16+
->where('concern_id', $this->getKeyValue())
17+
->where('concern_type', get_class($this));
18+
}
19+
20+
/**
21+
* @throws ConnectionException|Exception\QueryBuilderException
22+
*/
23+
public function markAsRead(string $notification_id)
24+
{
25+
return $this->notifications()->where('id', $notification_id)->update(['read_at' => app_now()]);
26+
}
27+
28+
/**
29+
* @throws ConnectionException|Exception\QueryBuilderException
30+
*/
31+
public function markAllAsRead()
32+
{
33+
return $this->notifications()->update(['read_at' => app_now()]);
34+
}
35+
}

src/Mail/Adapters/SmtpAdapter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public function send(Envelop $envelop): bool
151151

152152
// SMTP command
153153
if ($envelop->getFrom() !== null) {
154-
$this->write('MAIL FROM: <' . $envelop->getFrom() . '>', 250);
154+
$this->write('MAIL FROM: ' . $envelop->getFrom(), 250);
155155
} elseif ($this->username !== null) {
156156
$this->write('MAIL FROM: <' . $this->username . '>', 250);
157157
}

src/Messaging/Adapters/DatabaseChannelAdapter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ public function send(Model $context, Messaging $message): void
2323

2424
$database = $message->toDatabase($context);
2525

26-
Database::table('notifications')->insert([
26+
Database::table(config('messaging.notification.table') ?? 'notifications')->insert([
2727
'id' => str_uuid(),
2828
'data' => $database['data'],
2929
'concern_id' => $context->getKey(),
3030
'concern_type' => get_class($context),
31-
'type' => $database['type'],
31+
'type' => $database['type'] ?? 'notification',
3232
]);
3333
}
3434
}

tests/Application/ApplicationTest.php

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

55
use Bow\Application\Application;
66
use Bow\Container\Capsule;
7+
use Bow\Http\Exception\BadRequestException;
78
use Bow\Http\Request;
89
use Bow\Http\Response;
910
use Bow\Router\Exception\RouterException;
@@ -87,6 +88,11 @@ public function test_send_application_with_404_status()
8788
$app->send();
8889
}
8990

91+
/**
92+
* @throws BadRequestException
93+
* @throws \ReflectionException
94+
* @throws RouterException
95+
*/
9096
public function test_send_application_with_matched_route()
9197
{
9298
$response = Mockery::mock(Response::class);

tests/Config/stubs/mail.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
'charset' => 'utf8',
66

77
'smtp' => [
8-
'hostname' => 'localhost',
8+
'hostname' => '192.168.1.3',
99
'username' => 'test@test.dev',
1010
'password' => null,
1111
'port' => 1025,

tests/Database/Query/ModelQueryTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ public function test_insert_by_create_method(string $name)
147147
$next_id = PetModelStub::all()->count() + 1;
148148

149149
$insert_result = PetModelStub::create(['name' => 'Tor']);
150+
$insert_result->persist();
150151
$select_result = PetModelStub::retrieveBy('id', $next_id)->first();
151152

152153
$this->assertInstanceOf(PetModelStub::class, $insert_result);

0 commit comments

Comments
 (0)