Completed
Push — master ( 1e9262...2e1128 )
by Matt
15s queued 12s
created

SettingsService   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
eloc 23
c 4
b 1
f 0
dl 0
loc 73
rs 10
wmc 14

10 Methods

Rating   Name   Duplication   Size   Complexity  
A displayTwitterCards() 0 3 1
A getTwitterHandle() 0 3 1
A getSiteAbout() 0 3 1
A __construct() 0 13 2
A getSiteTitle() 0 3 1
A getSettings() 0 3 1
A getSiteSubtitle() 0 3 1
A getGravatarEmail() 0 3 1
A getGravatarImageUrl() 0 12 4
A getHasGravatar() 0 3 1
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 ($size < 0 || $size > 2000) {
68
            $size = 200;
69
        }
70
        $email = $this->settings->getGravatarEmail();
71
        if (!$email) {
72
            return null;
73
        }
74
        $hash = md5(trim(strtolower($email)));
75
        // TODO: This is hardcoded to 200px. Make it configurable.
76
        return "https://www.gravatar.com/avatar/$hash?s=$size";
77
    }
78
79
    public function getHasGravatar(): bool
80
    {
81
        return !empty($this->settings->getGravatarEmail());
82
    }
83
}
84