Completed
Push — master ( 9c95f5...e428dc )
by Paweł
06:06
created

SettingsRepository::findAllByScopeAndOwner()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 12
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Superdesk Web Publisher Settings Bundle.
7
 *
8
 * Copyright 2017 Sourcefabric z.ú. and contributors.
9
 *
10
 * For the full copyright and license information, please see the
11
 * AUTHORS and LICENSE files distributed with this source code.
12
 *
13
 * @copyright 2017 Sourcefabric z.ú
14
 * @license http://www.superdesk.org/license
15
 */
16
17
namespace SWP\Bundle\SettingsBundle\Model;
18
19
use Doctrine\ORM\QueryBuilder;
20
use SWP\Bundle\SettingsBundle\Context\ScopeContextInterface;
21
use SWP\Bundle\StorageBundle\Doctrine\ORM\EntityRepository;
22
23
class SettingsRepository extends EntityRepository implements SettingsRepositoryInterface
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function findAllByScopeAndOwner(ScopeContextInterface $scopeContext): QueryBuilder
29
    {
30
        $qb = $this->createQueryBuilder('s')
31
            ->andWhere('s.scope = :globalScope')
32
            ->setParameter('globalScope', ScopeContextInterface::SCOPE_GLOBAL);
33
34
        /* @var array $scope */
35
        foreach ($scopeContext->getScopesOwners() as $scopeName => $owner) {
36
            $qb->orWhere(
37
                $qb->expr()->andX(
38
                    $qb->expr()->eq('s.scope', ':scope_'.$scopeName),
39
                    $qb->expr()->eq('s.owner', ':owner_'.$scopeName)
40
                )
41
            )
42
            ->setParameter('scope_'.$scopeName, $scopeName)
43
            ->setParameter('owner_'.$scopeName, $owner->getId());
44
        }
45
46
        return $qb;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function findOneByNameAndScopeAndOwner(string $name, string $scope, SettingsOwnerInterface $owner = null): QueryBuilder
53
    {
54
        $qb = $this->createQueryBuilder('s')
55
            ->andWhere('s.scope = :scope')
56
            ->setParameter('scope', $scope)
57
            ->andWhere('s.name = :name')
58
            ->setParameter('name', $name);
59
60
        if (null !== $scope && null !== $owner) {
61
            $qb
62
                ->andWhere('s.owner = :owner')
63
                ->setParameter('owner', $owner->getId())
64
            ;
65
        }
66
67
        return $qb;
68
    }
69
}
70