CityService::remove()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 6
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Service\Admin;
6
7
use App\Entity\City;
8
use App\Service\AbstractService;
9
use Doctrine\ORM\EntityManagerInterface;
10
use Symfony\Component\HttpFoundation\RequestStack;
11
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
12
13
final class CityService extends AbstractService
14
{
15
    private EntityManagerInterface $em;
16
17
    public function __construct(
18
        CsrfTokenManagerInterface $tokenManager,
19
        RequestStack $requestStack,
20
        EntityManagerInterface $entityManager
21
    ) {
22
        parent::__construct($tokenManager, $requestStack);
23
        $this->em = $entityManager;
24
    }
25
26
    public function create(City $city): void
27
    {
28
        $this->save($city);
29
        $this->clearCache('cities_count');
30
        $this->addFlash('success', 'message.created');
31
    }
32
33
    public function update(City $city): void
34
    {
35
        $this->save($city);
36
        $this->addFlash('success', 'message.updated');
37
    }
38
39
    public function remove(City $city): void
40
    {
41
        $this->em->remove($city);
42
        $this->em->flush();
43
        $this->clearCache('cities_count');
44
        $this->addFlash('success', 'message.deleted');
45
    }
46
47
    private function save(City $city): void
48
    {
49
        $this->em->persist($city);
50
        $this->em->flush();
51
    }
52
}
53