Completed
Push — master ( cd3b2e...50c111 )
by Craig
07:10
created

BlockRepository::persistAndFlush()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Zikula package.
5
 *
6
 * Copyright Zikula Foundation - http://zikula.org/
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zikula\BlocksModule\Entity\Repository;
13
14
use Doctrine\ORM\EntityRepository;
15
use Zikula\BlocksModule\Entity\BlockEntity;
16
use Zikula\BlocksModule\Entity\RepositoryInterface\BlockRepositoryInterface;
17
18
class BlockRepository extends EntityRepository implements BlockRepositoryInterface
19
{
20
    public function getFilteredBlocks(array $filters)
21
    {
22
        $qb = $this->_em->createQueryBuilder();
23
        $query = $qb->select('b')
24
            ->from($this->_entityName, 'b');
25
        if (isset($filters['position'])) {
26
            $subQb = $this->_em->createQueryBuilder();
27
            $query
28
                ->join('b.placements', 'p')
29
                ->where($qb->expr()->in('p.position',
30
                    $subQb->select('bp')
31
                        ->from('ZikulaBlocksModule:BlockPositionEntity', 'bp')
32
                        ->where('bp.pid = ?1')
33
                        ->getDQL()
34
                ))
35
                ->setParameter(1, $filters['position']);
36
            unset($filters['position']);
37
        }
38
        $paramIndex = 2;
39
        $sortField = isset($filters['sort-field']) ? $filters['sort-field'] : 'bid';
40
        $sortDirection = isset($filters['sort-direction']) ? $filters['sort-direction'] : 'ASC';
41
        unset($filters['sort-field'], $filters['sort-direction']);
42
        foreach ($filters as $key => $value) {
43
            if (!isset($value)) {
44
                unset($filters[$key]);
45
            } else {
46
                $query->andWhere($qb->expr()->eq('b.' . $key, '?' . $paramIndex))
47
                    ->setParameter($paramIndex, $value);
48
            }
49
        }
50
        $query->orderBy('b.' . $sortField, $sortDirection);
51
52
        return $query->getQuery()->getResult();
53
    }
54
55
    public function persistAndFlush(BlockEntity $entity)
56
    {
57
        $this->_em->persist($entity);
58
        $this->_em->flush($entity);
59
    }
60
}
61