Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
82.93% |
34 / 41 |
|
75.00% |
6 / 8 |
CRAP | |
0.00% |
0 / 1 |
| IdentifyMethod | |
82.93% |
34 / 41 |
|
75.00% |
6 / 8 |
20.80 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
1 | |||
| setIdentifiedAtDate | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
| isDeletedAccount | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getIdentifierValue | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
2.06 | |||
| setLastAttemptDate | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| getUniqueIdentifier | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getRequirement | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
6 | |||
| setRequirement | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| 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\Db; |
| 10 | |
| 11 | use OCA\Libresign\Enum\IdentifyMethodRequirement; |
| 12 | use OCP\AppFramework\Db\Entity; |
| 13 | use OCP\DB\Types; |
| 14 | |
| 15 | /** |
| 16 | * @method void setSignRequestId(int $signRequestId) |
| 17 | * @method int getSignRequestId() |
| 18 | * @method void setAttempts(int $attempts) |
| 19 | * @method int getAttempts() |
| 20 | * @method void setMandatory(int $mandatory) |
| 21 | * @method int getMandatory() |
| 22 | * @method void setIdentifierKey(string $identifierKey) |
| 23 | * @method string getIdentifierKey() |
| 24 | * @method void setIdentifierValue(string $identifierValue) |
| 25 | * @method void setCode(string $code) |
| 26 | * @method ?string getCode() |
| 27 | * @method ?\DateTime getIdentifiedAtDate() |
| 28 | * @method ?\DateTime getLastAttemptDate() |
| 29 | * @method void setMetadata(array $metadata) |
| 30 | * @method array getMetadata() |
| 31 | */ |
| 32 | class IdentifyMethod extends Entity { |
| 33 | public int $signRequestId = 0; |
| 34 | public int $mandatory = 0; |
| 35 | public string $identifierKey = ''; |
| 36 | public string $identifierValue = ''; |
| 37 | public int $attempts = 0; |
| 38 | public ?string $code = null; |
| 39 | public ?\DateTime $identifiedAtDate = null; |
| 40 | public ?\DateTime $lastAttemptDate = null; |
| 41 | protected ?array $metadata = null; |
| 42 | |
| 43 | public function __construct() { |
| 44 | $this->addType('signRequestId', Types::INTEGER); |
| 45 | $this->addType('mandatory', Types::INTEGER); |
| 46 | $this->addType('code', Types::STRING); |
| 47 | $this->addType('identifierKey', Types::STRING); |
| 48 | $this->addType('identifierValue', Types::STRING); |
| 49 | $this->addType('attempts', Types::INTEGER); |
| 50 | $this->addType('identifiedAtDate', Types::DATETIME); |
| 51 | $this->addType('lastAttemptDate', Types::DATETIME); |
| 52 | $this->addType('metadata', Types::JSON); |
| 53 | } |
| 54 | |
| 55 | public function setIdentifiedAtDate(null|string|\DateTime $identifiedAtDate): void { |
| 56 | if ($identifiedAtDate) { |
| 57 | if (!$identifiedAtDate instanceof \DateTime) { |
| 58 | $this->identifiedAtDate = new \DateTime($identifiedAtDate, new \DateTimeZone('UTC')); |
| 59 | } else { |
| 60 | $this->identifiedAtDate = $identifiedAtDate; |
| 61 | } |
| 62 | } else { |
| 63 | $this->identifiedAtDate = null; |
| 64 | } |
| 65 | $this->markFieldUpdated('identifiedAtDate'); |
| 66 | } |
| 67 | |
| 68 | public function isDeletedAccount(): bool { |
| 69 | $metadata = $this->getMetadata(); |
| 70 | return isset($metadata['deleted_account']); |
| 71 | } |
| 72 | |
| 73 | public function getIdentifierValue(): string { |
| 74 | $metadata = $this->getMetadata(); |
| 75 | if (isset($metadata['deleted_account'])) { |
| 76 | return $metadata['deleted_account']['email'] ?? $metadata['deleted_account']['account']; |
| 77 | } |
| 78 | return $this->identifierValue; |
| 79 | } |
| 80 | |
| 81 | public function setLastAttemptDate(null|string|\DateTime $lastAttemptDate): void { |
| 82 | if ($lastAttemptDate) { |
| 83 | if (!$lastAttemptDate instanceof \DateTime) { |
| 84 | $this->lastAttemptDate = new \DateTime($lastAttemptDate, new \DateTimeZone('UTC')); |
| 85 | } else { |
| 86 | $this->lastAttemptDate = $lastAttemptDate; |
| 87 | } |
| 88 | } else { |
| 89 | $this->lastAttemptDate = null; |
| 90 | } |
| 91 | $this->markFieldUpdated('lastAttemptDate'); |
| 92 | } |
| 93 | |
| 94 | public function getUniqueIdentifier(): string { |
| 95 | return $this->getIdentifierKey() . ':' . $this->getIdentifierValue(); |
| 96 | } |
| 97 | |
| 98 | public function getRequirement(): string { |
| 99 | $metadata = $this->getMetadata(); |
| 100 | if (is_array($metadata) && isset($metadata['requirement']) && is_string($metadata['requirement'])) { |
| 101 | $requirement = IdentifyMethodRequirement::tryFrom($metadata['requirement']); |
| 102 | if ($requirement !== null) { |
| 103 | return $requirement->value; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | return $this->mandatory === 1 |
| 108 | ? IdentifyMethodRequirement::REQUIRED->value |
| 109 | : IdentifyMethodRequirement::OPTIONAL->value; |
| 110 | } |
| 111 | |
| 112 | public function setRequirement(string $requirement): void { |
| 113 | $normalized = IdentifyMethodRequirement::tryFrom($requirement) ?? IdentifyMethodRequirement::OPTIONAL; |
| 114 | $metadata = $this->getMetadata() ?? []; |
| 115 | $metadata['requirement'] = $normalized->value; |
| 116 | $this->setMetadata($metadata); |
| 117 | $this->setMandatory($normalized === IdentifyMethodRequirement::REQUIRED ? 1 : 0); |
| 118 | } |
| 119 | } |