Completed
Push — master ( 674d5a...194be7 )
by Gorka
04:12
created

DoctrineORMBySlugSpecification   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 0
loc 27
rs 10
wmc 2
lcom 1
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A buildQuery() 0 17 1
1
<?php
2
3
/*
4
 * This file is part of the Kreta package.
5
 *
6
 * (c) Beñat Espiña <[email protected]>
7
 * (c) Gorka Laucirica <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
declare(strict_types=1);
14
15
namespace Kreta\TaskManager\Infrastructure\Persistence\Doctrine\ORM\Organization;
16
17
use Doctrine\ORM\Query;
18
use Doctrine\ORM\QueryBuilder;
19
use Kreta\SharedKernel\Domain\Model\Identity\Slug;
20
use Kreta\SharedKernel\Infrastructure\Persistence\Doctrine\ORM\DoctrineORMQuerySpecification;
21
use Kreta\TaskManager\Domain\Model\Organization\Organization;
22
23
class DoctrineORMBySlugSpecification implements DoctrineORMQuerySpecification
24
{
25
    private $slug;
26
27
    public function __construct(Slug $slug)
28
    {
29
        $this->slug = $slug;
30
    }
31
32
    public function buildQuery(QueryBuilder $queryBuilder) : Query
33
    {
34
        return $queryBuilder
35
            ->select('o')
36
            ->from(Organization::class, 'o')
37
            ->leftJoin('o.organizationMembers', 'om')
38
            ->leftJoin('o.owners', 'ow')
39
            ->where($queryBuilder->expr()->like('o.slug.slug', ':slug'))
40
            ->andWhere(
41
                $queryBuilder->expr()->orX(
42
                    $queryBuilder->expr()->eq('om.userId', ':userId'),
43
                    $queryBuilder->expr()->eq('ow.userId', ':userId')
44
                )
45
            )
46
            ->setParameter('slug', $this->slug)
47
            ->getQuery();
48
    }
49
}
50