Skip to content

Commit 2119f31

Browse files
committed
Add tenant configuration
1 parent b9be5e0 commit 2119f31

3 files changed

Lines changed: 119 additions & 10 deletions

File tree

src/Adapters/Laravel/Repositories/DatabaseAnalyticsRepository.php

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ public function all(): array
3232
{
3333
/** @var array<int, Analytic> $all */
3434
$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
35+
id: (int) $analytic->id,
36+
name: $analytic->name,
37+
impressions: (int) $analytic->impressions,
38+
hovers: (int) $analytic->hovers,
39+
clicks: (int) $analytic->clicks,
4040
))->toArray();
4141

4242
return $all;
@@ -50,21 +50,38 @@ public function increment(string $name, EventType $event): void
5050
[
5151
'allowed_analytics' => $allowedAnalytics,
5252
'max_analytics' => $maxAnalytics,
53+
'tenant_field' => $tenantField,
54+
'tenant_id' => $tenantId,
5355
] = $this->config->toArray();
5456

5557
if (count($allowedAnalytics) > 0 && ! in_array($name, $allowedAnalytics, true)) {
5658
return;
5759
}
5860

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]);
61+
// Restrict query to tenant if tenant field and id are set
62+
$baseQuery = DB::table('pan_analytics');
63+
64+
if ($tenantField !== null && $tenantId !== null) {
65+
$baseQuery->where($tenantField, $tenantId);
66+
}
67+
68+
$fieldQuery = clone $baseQuery;
69+
$fieldQuery = $fieldQuery->where('name', $name);
70+
71+
if ($fieldQuery->count() === 0) {
72+
if ($baseQuery->count() < $maxAnalytics) {
73+
$baseQuery->insert(array_filter([
74+
'name' => $name,
75+
$event->column() => 1,
76+
'tenant_field' => $tenantField,
77+
'tenant_id' => $tenantId,
78+
]));
6279
}
6380

6481
return;
6582
}
6683

67-
DB::table('pan_analytics')->where('name', $name)->increment($event->column());
84+
$fieldQuery->increment($event->column());
6885
}
6986

7087
/**

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
}

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
});

0 commit comments

Comments
 (0)