|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace App\Service\Admin; |
|
6
|
|
|
|
|
7
|
|
|
use App\Entity\Property; |
|
8
|
|
|
use App\Message\DeletePhotos; |
|
9
|
|
|
use App\Service\AbstractService; |
|
10
|
|
|
use App\Utils\Slugger; |
|
11
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
12
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
|
13
|
|
|
use Symfony\Component\Messenger\MessageBusInterface; |
|
14
|
|
|
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface; |
|
15
|
|
|
|
|
16
|
|
|
final class PropertyService extends AbstractService |
|
17
|
|
|
{ |
|
18
|
|
|
private EntityManagerInterface $em; |
|
19
|
|
|
private MessageBusInterface $messageBus; |
|
20
|
|
|
private Slugger $slugger; |
|
21
|
|
|
|
|
22
|
|
|
public function __construct( |
|
23
|
|
|
CsrfTokenManagerInterface $tokenManager, |
|
24
|
|
|
RequestStack $requestStack, |
|
25
|
|
|
EntityManagerInterface $entityManager, |
|
26
|
|
|
MessageBusInterface $messageBus, |
|
27
|
|
|
Slugger $slugger |
|
28
|
|
|
) { |
|
29
|
|
|
parent::__construct($tokenManager, $requestStack); |
|
30
|
|
|
$this->em = $entityManager; |
|
31
|
|
|
$this->messageBus = $messageBus; |
|
32
|
|
|
$this->slugger = $slugger; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function create(Property $property): void |
|
36
|
|
|
{ |
|
37
|
|
|
// Make slug |
|
38
|
|
|
$slug = $this->slugger->slugify($property->getPropertyDescription()->getTitle() ?? 'property'); |
|
39
|
|
|
|
|
40
|
|
|
$property->setSlug($slug); |
|
41
|
|
|
$property->setCreatedAt(new \DateTime('now')); |
|
42
|
|
|
$property->setUpdatedAt(new \DateTime('now')); |
|
43
|
|
|
$property->setState('published'); |
|
44
|
|
|
$property->setPriorityNumber((int) ($property->getPriorityNumber())); |
|
45
|
|
|
$this->save($property); |
|
46
|
|
|
$this->clearCache('properties_count'); |
|
47
|
|
|
$this->addFlash('success', 'message.created'); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function update(Property $property): void |
|
51
|
|
|
{ |
|
52
|
|
|
$slug = $this->slugger->slugify($property->getPropertyDescription()->getTitle() ?? 'property'); |
|
53
|
|
|
$property->setSlug($slug); |
|
54
|
|
|
$property->setUpdatedAt(new \DateTime('now')); |
|
55
|
|
|
$property->setPriorityNumber((int) ($property->getPriorityNumber())); |
|
56
|
|
|
$this->em->flush(); |
|
57
|
|
|
$this->addFlash('success', 'message.updated'); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function save(Property $property): void |
|
61
|
|
|
{ |
|
62
|
|
|
$this->em->persist($property); |
|
63
|
|
|
$this->em->flush(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function remove(Property $property): void |
|
67
|
|
|
{ |
|
68
|
|
|
$this->em->remove($property); |
|
69
|
|
|
$this->em->flush(); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function delete(Property $property): void |
|
73
|
|
|
{ |
|
74
|
|
|
$this->messageBus->dispatch(new DeletePhotos($property)); |
|
75
|
|
|
$this->remove($property); |
|
76
|
|
|
$this->clearCache('properties_count'); |
|
77
|
|
|
$this->addFlash('success', 'message.deleted'); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|