Passed
Pull Request — master (#134)
by Matt
04:31
created

SettingsService::getGravatarImageUrl()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 9
nc 5
nop 1
dl 0
loc 15
rs 9.6111
c 0
b 0
f 0
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)) {
0 ignored issues
show
introduced by
The condition is_int($size) is always true.
Loading history...
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;
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $this->settings->getGravatarEmail() of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
85
    }
86
}
87