Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
EventManagementFactory
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 8
272
0.00% covered (danger)
0.00%
0 / 1
 createEventService
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
20
 createTemplateService
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 createEventServiceWithDb
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 createTemplateServiceWithDb
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 createIntegrationService
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
20
 createIntegrationServiceWithDb
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 createServices
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 getStoreType
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace BuyerKiosk\EventManagement;
4
5use PDO;
6use InvalidArgumentException;
7use BuyerKiosk\EventManagement\Services\EventService;
8use BuyerKiosk\EventManagement\Services\TemplateService;
9use BuyerKiosk\EventManagement\Services\IntegrationService;
10use BuyerKiosk\StoreController;
11
12/**
13 * EventManagementFactory - Factory for creating Event Management service instances
14 *
15 * Provides dependency injection support for Event Management services.
16 * Handles database connection management for both store-specific and central databases.
17 *
18 * Usage:
19 * ```php
20 * // Create EventService for a specific store
21 * $eventService = EventManagementFactory::createEventService('ou00', $employeeId);
22 *
23 * // Create TemplateService (uses central DB)
24 * $templateService = EventManagementFactory::createTemplateService();
25 * ```
26 *
27 * @package BuyerKiosk\EventManagement
28 */
29class EventManagementFactory
30{
31    /**
32     * Central database name for templates
33     */
34    private const CENTRAL_DB_NAME = 'kiosk_buykiosk';
35
36    /**
37     * Create EventService for a specific store
38     *
39     * @param string $typeNum Store identifier (e.g., 'ou00', 'pa01')
40     * @param int|null $employeeId Employee ID for audit logging
41     * @return EventService Configured EventService instance
42     * @throws InvalidArgumentException If store not found or database connection fails
43     */
44    public static function createEventService(string $typeNum, ?int $employeeId = null): EventService
45    {
46        // Validate typeNum format
47        if (!preg_match('/^[a-z]{2}\d+$/', $typeNum)) {
48            throw new InvalidArgumentException("Invalid typeNum format: {$typeNum}");
49        }
50
51        // Get store database connection
52        $storeController = new StoreController($typeNum);
53        $store = $storeController->getStore();
54
55        if (!$store) {
56            throw new InvalidArgumentException("Store not found: {$typeNum}");
57        }
58
59        $db = dbConnectByName($store->getDbName());
60
61        if (!$db) {
62            throw new InvalidArgumentException("Failed to connect to store database: {$typeNum}");
63        }
64
65        return new EventService($db, $employeeId);
66    }
67
68    /**
69     * Create TemplateService (uses central DB)
70     *
71     * @return TemplateService Configured TemplateService instance
72     * @throws InvalidArgumentException If database connection fails
73     */
74    public static function createTemplateService(): TemplateService
75    {
76        $centralDb = dbConnectByName(self::CENTRAL_DB_NAME);
77
78        if (!$centralDb) {
79            throw new InvalidArgumentException("Failed to connect to central database");
80        }
81
82        return new TemplateService($centralDb);
83    }
84
85    /**
86     * Create EventService with an existing database connection
87     *
88     * Use this method when you already have a database connection
89     * and want to avoid creating a new one.
90     *
91     * @param PDO $db Existing database connection
92     * @param int|null $employeeId Employee ID for audit logging
93     * @return EventService Configured EventService instance
94     */
95    public static function createEventServiceWithDb(PDO $db, ?int $employeeId = null): EventService
96    {
97        return new EventService($db, $employeeId);
98    }
99
100    /**
101     * Create TemplateService with an existing database connection
102     *
103     * Use this method when you already have a central database connection
104     * and want to avoid creating a new one.
105     *
106     * @param PDO $centralDb Existing central database connection
107     * @return TemplateService Configured TemplateService instance
108     */
109    public static function createTemplateServiceWithDb(PDO $centralDb): TemplateService
110    {
111        return new TemplateService($centralDb);
112    }
113
114    /**
115     * Create IntegrationService for a specific store
116     *
117     * @param string $typeNum Store identifier (e.g., 'ou00', 'pa01')
118     * @return IntegrationService Configured IntegrationService instance
119     * @throws InvalidArgumentException If store not found or database connection fails
120     */
121    public static function createIntegrationService(string $typeNum): IntegrationService
122    {
123        // Validate typeNum format
124        if (!preg_match('/^[a-z]{2}\d+$/', $typeNum)) {
125            throw new InvalidArgumentException("Invalid typeNum format: {$typeNum}");
126        }
127
128        // Get store database connection
129        $storeController = new StoreController($typeNum);
130        $store = $storeController->getStore();
131
132        if (!$store) {
133            throw new InvalidArgumentException("Store not found: {$typeNum}");
134        }
135
136        $db = dbConnectByName($store->getDbName());
137        $centralDb = dbConnectByName(self::CENTRAL_DB_NAME);
138        $timezone = $store->getTimezone() ?? 'America/Chicago';
139
140        if (!$db) {
141            throw new InvalidArgumentException("Failed to connect to store database: {$typeNum}");
142        }
143
144        return new IntegrationService($db, $centralDb, $timezone);
145    }
146
147    /**
148     * Create IntegrationService with existing database connections
149     *
150     * @param PDO $db Store database connection
151     * @param PDO|null $centralDb Central database connection (optional)
152     * @param string|null $timezone Store timezone
153     * @return IntegrationService Configured IntegrationService instance
154     */
155    public static function createIntegrationServiceWithDb(
156        PDO $db,
157        ?PDO $centralDb = null,
158        ?string $timezone = null
159    ): IntegrationService {
160        return new IntegrationService($db, $centralDb, $timezone);
161    }
162
163    /**
164     * Create all services for a store context
165     *
166     * Returns EventService, TemplateService, and IntegrationService
167     * for use cases that need all services together.
168     *
169     * @param string $typeNum Store identifier (e.g., 'ou00', 'pa01')
170     * @param int|null $employeeId Employee ID for audit logging
171     * @return array{eventService: EventService, templateService: TemplateService, integrationService: IntegrationService}
172     * @throws InvalidArgumentException If store not found or database connection fails
173     */
174    public static function createServices(string $typeNum, ?int $employeeId = null): array
175    {
176        return [
177            'eventService' => self::createEventService($typeNum, $employeeId),
178            'templateService' => self::createTemplateService(),
179            'integrationService' => self::createIntegrationService($typeNum),
180        ];
181    }
182
183    /**
184     * Get the store type prefix from a typeNum
185     *
186     * Extracts the two-letter store type prefix (e.g., 'ou' from 'ou00').
187     * Useful for filtering templates by store type.
188     *
189     * @param string $typeNum Store identifier
190     * @return string|null Store type prefix or null if invalid format
191     */
192    public static function getStoreType(string $typeNum): ?string
193    {
194        if (preg_match('/^([a-z]{2})\d+$/', $typeNum, $matches)) {
195            return $matches[1];
196        }
197
198        return null;
199    }
200}