Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
96.67% |
29 / 30 |
|
80.00% |
4 / 5 |
CRAP | |
0.00% |
0 / 1 |
| CertificateEngineFactory | |
96.67% |
29 / 30 |
|
80.00% |
4 / 5 |
15 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getEngine | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
7 | |||
| setEngine | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| getConfiguredEngineName | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
5.07 | |||
| resolveHandler | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | /** |
| 5 | * SPDX-FileCopyrightText: 2020-2024 LibreCode coop and contributors |
| 6 | * SPDX-License-Identifier: AGPL-3.0-or-later |
| 7 | */ |
| 8 | |
| 9 | namespace OCA\Libresign\Handler\CertificateEngine; |
| 10 | |
| 11 | use OCA\Libresign\AppInfo\Application; |
| 12 | use OCA\Libresign\Exception\LibresignException; |
| 13 | use OCP\IAppConfig; |
| 14 | |
| 15 | class CertificateEngineFactory { |
| 16 | private static ?IEngineHandler $engine = null; |
| 17 | |
| 18 | public function __construct( |
| 19 | private IAppConfig $appConfig, |
| 20 | private OpenSslHandler $openSslHandler, |
| 21 | private CfsslHandler $cfsslHandler, |
| 22 | private NoneHandler $noneHandler, |
| 23 | ) { |
| 24 | } |
| 25 | |
| 26 | public function getEngine(string $engineName = '', array $rootCert = []): IEngineHandler { |
| 27 | if (self::$engine && !empty($engineName) && self::$engine->getName() === $engineName) { |
| 28 | self::$engine->populateInstance($rootCert); |
| 29 | return self::$engine; |
| 30 | } |
| 31 | |
| 32 | if (!$engineName) { |
| 33 | $configuredEngineName = $this->getConfiguredEngineName(); |
| 34 | |
| 35 | if (self::$engine && self::$engine->getName() === $configuredEngineName) { |
| 36 | self::$engine->populateInstance($rootCert); |
| 37 | return self::$engine; |
| 38 | } |
| 39 | |
| 40 | $engineName = $configuredEngineName; |
| 41 | } |
| 42 | |
| 43 | self::$engine = $this->resolveHandler($engineName); |
| 44 | self::$engine->populateInstance($rootCert); |
| 45 | return self::$engine; |
| 46 | } |
| 47 | |
| 48 | public function setEngine(string $engineName): IEngineHandler { |
| 49 | $handler = $this->resolveHandler($engineName); |
| 50 | $handler->setEngine($engineName); |
| 51 | self::$engine = $handler; |
| 52 | return self::$engine; |
| 53 | } |
| 54 | |
| 55 | private function getConfiguredEngineName(): string { |
| 56 | $configValues = $this->appConfig->getAllValues(Application::APP_ID); |
| 57 | $configuredEngineName = $configValues['certificate_engine'] ?? ''; |
| 58 | |
| 59 | if (is_string($configuredEngineName) && $configuredEngineName !== '') { |
| 60 | return $configuredEngineName; |
| 61 | } |
| 62 | |
| 63 | if (is_scalar($configuredEngineName) && (string)$configuredEngineName !== '') { |
| 64 | return (string)$configuredEngineName; |
| 65 | } |
| 66 | |
| 67 | return $this->appConfig->getValueString(Application::APP_ID, 'certificate_engine', 'openssl'); |
| 68 | } |
| 69 | |
| 70 | private function resolveHandler(string $engineName): IEngineHandler { |
| 71 | return match ($engineName) { |
| 72 | 'openssl' => $this->openSslHandler, |
| 73 | 'cfssl' => $this->cfsslHandler, |
| 74 | 'none' => $this->noneHandler, |
| 75 | default => throw new LibresignException("Certificate engine not found: $engineName"), |
| 76 | }; |
| 77 | } |
| 78 | } |