Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
TenantEventDispatcher
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
6 / 6
6
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 dispatchTenantCreated
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 dispatchTenantSuspended
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 dispatchTenantDeleted
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 dispatchTenantPlanChanged
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 dispatch
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * Tenant event dispatcher
5 *
6 * This Source Code Form is subject to the terms of the Mozilla Public License,
7 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
8 * obtain one at https://mozilla.org/MPL/2.0/.
9 *
10 * @package   phpMyFAQ
11 * @author    Thorsten Rinne <thorsten@phpmyfaq.de>
12 * @copyright 2026 phpMyFAQ Team
13 * @license   https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
14 * @link      https://www.phpmyfaq.de
15 * @since     2026-02-08
16 */
17
18declare(strict_types=1);
19
20namespace phpMyFAQ\Tenant;
21
22use Symfony\Component\EventDispatcher\EventDispatcherInterface;
23
24class TenantEventDispatcher
25{
26    public const string TENANT_CREATED = 'tenant.created';
27    public const string TENANT_SUSPENDED = 'tenant.suspended';
28    public const string TENANT_DELETED = 'tenant.deleted';
29    public const string TENANT_PLAN_CHANGED = 'tenant.plan.changed';
30
31    public function __construct(
32        private readonly EventDispatcherInterface $eventDispatcher,
33    ) {
34    }
35
36    /**
37     * @param array<string, mixed> $context
38     */
39    public function dispatchTenantCreated(int $tenantId, array $context = []): TenantLifecycleEvent
40    {
41        return $this->dispatch(self::TENANT_CREATED, $tenantId, $context);
42    }
43
44    /**
45     * @param array<string, mixed> $context
46     */
47    public function dispatchTenantSuspended(int $tenantId, array $context = []): TenantLifecycleEvent
48    {
49        return $this->dispatch(self::TENANT_SUSPENDED, $tenantId, $context);
50    }
51
52    /**
53     * @param array<string, mixed> $context
54     */
55    public function dispatchTenantDeleted(int $tenantId, array $context = []): TenantLifecycleEvent
56    {
57        return $this->dispatch(self::TENANT_DELETED, $tenantId, $context);
58    }
59
60    /**
61     * @param array<string, mixed> $context
62     */
63    public function dispatchTenantPlanChanged(
64        int $tenantId,
65        string $oldPlan,
66        string $newPlan,
67        array $context = [],
68    ): TenantLifecycleEvent {
69        $eventContext = $context;
70        $eventContext['oldPlan'] = $oldPlan;
71        $eventContext['newPlan'] = $newPlan;
72
73        return $this->dispatch(self::TENANT_PLAN_CHANGED, $tenantId, $eventContext);
74    }
75
76    /**
77     * @param array<string, mixed> $context
78     */
79    private function dispatch(string $eventName, int $tenantId, array $context): TenantLifecycleEvent
80    {
81        $event = new TenantLifecycleEvent($tenantId, $context);
82        $this->eventDispatcher->dispatch($event, $eventName);
83
84        return $event;
85    }
86}