MenuRepository   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 26
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A findItems() 0 3 1
A reorderItems() 0 14 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Repository;
6
7
use App\Entity\Menu;
8
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
9
use Doctrine\Persistence\ManagerRegistry;
10
11
/**
12
 * @method Menu|null find($id, $lockMode = null, $lockVersion = null)
13
 * @method Menu|null findOneBy(array $criteria, array $orderBy = null)
14
 * @method Menu[]    findAll()
15
 * @method Menu[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
16
 */
17
final class MenuRepository extends ServiceEntityRepository
18
{
19
    public function __construct(ManagerRegistry $registry)
20
    {
21
        parent::__construct($registry, Menu::class);
22
    }
23
24
    public function findItems(): array
25
    {
26
        return $this->findBy([], ['sort_order' => 'ASC']);
27
    }
28
29
    public function reorderItems(array $items): void
30
    {
31
        $i = 1;
32
33
        foreach ($items as $item) {
34
            $this->createQueryBuilder('i')
35
                ->update('App\Entity\Menu', 'm')
36
                ->set('m.sort_order', $i)
37
                ->where('m.id = ?1')
38
                ->setParameter(1, $item)
39
                ->getQuery()
40
                ->execute();
41
42
            ++$i;
43
        }
44
    }
45
}
46