|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Service; |
|
4
|
|
|
|
|
5
|
|
|
use App\Repository\SettingsRepository; |
|
6
|
|
|
use App\Entity\Settings; |
|
7
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
8
|
|
|
|
|
9
|
|
|
class SettingsService |
|
10
|
|
|
{ |
|
11
|
|
|
/** @var Settings */ |
|
12
|
|
|
private $settings; |
|
13
|
|
|
|
|
14
|
|
|
public function __construct(SettingsRepository $settingsRepository, EntityManagerInterface $entityManager) |
|
15
|
|
|
{ |
|
16
|
|
|
$settings = $settingsRepository->getTheSingleRow(); |
|
17
|
|
|
if ($settings === null) { |
|
18
|
|
|
// Minuscule chance of a race condition but in that worst-case scenario |
|
19
|
|
|
// we'll always bring back the first row from the database when we |
|
20
|
|
|
// getTheSingleRow() so all that will happen is that an extra row |
|
21
|
|
|
// will languish in the database forever. |
|
22
|
|
|
$settings = new Settings(); |
|
23
|
|
|
$entityManager->persist($settings); |
|
24
|
|
|
$entityManager->flush(); |
|
25
|
|
|
} |
|
26
|
|
|
$this->settings = $settings; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function getSettings(): Settings |
|
30
|
|
|
{ |
|
31
|
|
|
return $this->settings; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
// Helpers for Twig |
|
35
|
|
|
public function getSiteTitle() : string |
|
36
|
|
|
{ |
|
37
|
|
|
return $this->settings->getSiteTitle() ?? ""; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function getSiteSubtitle() : string |
|
41
|
|
|
{ |
|
42
|
|
|
return $this->settings->getSiteSubtitle() ?? ""; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function getSiteAbout() : string |
|
46
|
|
|
{ |
|
47
|
|
|
return $this->settings->getSiteAbout() ?? ""; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function displayTwitterCards(): bool |
|
51
|
|
|
{ |
|
52
|
|
|
return !empty($this->settings->getTwitterHandle()); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function getTwitterHandle(): string |
|
56
|
|
|
{ |
|
57
|
|
|
return $this->settings->getTwitterHandle() ?? ""; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function getGravatarEmail(): string |
|
61
|
|
|
{ |
|
62
|
|
|
return $this->settings->getGravatarEmail() ?? ""; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function getGravatarImageUrl(int $size = 200): ?string |
|
66
|
|
|
{ |
|
67
|
|
|
if (!is_int($size)) { |
|
|
|
|
|
|
68
|
|
|
return null; |
|
69
|
|
|
} |
|
70
|
|
|
if ($size < 0 || $size > 2000) { |
|
71
|
|
|
$size = 200; |
|
72
|
|
|
} |
|
73
|
|
|
$email = $this->settings->getGravatarEmail(); |
|
74
|
|
|
if (!$email) { |
|
75
|
|
|
return null; |
|
76
|
|
|
} |
|
77
|
|
|
$hash = md5(trim(strtolower($email))); |
|
78
|
|
|
// TODO: This is hardcoded to 200px. Make it configurable. |
|
79
|
|
|
return "https://www.gravatar.com/avatar/$hash?s=$size"; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function getHasGravatar(): bool |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->settings->getGravatarEmail() != null; |
|
|
|
|
|
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|