Skip to content

Commit 45cc881

Browse files
committed
Add tenant configuration
1 parent b9be5e0 commit 45cc881

7 files changed

Lines changed: 139 additions & 23 deletions

File tree

src/Adapters/Laravel/Repositories/DatabaseAnalyticsRepository.php

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,18 @@ public function __construct(private PanConfiguration $config)
3030
*/
3131
public function all(): array
3232
{
33+
[
34+
'tenant_field' => $tenantField,
35+
] = $this->config->toArray();
36+
3337
/** @var array<int, Analytic> $all */
3438
$all = DB::table('pan_analytics')->get()->map(fn (mixed $analytic): Analytic => new Analytic(
35-
id: (int) $analytic->id, // @phpstan-ignore-line
36-
name: $analytic->name, // @phpstan-ignore-line
37-
impressions: (int) $analytic->impressions, // @phpstan-ignore-line
38-
hovers: (int) $analytic->hovers, // @phpstan-ignore-line
39-
clicks: (int) $analytic->clicks, // @phpstan-ignore-line
39+
id: (int) $analytic->id,
40+
tenant: ($tenantField) ? $analytic->{$tenantField} : null,
41+
name: $analytic->name,
42+
impressions: (int) $analytic->impressions,
43+
hovers: (int) $analytic->hovers,
44+
clicks: (int) $analytic->clicks,
4045
))->toArray();
4146

4247
return $all;
@@ -50,21 +55,38 @@ public function increment(string $name, EventType $event): void
5055
[
5156
'allowed_analytics' => $allowedAnalytics,
5257
'max_analytics' => $maxAnalytics,
58+
'tenant_field' => $tenantField,
59+
'tenant_id' => $tenantId,
5360
] = $this->config->toArray();
5461

5562
if (count($allowedAnalytics) > 0 && ! in_array($name, $allowedAnalytics, true)) {
5663
return;
5764
}
5865

59-
if (DB::table('pan_analytics')->where('name', $name)->count() === 0) {
60-
if (DB::table('pan_analytics')->count() < $maxAnalytics) {
61-
DB::table('pan_analytics')->insert(['name' => $name, $event->column() => 1]);
66+
// Restrict query to tenant if tenant field and id are set
67+
$baseQuery = DB::table('pan_analytics');
68+
69+
if ($tenantField !== null && $tenantId !== null) {
70+
$baseQuery->where($tenantField, $tenantId);
71+
}
72+
73+
$fieldQuery = clone $baseQuery;
74+
$fieldQuery = $fieldQuery->where('name', $name);
75+
76+
if ($fieldQuery->count() === 0) {
77+
if ($baseQuery->count() < $maxAnalytics) {
78+
$baseQuery->insert(array_filter([
79+
'name' => $name,
80+
$event->column() => 1,
81+
'tenant_field' => $tenantField,
82+
'tenant_id' => $tenantId,
83+
]));
6284
}
6385

6486
return;
6587
}
6688

67-
DB::table('pan_analytics')->where('name', $name)->increment($event->column());
89+
$fieldQuery->increment($event->column());
6890
}
6991

7092
/**

src/PanConfiguration.php

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ private function __construct(
2020
private int $maxAnalytics = 50,
2121
private array $allowedAnalytics = [],
2222
private string $routePrefix = 'pan',
23+
private ?string $tenantField = null,
24+
private string|int|null $tenantId = null,
2325
) {
2426
//
2527
}
@@ -66,6 +68,26 @@ public function setRoutePrefix(string $prefix): void
6668
$this->routePrefix = $prefix;
6769
}
6870

71+
/**
72+
* Sets the tenant field to be used.
73+
*
74+
* @internal
75+
*/
76+
public function setTenantField(?string $field): void
77+
{
78+
$this->tenantField = $field;
79+
}
80+
81+
/**
82+
* Sets the tenant ID to be used.
83+
*
84+
* @internal
85+
*/
86+
public function setTenantId(string|int|null $id): void
87+
{
88+
$this->tenantId = $id;
89+
}
90+
6991
/**
7092
* Sets the maximum number of analytics to store.
7193
*/
@@ -102,6 +124,26 @@ public static function routePrefix(string $prefix): void
102124
self::instance()->setRoutePrefix($prefix);
103125
}
104126

127+
/**
128+
* Sets the tenant field to be used.
129+
*
130+
* @internal
131+
*/
132+
public static function tenantField(?string $field): void
133+
{
134+
self::instance()->setTenantField($field);
135+
}
136+
137+
/**
138+
* Sets the tenant ID to be used.
139+
*
140+
* @internal
141+
*/
142+
public static function tenantId(string|int|null $id): void
143+
{
144+
self::instance()->setTenantId($id);
145+
}
146+
105147
/**
106148
* Resets the configuration to its default values.
107149
*
@@ -112,12 +154,20 @@ public static function reset(): void
112154
self::maxAnalytics(50);
113155
self::allowedAnalytics([]);
114156
self::routePrefix('pan');
157+
self::tenantField(null);
158+
self::tenantId(null);
115159
}
116160

117161
/**
118162
* Converts the Pan configuration to an array.
119163
*
120-
* @return array{max_analytics: int, allowed_analytics: array<int, string>, route_prefix: string}
164+
* @return array{
165+
* max_analytics: int,
166+
* allowed_analytics: array<int, string>,
167+
* route_prefix: string,
168+
* tenant_field: string|null,
169+
* tenant_id: string|int|null,
170+
* }
121171
*
122172
* @internal
123173
*/
@@ -127,6 +177,8 @@ public function toArray(): array
127177
'max_analytics' => $this->maxAnalytics,
128178
'allowed_analytics' => $this->allowedAnalytics,
129179
'route_prefix' => $this->routePrefix,
180+
'tenant_field' => $this->tenantField,
181+
'tenant_id' => $this->tenantId,
130182
];
131183
}
132184
}

src/ValueObjects/Analytic.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
public function __construct(
1818
public int $id,
19+
public string|int|null $tenant,
1920
public string $name,
2021
public int $impressions,
2122
public int $hovers,
@@ -27,12 +28,13 @@ public function __construct(
2728
/**
2829
* Returns the analytic as an array.
2930
*
30-
* @return array{id: int, name: string, impressions: int, hovers: int, clicks: int}
31+
* @return array{id: int, tenant: string|int|null, name: string, impressions: int, hovers: int, clicks: int}
3132
*/
3233
public function toArray(): array
3334
{
3435
return [
3536
'id' => $this->id,
37+
'tenant' => $this->tenant,
3638
'name' => $this->name,
3739
'impressions' => $this->impressions,
3840
'hovers' => $this->hovers,

tests/Feature/Laravel/Http/EventsTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
$analytics = array_map(fn (Analytic $analytic): array => $analytic->toArray(), app(AnalyticsRepository::class)->all());
1919

2020
expect($analytics)->toBe([
21-
['id' => 1, 'name' => 'help-modal', 'impressions' => 0, 'hovers' => 0, 'clicks' => 1],
21+
['id' => 1, 'tenant' => null, 'name' => 'help-modal', 'impressions' => 0, 'hovers' => 0, 'clicks' => 1],
2222
]);
2323
});
2424

@@ -35,7 +35,7 @@
3535
$analytics = array_map(fn (Analytic $analytic): array => $analytic->toArray(), app(AnalyticsRepository::class)->all());
3636

3737
expect($analytics)->toBe([
38-
['id' => 1, 'name' => 'help-modal', 'impressions' => 0, 'hovers' => 1, 'clicks' => 0],
38+
['id' => 1, 'tenant' => null, 'name' => 'help-modal', 'impressions' => 0, 'hovers' => 1, 'clicks' => 0],
3939
]);
4040
});
4141

@@ -52,7 +52,7 @@
5252
$analytics = array_map(fn (Analytic $analytic): array => $analytic->toArray(), app(AnalyticsRepository::class)->all());
5353

5454
expect($analytics)->toBe([
55-
['id' => 1, 'name' => 'help-modal', 'impressions' => 1, 'hovers' => 0, 'clicks' => 0],
55+
['id' => 1, 'tenant' => null, 'name' => 'help-modal', 'impressions' => 1, 'hovers' => 0, 'clicks' => 0],
5656
]);
5757
});
5858

@@ -75,7 +75,7 @@
7575
$analytics = array_map(fn (Analytic $analytic): array => $analytic->toArray(), app(AnalyticsRepository::class)->all());
7676

7777
expect($analytics)->toBe([
78-
['id' => 1, 'name' => 'help-modal', 'impressions' => 1, 'hovers' => 0, 'clicks' => 1],
78+
['id' => 1, 'tenant' => null, 'name' => 'help-modal', 'impressions' => 1, 'hovers' => 0, 'clicks' => 1],
7979
]);
8080
});
8181

@@ -135,7 +135,7 @@
135135
$analytics = array_map(fn (Analytic $analytic): array => $analytic->toArray(), app(AnalyticsRepository::class)->all());
136136

137137
expect($analytics)->toBe([
138-
['id' => 1, 'name' => 'help-modal', 'impressions' => 1, 'hovers' => 0, 'clicks' => 0],
138+
['id' => 1, 'tenant' => null, 'name' => 'help-modal', 'impressions' => 1, 'hovers' => 0, 'clicks' => 0],
139139
]);
140140
})->after(function (): void {
141141
PanConfiguration::routePrefix('pan');

tests/Unit/Actions/CreateEventTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@
1515
$analytics = array_map(fn (Analytic $analytic): array => $analytic->toArray(), app(AnalyticsRepository::class)->all());
1616

1717
expect($analytics)->toBe([
18-
['id' => 1, 'name' => 'help-modal', 'impressions' => 0, 'hovers' => 1, 'clicks' => 2],
18+
['id' => 1, 'tenant' => null, 'name' => 'help-modal', 'impressions' => 0, 'hovers' => 1, 'clicks' => 2],
1919
]);
2020
});

tests/Unit/PanConfigurationTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
'max_analytics' => 50,
88
'allowed_analytics' => [],
99
'route_prefix' => 'pan',
10+
'tenant_field' => null,
11+
'tenant_id' => null,
1012
]);
1113
});
1214

@@ -17,6 +19,8 @@
1719
'max_analytics' => 100,
1820
'allowed_analytics' => [],
1921
'route_prefix' => 'pan',
22+
'tenant_field' => null,
23+
'tenant_id' => null,
2024
]);
2125
});
2226

@@ -27,6 +31,8 @@
2731
'max_analytics' => PHP_INT_MAX,
2832
'allowed_analytics' => [],
2933
'route_prefix' => 'pan',
34+
'tenant_field' => null,
35+
'tenant_id' => null,
3036
]);
3137
});
3238

@@ -37,6 +43,8 @@
3743
'max_analytics' => 50,
3844
'allowed_analytics' => ['help-modal', 'contact-modal'],
3945
'route_prefix' => 'pan',
46+
'tenant_field' => null,
47+
'tenant_id' => null,
4048
]);
4149
});
4250

@@ -45,6 +53,8 @@
4553
'max_analytics' => 50,
4654
'allowed_analytics' => [],
4755
'route_prefix' => 'pan',
56+
'tenant_field' => null,
57+
'tenant_id' => null,
4858
]);
4959
});
5060

@@ -55,6 +65,32 @@
5565
'max_analytics' => 50,
5666
'allowed_analytics' => [],
5767
'route_prefix' => 'new-pan',
68+
'tenant_field' => null,
69+
'tenant_id' => null,
70+
]);
71+
});
72+
73+
it('can set the tenant field', function (): void {
74+
PanConfiguration::tenantField('team_id');
75+
76+
expect(PanConfiguration::instance()->toArray())->toBe([
77+
'max_analytics' => 50,
78+
'allowed_analytics' => [],
79+
'route_prefix' => 'pan',
80+
'tenant_field' => 'team_id',
81+
'tenant_id' => null,
82+
]);
83+
});
84+
85+
it('can set the tenant id', function (): void {
86+
PanConfiguration::tenantId(1);
87+
88+
expect(PanConfiguration::instance()->toArray())->toBe([
89+
'max_analytics' => 50,
90+
'allowed_analytics' => [],
91+
'route_prefix' => 'pan',
92+
'tenant_field' => null,
93+
'tenant_id' => 1,
5894
]);
5995
});
6096

@@ -67,6 +103,8 @@
67103
'max_analytics' => 99,
68104
'allowed_analytics' => ['help-modal', 'contact-modal'],
69105
'route_prefix' => 'new-pan',
106+
'tenant_field' => null,
107+
'tenant_id' => null,
70108
]);
71109

72110
PanConfiguration::reset();
@@ -75,5 +113,7 @@
75113
'max_analytics' => 50,
76114
'allowed_analytics' => [],
77115
'route_prefix' => 'pan',
116+
'tenant_field' => null,
117+
'tenant_id' => null,
78118
]);
79119
});

tests/Unit/Presentors/AnalyticPresentorTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use Pan\ValueObjects\Analytic;
55

66
it('present an analytic', function (): void {
7-
$analytic = new Analytic(1, 'help-modal', 1, 1, 1);
7+
$analytic = new Analytic(1, null, 'help-modal', 1, 1, 1);
88

99
$presentor = new AnalyticPresentor;
1010

@@ -20,7 +20,7 @@
2020
});
2121

2222
it('present an analytic with 0 impressions', function (): void {
23-
$analytic = new Analytic(1, 'help-modal', 0, 1, 1);
23+
$analytic = new Analytic(1, null, 'help-modal', 0, 1, 1);
2424

2525
$presentor = new AnalyticPresentor;
2626

@@ -36,7 +36,7 @@
3636
});
3737

3838
it('present an analytic with 0 hovers', function (): void {
39-
$analytic = new Analytic(1, 'help-modal', 1, 0, 1);
39+
$analytic = new Analytic(1, null, 'help-modal', 1, 0, 1);
4040

4141
$presentor = new AnalyticPresentor;
4242

@@ -52,7 +52,7 @@
5252
});
5353

5454
it('present an analytic with 0 clicks', function (): void {
55-
$analytic = new Analytic(1, 'help-modal', 1, 1, 0);
55+
$analytic = new Analytic(1, null, 'help-modal', 1, 1, 0);
5656

5757
$presentor = new AnalyticPresentor;
5858

@@ -68,7 +68,7 @@
6868
});
6969

7070
it('presents huge numbers', function (): void {
71-
$analytic = new Analytic(1, 'help-modal', 1000000, 1000000, 1000000);
71+
$analytic = new Analytic(1, null, 'help-modal', 1000000, 1000000, 1000000);
7272

7373
$presentor = new AnalyticPresentor;
7474

@@ -84,7 +84,7 @@
8484
});
8585

8686
it('presents huge numbers with 0 impressions', function (): void {
87-
$analytic = new Analytic(1, 'help-modal', 0, 1000000, 1000000);
87+
$analytic = new Analytic(1, null, 'help-modal', 0, 1000000, 1000000);
8888

8989
$presentor = new AnalyticPresentor;
9090

0 commit comments

Comments
 (0)