Skip to content

Instantly share code, notes, and snippets.

Avatar

Richard Keil richartkeil

View GitHub Profile
View 4.ts
const processUser = functions.firestore
.document("accounts/{accountId}/users/{userId}")
.onCreate(async (snapshot, context) => {
try {
await processTheUser(snapshot);
const event = new UserProcessed(snapshot);
await new Account(context.params.accountId).addEvent(event);
} catch (error) {
const event = new UserProcessingFailed(snapshot, error);
await new Account(context.params.accountId).addEvent(event);
View 3.ts
class Account {
constructor(protected accountId: string) {}
public async addEvent(event: Event) {
return firestore()
.collection("accounts")
.doc(this.accountId)
.collection("events")
.add({
name: event.getName(),
View 2.ts
interface Event {
public isCritical(): boolean;
public getName(): string;
public getPayload(): object;
}
class UserProcessed implements Event {
constructor(protected user: firestore.DocumentSnapshot) {}
public isCritical() {
View 1.ts
const processUser = functions.firestore
.document("accounts/{accountId}/users/{userId}")
.onCreate(async (snapshot, context) => {
await processTheUser(snapshot);
const event = new UserProcessed(snapshot);
await new Account(context.params.accountId).addEvent(event);
})
View dependency-inversion-2.ts
interface Broadcaster {
send(event: Event, user: User)
}
class EmailBroadcaster implements Broadcaster {
send(event: Event, user: User) {
const description = `New event ${event.name} occured at ${event.getTimestamp()}`
emailService.send({ to: user.email, subject: event.name, body: description })
}
}
View dependency-inversion-1.ts
class EmailBroadcaster {
send(event: Event, user: User) {
const description = `New event ${event.name} occured at ${event.getTimestamp()}`
emailService.send({ to: user.email, subject: event.name, body: description })
}
}
class NotificationHandler {
private mailer: EmailBroadcaster
View interface-segregation-2.ts
interface CanBroadcast {
send(event: Event, user: User)
}
interface CanTrackReads {
trackReads(track: boolean);
}
class EmailBroadcaster implements CanBroadcast, CanTrackReads {
send(event: Event, user: User) {
View interface-segregation-1.ts
interface CanBroadcast {
send(event: Event, user: User)
trackReads(track: boolean)
}
class EmailBroadcaster implements CanBroadcast {
send(event: Event, user: User) {
emailService.send({ to: user.email, subject: event.name })
}
trackReads(track: boolean) {
@richartkeil
richartkeil / Tests\CreatesFakeStorage.php
Created Nov 20, 2018
Laravel Storage and Parallel Testing - CreatesFakeStorage
View Tests\CreatesFakeStorage.php
<?php
namespace Tests;
use Storage;
use Illuminate\Filesystem\Filesystem;
trait CreatesFakeStorage
{
/**
@richartkeil
richartkeil / Illuminate\Support\Facades\Storage.php
Last active May 31, 2020
Laravel Storage and Parallel Testing - Storage Facade
View Illuminate\Support\Facades\Storage.php
<?php
// ...
public static function fake($disk = null, array $config = []) {
$disk = $disk ?: static::$app['config']->get('filesystems.default');
(new Filesystem)->cleanDirectory(
$root = storage_path('framework/testing/disks/'.$disk)
);