1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace App\Service; |
6
|
|
|
|
7
|
|
|
use App\Entity\Property; |
8
|
|
|
use App\Entity\User; |
9
|
|
|
use App\Utils\Slugger; |
10
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
11
|
|
|
use Pagerfanta\Pagerfanta; |
12
|
|
|
use Symfony\Component\Cache\Adapter\FilesystemAdapter; |
13
|
|
|
|
14
|
|
|
final class PropertyService |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var EntityManagerInterface |
18
|
|
|
*/ |
19
|
|
|
private $em; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var Slugger |
23
|
|
|
*/ |
24
|
|
|
private $slugger; |
25
|
|
|
|
26
|
|
|
public function __construct(EntityManagerInterface $entityManager, Slugger $slugger) |
27
|
|
|
{ |
28
|
|
|
$this->em = $entityManager; |
29
|
|
|
$this->slugger = $slugger; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function create(Property $property, User $user): void |
33
|
|
|
{ |
34
|
|
|
// Make slug |
35
|
|
|
$slug = $this->slugger->slugify($property->getTitle()); |
36
|
|
|
|
37
|
|
|
$property->setAuthor($user); |
38
|
|
|
$property->setSlug($slug); |
39
|
|
|
$property->setPublishedAt(new \DateTime('now')); |
40
|
|
|
$property->setPublished(true); |
41
|
|
|
$this->save($property); |
42
|
|
|
$this->clearCache(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function findLatest(int $page): Pagerfanta |
46
|
|
|
{ |
47
|
|
|
$repository = $this->em->getRepository(Property::class); |
48
|
|
|
|
49
|
|
|
return $repository->findLatest($page); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function countAll(): int |
53
|
|
|
{ |
54
|
|
|
$cache = new FilesystemAdapter(); |
55
|
|
|
|
56
|
|
|
$count = $cache->get('properties_count', function () { |
57
|
|
|
return $this->em->getRepository(Property::class)->countAll(); |
58
|
|
|
}); |
59
|
|
|
|
60
|
|
|
return (int) $count; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function update(Property $property): void |
64
|
|
|
{ |
65
|
|
|
$slug = $this->slugger->slugify($property->getTitle()); |
66
|
|
|
$property->setSlug($slug); |
67
|
|
|
$this->em->flush(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function save(Property $property): void |
71
|
|
|
{ |
72
|
|
|
$this->em->persist($property); |
73
|
|
|
$this->em->flush(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function remove($object): void |
77
|
|
|
{ |
78
|
|
|
$this->em->remove($object); |
79
|
|
|
$this->em->flush(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function delete(Property $property): void |
83
|
|
|
{ |
84
|
|
|
// Search photos |
85
|
|
|
$photos = $property->getPhotos(); |
86
|
|
|
|
87
|
|
|
if ($photos) { |
|
|
|
|
88
|
|
|
// Remove photos |
89
|
|
|
foreach ($photos as $photo) { |
90
|
|
|
$this->remove($photo); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$this->remove($property); |
95
|
|
|
$this->clearCache(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
private function clearCache(): void |
99
|
|
|
{ |
100
|
|
|
$cache = new FilesystemAdapter(); |
101
|
|
|
$cache->delete('properties_count'); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|