setEmptyValueWhereAttributeNameIn()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 12
nc 4
nop 3
dl 0
loc 20
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula - https://ziku.la/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Zikula\UsersBundle\Repository;
15
16
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
17
use Doctrine\Persistence\ManagerRegistry;
18
use Zikula\UsersBundle\Entity\UserAttribute;
19
use Zikula\UsersBundle\UsersConstant;
20
21
class UserAttributeRepository extends ServiceEntityRepository implements UserAttributeRepositoryInterface
22
{
23
    public function __construct(ManagerRegistry $registry)
24
    {
25
        parent::__construct($registry, UserAttribute::class);
26
    }
27
28
    public function setEmptyValueWhereAttributeNameIn(
29
        array $attributeNames,
30
        array $users = [],
31
        array $forbiddenUsers = [UsersConstant::USER_ID_ADMIN, UsersConstant::USER_ID_ANONYMOUS]
32
    ): mixed {
33
        $qb = $this->createQueryBuilder('a')
34
            ->update()
35
            ->set('a.value', '\'\'')
36
            ->where('a.name IN (:attributeNames)')
37
            ->setParameter('attributeNames', $attributeNames);
38
        if (!empty($users)) {
39
            $qb->andWhere('a.user IN (:users)')
40
                ->setParameter('users', $users);
41
        }
42
        if (!empty($forbiddenUsers)) {
43
            $qb->andWhere('a.user NOT IN (:forbidden_users)')
44
                ->setParameter('forbidden_users', $forbiddenUsers);
45
        }
46
47
        return $qb->getQuery()->execute();
48
    }
49
}
50