Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
44.74% covered (danger)
44.74%
17 / 38
20.00% covered (danger)
20.00%
1 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
PermissionSetBindingMapper
44.74% covered (danger)
44.74%
17 / 38
20.00% covered (danger)
20.00%
1 / 5
22.67
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getById
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
6
 getByTarget
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 findByTargets
81.82% covered (warning)
81.82%
9 / 11
0.00% covered (danger)
0.00%
0 / 1
3.05
 findByTargetType
87.50% covered (warning)
87.50%
7 / 8
0.00% covered (danger)
0.00%
0 / 1
2.01
1<?php
2
3declare(strict_types=1);
4/**
5 * SPDX-FileCopyrightText: 2026 LibreCode coop and contributors
6 * SPDX-License-Identifier: AGPL-3.0-or-later
7 */
8
9namespace OCA\Libresign\Db;
10
11use OCP\AppFramework\Db\DoesNotExistException;
12use OCP\DB\QueryBuilder\IQueryBuilder;
13use OCP\ICacheFactory;
14use OCP\IDBConnection;
15
16/**
17 * @template-extends CachedQBMapper<PermissionSetBinding>
18 */
19class PermissionSetBindingMapper extends CachedQBMapper {
20    public function __construct(IDBConnection $db, ICacheFactory $cacheFactory) {
21        parent::__construct($db, $cacheFactory, 'libresign_permission_set_binding');
22    }
23
24    /**
25     * @throws DoesNotExistException
26     */
27    public function getById(int $id): PermissionSetBinding {
28        $cached = $this->cacheGet('id:' . $id);
29        if ($cached instanceof PermissionSetBinding) {
30            return $cached;
31        }
32
33        $qb = $this->db->getQueryBuilder();
34        $qb->select('*')
35            ->from($this->getTableName())
36            ->where($qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT)));
37
38        /** @var PermissionSetBinding */
39        $entity = $this->findEntity($qb);
40        $this->cacheEntity($entity);
41        return $entity;
42    }
43
44    /**
45     * @throws DoesNotExistException
46     */
47    public function getByTarget(string $targetType, string $targetId): PermissionSetBinding {
48        $qb = $this->db->getQueryBuilder();
49        $qb->select('*')
50            ->from($this->getTableName())
51            ->where($qb->expr()->eq('target_type', $qb->createNamedParameter($targetType)))
52            ->andWhere($qb->expr()->eq('target_id', $qb->createNamedParameter($targetId)));
53
54        /** @var PermissionSetBinding */
55        $entity = $this->findEntity($qb);
56        $this->cacheEntity($entity);
57        return $entity;
58    }
59
60    /**
61     * @param list<string> $targetIds
62     * @return list<PermissionSetBinding>
63     */
64    public function findByTargets(string $targetType, array $targetIds): array {
65        if ($targetIds === []) {
66            return [];
67        }
68
69        $qb = $this->db->getQueryBuilder();
70        $qb->select('*')
71            ->from($this->getTableName())
72            ->where($qb->expr()->eq('target_type', $qb->createNamedParameter($targetType)))
73            ->andWhere($qb->expr()->in('target_id', $qb->createNamedParameter($targetIds, IQueryBuilder::PARAM_STR_ARRAY)));
74
75        /** @var list<PermissionSetBinding> */
76        $entities = $this->findEntities($qb);
77        foreach ($entities as $entity) {
78            $this->cacheEntity($entity);
79        }
80
81        return $entities;
82    }
83
84    /**
85     * @return list<PermissionSetBinding>
86     */
87    public function findByTargetType(string $targetType): array {
88        $qb = $this->db->getQueryBuilder();
89        $qb->select('*')
90            ->from($this->getTableName())
91            ->where($qb->expr()->eq('target_type', $qb->createNamedParameter($targetType)));
92
93        /** @var list<PermissionSetBinding> */
94        $entities = $this->findEntities($qb);
95        foreach ($entities as $entity) {
96            $this->cacheEntity($entity);
97        }
98
99        return $entities;
100    }
101}