Completed
Push — master ( fc1f93...3f8ded )
by Valery
07:43
created

PhotoRepository::reorderPhotos()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 14
rs 9.9332
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Repository;
6
7
use App\Entity\Photo;
8
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
9
use Symfony\Bridge\Doctrine\RegistryInterface;
10
11
/**
12
 * @method Photo|null find($id, $lockMode = null, $lockVersion = null)
13
 * @method Photo|null findOneBy(array $criteria, array $orderBy = null)
14
 * @method Photo[]    findAll()
15
 * @method Photo[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
16
 */
17
final class PhotoRepository extends ServiceEntityRepository
18
{
19
    public function __construct(RegistryInterface $registry)
20
    {
21
        parent::__construct($registry, Photo::class);
22
    }
23
24
    public function reorderPhotos(array $ids): void
25
    {
26
        $i = 1;
27
28
        foreach ($ids as $id) {
29
            $this->createQueryBuilder('i')
30
                ->update('App\Entity\Photo', 'p')
31
                ->set('p.sort_order', $i)
32
                ->where('p.id = ?1')
33
                ->setParameter(1, $id)
34
                ->getQuery()
35
                ->execute();
36
37
            ++$i;
38
        }
39
    }
40
}
41