Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
73.91% |
17 / 23 |
|
80.00% |
4 / 5 |
CRAP | |
0.00% |
0 / 1 |
| AbstractDavTask | |
72.73% |
16 / 22 |
|
80.00% |
4 / 5 |
7.99 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| resolvePdo | |
25.00% |
2 / 8 |
|
0.00% |
0 / 1 |
6.80 | |||
| resolveCalendarSync | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| resolveContactSync | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| resolveAccountResolver | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | /** @license For full copyright and license information, please see the LICENSE.md file. */ |
| 6 | |
| 7 | namespace App\Modules\Dav\Task; |
| 8 | |
| 9 | defined('AMMONLY_APP') || exit('Direct script access is forbidden.'); |
| 10 | |
| 11 | use App\Core\Cron\CronTaskInterface; |
| 12 | use App\Core\Database\MultiDbRouter; |
| 13 | use App\Modules\Dav\Application\Service\DavAccountResolver; |
| 14 | use App\Modules\Dav\Application\Service\DavCalendarSyncService; |
| 15 | use App\Modules\Dav\Application\Service\DavContactSyncService; |
| 16 | use App\Modules\Dav\Infrastructure\Client\CurlDavClient; |
| 17 | use App\Modules\Dav\Infrastructure\Converter\ICalendarConverter; |
| 18 | use App\Modules\Dav\Infrastructure\Converter\VCardConverter; |
| 19 | use App\Shared\Infrastructure\Config\InstallerConfigLoader; |
| 20 | use PDO; |
| 21 | use Throwable; |
| 22 | |
| 23 | /** |
| 24 | * Abstract Base Class for DAV Scheduled Tasks. |
| 25 | * |
| 26 | * Provides shared dependency resolution, PDO connection handling, |
| 27 | * and service initialization for CalDAV and CardDAV synchronization tasks. |
| 28 | * |
| 29 | * @package App\Modules\Dav\Task |
| 30 | */ |
| 31 | abstract readonly class AbstractDavTask implements CronTaskInterface |
| 32 | { |
| 33 | /** |
| 34 | * AbstractDavTask constructor. |
| 35 | * |
| 36 | * @param PDO|null $pdo Optional PDO database connection. |
| 37 | * @param DavCalendarSyncService|null $calendarSync Calendar sync service. |
| 38 | * @param DavContactSyncService|null $contactSync Contact sync service. |
| 39 | * @param DavAccountResolver|null $accountResolver Account resolver service. |
| 40 | * @param string $tablePrefix Database table prefix. |
| 41 | */ |
| 42 | public function __construct( |
| 43 | protected ?PDO $pdo = null, |
| 44 | protected ?DavCalendarSyncService $calendarSync = null, |
| 45 | protected ?DavContactSyncService $contactSync = null, |
| 46 | protected ?DavAccountResolver $accountResolver = null, |
| 47 | protected string $tablePrefix = 'a_' |
| 48 | ) { |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Resolves PDO connection instance. |
| 53 | * |
| 54 | * @return PDO Active PDO connection. |
| 55 | */ |
| 56 | protected function resolvePdo(): PDO |
| 57 | { |
| 58 | if ($this->pdo instanceof PDO) { |
| 59 | return $this->pdo; |
| 60 | } |
| 61 | |
| 62 | try { |
| 63 | $config = InstallerConfigLoader::load(); |
| 64 | $dbParams = $config['db'] ?? []; |
| 65 | $router = new MultiDbRouter(['default' => $dbParams]); |
| 66 | |
| 67 | return $router->getConnection('default'); |
| 68 | } catch (Throwable) { |
| 69 | return new PDO('sqlite::memory:'); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Resolves initialized DavCalendarSyncService instance. |
| 75 | * |
| 76 | * @param PDO $pdo Active PDO connection. |
| 77 | * @return DavCalendarSyncService |
| 78 | */ |
| 79 | protected function resolveCalendarSync(PDO $pdo): DavCalendarSyncService |
| 80 | { |
| 81 | return $this->calendarSync ?? new DavCalendarSyncService( |
| 82 | $pdo, |
| 83 | new CurlDavClient(), |
| 84 | new ICalendarConverter(), |
| 85 | $this->tablePrefix |
| 86 | ); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Resolves initialized DavContactSyncService instance. |
| 91 | * |
| 92 | * @param PDO $pdo Active PDO connection. |
| 93 | * @return DavContactSyncService |
| 94 | */ |
| 95 | protected function resolveContactSync(PDO $pdo): DavContactSyncService |
| 96 | { |
| 97 | return $this->contactSync ?? new DavContactSyncService( |
| 98 | $pdo, |
| 99 | new CurlDavClient(), |
| 100 | new VCardConverter(), |
| 101 | $this->tablePrefix |
| 102 | ); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Resolves initialized DavAccountResolver instance. |
| 107 | * |
| 108 | * @param PDO $pdo Active PDO connection. |
| 109 | * @return DavAccountResolver |
| 110 | */ |
| 111 | protected function resolveAccountResolver(PDO $pdo): DavAccountResolver |
| 112 | { |
| 113 | return $this->accountResolver ?? new DavAccountResolver($pdo, $this->tablePrefix); |
| 114 | } |
| 115 | } |