Completed
Push — master ( a15f89...abb079 )
by Craig
07:12 queued 01:36
created

BlockRepository::remove()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 4
nop 1
dl 0
loc 10
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 Foundation - 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\BlocksModule\Entity\Repository;
15
16
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
17
use Doctrine\Persistence\ManagerRegistry;
18
use Zikula\BlocksModule\Entity\BlockEntity;
19
use Zikula\BlocksModule\Entity\RepositoryInterface\BlockRepositoryInterface;
20
21
class BlockRepository extends ServiceEntityRepository implements BlockRepositoryInterface
22
{
23
    public function __construct(ManagerRegistry $registry)
24
    {
25
        parent::__construct($registry, BlockEntity::class);
26
    }
27
28
    public function getFilteredBlocks(array $filters = [])
29
    {
30
        $qb = $this->_em->createQueryBuilder();
31
        $query = $qb->select('b')
32
            ->from($this->_entityName, 'b')
33
        ;
34
        if (isset($filters['position'])) {
35
            $subQb = $this->_em->createQueryBuilder();
36
            $query
37
                ->join('b.placements', 'p')
38
                ->where($qb->expr()->in('p.position',
39
                    $subQb->select('bp')
40
                        ->from('ZikulaBlocksModule:BlockPositionEntity', 'bp')
41
                        ->where('bp.pid = ?1')
42
                        ->getDQL()
43
                ))
44
                ->setParameter(1, $filters['position'])
45
            ;
46
            unset($filters['position']);
47
        }
48
        $paramIndex = 2;
49
        $sortField = $filters['sort-field'] ?? 'bid';
50
        $sortDirection = $filters['sort-direction'] ?? 'ASC';
51
        unset($filters['sort-field'], $filters['sort-direction']);
52
        foreach ($filters as $key => $value) {
53
            if (!isset($value)) {
54
                unset($filters[$key]);
55
            } else {
56
                $query->andWhere($qb->expr()->eq('b.' . $key, '?' . $paramIndex))
57
                    ->setParameter($paramIndex, $value);
58
            }
59
        }
60
        $query->orderBy('b.' . $sortField, $sortDirection);
61
62
        return $query->getQuery()->getResult();
63
    }
64
65
    public function persistAndFlush(BlockEntity $entity): void
66
    {
67
        $this->_em->persist($entity);
68
        $this->_em->flush($entity);
69
    }
70
71
    /**
72
     * @param BlockEntity|BlockEntity[] $blocks
73
     *
74
     * @throws \Doctrine\ORM\ORMException
75
     * @throws \Doctrine\ORM\OptimisticLockException
76
     */
77
    public function remove($blocks): void
78
    {
79
        if (!is_array($blocks)) {
80
            $blocks = [$blocks];
81
        }
82
83
        foreach ($blocks as $block) {
84
            $this->_em->remove($block);
85
        }
86
        $this->_em->flush();
87
    }
88
}
89