Completed
Pull Request — master (#4256)
by Craig
05:44
created

HookBindingRepository::findOneOrNullByAreas()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 2
dl 0
loc 7
rs 10
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\Bundle\HookBundle\Repository;
15
16
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
17
use Doctrine\ORM\Query\Expr\OrderBy;
18
use Doctrine\Persistence\ManagerRegistry;
19
use Zikula\Bundle\HookBundle\Entity\HookBindingEntity;
20
use Zikula\Bundle\HookBundle\RepositoryInterface\HookBindingRepositoryInterface;
21
22
class HookBindingRepository extends ServiceEntityRepository implements HookBindingRepositoryInterface
23
{
24
    public function __construct(ManagerRegistry $registry)
25
    {
26
        parent::__construct($registry, HookBindingEntity::class);
27
    }
28
29
    public function deleteByBothAreas(string $subscriberArea, string $providerArea): void
30
    {
31
        $qb = $this->_em->createQueryBuilder();
32
        $qb->delete(HookBindingEntity::class, 't')
33
            ->where('t.pareaid = ?1 AND t.sareaid = ?2')
34
            ->setParameters([1 => $providerArea, 2 => $subscriberArea])
35
            ->getQuery()
36
            ->execute();
37
    }
38
39
    public function selectByAreaName(string $areaName, string $type = 'sareaid'): array
40
    {
41
        $type = in_array($type, ['sareaid', 'pareaid']) ? $type : 'sareaid';
42
        $order = new OrderBy();
43
        $order->add('t.sortorder', 'ASC');
44
        $order->add('t.sareaid', 'ASC');
45
46
        return $this->createQueryBuilder('t')
47
            ->where("t.${type} = ?1")
48
            ->orderBy($order)
49
            ->setParameter(1, $areaName)
50
            ->getQuery()
51
            ->getArrayResult();
52
    }
53
54
    public function setSortOrder(int $order, string $subscriberAreaName, string $providerAreaName): void
55
    {
56
        $this->_em->createQueryBuilder()
57
            ->update(HookBindingEntity::class, 't')
58
            ->set('t.sortorder', $order)
59
            ->where('t.sareaid = ?1 AND t.pareaid = ?2')
60
            ->setParameters([1 => $subscriberAreaName, 2 => $providerAreaName])
61
            ->getQuery()
62
            ->execute();
63
    }
64
65
    public function findOneOrNullByAreas(string $subscriberArea, string $providerArea)
66
    {
67
        return $this->createQueryBuilder('t')
68
            ->where('t.sareaid = ?1 AND t.pareaid = ?2')
69
            ->setParameters([1 => $subscriberArea, 2 => $providerArea])
70
            ->getQuery()
71
            ->getOneOrNullResult();
72
    }
73
74
    public function findByOwners(string $subscriberOwner, string $providerOwner): array
75
    {
76
        return $this->createQueryBuilder('t')
77
            ->where('t.sowner = ?1 AND t.powner = ?2')
78
            ->setParameters([1 => $subscriberOwner, 2 => $providerOwner])
79
            ->getQuery()
80
            ->getArrayResult();
81
    }
82
83
    public function deleteAllByOwner(string $owner): void
84
    {
85
        $qb = $this->_em->createQueryBuilder();
86
        $qb->delete(HookBindingEntity::class, 't')
87
            ->where('t.sowner = ?1 OR t.powner = ?2')
88
            ->setParameters([1 => $owner, 2 => $owner])
89
            ->getQuery()
90
            ->execute();
91
    }
92
}
93