Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
SignerNotificationPolicy
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
4
100.00% covered (success)
100.00%
1 / 1
 getValidationError
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3declare(strict_types=1);
4/**
5 * SPDX-FileCopyrightText: 2020-2024 LibreCode coop and contributors
6 * SPDX-License-Identifier: AGPL-3.0-or-later
7 */
8
9namespace OCA\Libresign\Service;
10
11use OCA\Libresign\Db\SignRequest;
12
13class SignerNotificationPolicy {
14    /**
15     * @param array $signer Signer data with email or uid key
16     * @param array<string, SignRequest[]> $signRequestIndex Indexed by identifierKey:identifierValue
17     * @return array{code:string, params:array}|null
18     */
19    public function getValidationError(array $signer, array $signRequestIndex): ?array {
20        $signerKey = key($signer);
21        $signerValue = current($signer);
22        $indexKey = $signerKey . ':' . $signerValue;
23
24        $matchingSignRequests = $signRequestIndex[$indexKey] ?? [];
25        if (empty($matchingSignRequests)) {
26            return [
27                'code' => 'not_requested',
28                'params' => [$signer['email'] ?? $signerValue],
29            ];
30        }
31
32        foreach ($matchingSignRequests as $signRequest) {
33            if ($signRequest->getSigned() !== null) {
34                return [
35                    'code' => 'already_signed',
36                    'params' => [$signRequest->getDisplayName()],
37                ];
38            }
39        }
40
41        return null;
42    }
43}