UserPropertyRepository::findByUser()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 9
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 15
rs 9.9666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Repository;
6
7
use App\Entity\Property;
8
use Knp\Component\Pager\Pagination\PaginationInterface;
9
use Throwable;
10
11
final class UserPropertyRepository extends PropertyRepository
12
{
13
    public function findByUser(array $params): PaginationInterface
14
    {
15
        $qb = $this->createQueryBuilder('p')
16
            ->where('p.author = :id');
17
18
        if ('published' === $params['state']) {
19
            $qb->andWhere("p.state = 'published'");
20
        } else {
21
            $qb->andWhere("p.state != 'published'");
22
        }
23
24
        $qb->orderBy('p.id', 'DESC')
25
            ->setParameter('id', $params['user']);
26
27
        return $this->createPaginator($qb->getQuery(), $params['page']);
28
    }
29
30
    public function changeState(Property $property, string $state): bool
31
    {
32
        try {
33
            $property->setState($state);
34
            $em = $this->getEntityManager();
35
            $em->persist($property);
36
            $em->flush();
37
38
            return true;
39
        } catch (Throwable $e) {
40
            return false;
41
        }
42
    }
43
}
44