Completed
Push — issue/AZ_53_respond_to_spam_ev... ( b7bb66...684b25 )
by Dominik
02:16
created

AzineMailgunCockpitService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 3
1
<?php
2
3
4
namespace Azine\MailgunWebhooksBundle\Services;
5
6
use Azine\MailgunWebhooksBundle\Entity\MailgunEvent;
7
use Azine\MailgunWebhooksBundle\Entity\Repositories\MailgunEventRepository;
8
use Symfony\Bridge\Doctrine\ManagerRegistry;
9
10
class AzineMailgunCockpitService
11
{
12
    private $emailDomain;
13
    /** @var ManagerRegistry  */
14
    private $managerRegistry;
15
    /** @var \Twig_Environment  */
16
    private $twig;
17
    
18
    private $cachedLastKnownIp;
19
    
20
    public function __construct(ManagerRegistry $managerRegistry, \Twig_Environment $twig, $emailDomain)
21
    {
22
        $this->emailDomain = $emailDomain;
23
        $this->managerRegistry = $managerRegistry;
24
        $this->twig = $twig;
25
    }
26
27
    public function getLastKnownSenderIp()
28
    {
29
        if (is_null($this->cachedLastKnownIp)) {
30
            /** @var MailgunEventRepository $eventRepository */
31
            $eventRepository = $this->managerRegistry->getRepository(MailgunEvent::class);
32
            $lastKnownIp = $eventRepository->getLastKnownSenderIp();
33
34
            $this->cachedLastKnownIp = $lastKnownIp;
35
        } else {
36
            $lastKnownIp = $this->cachedLastKnownIp;
37
        }
38
        return $this->getValueOrEmptyString($lastKnownIp);
39
    }
40
41
    public function getEmailDomain()
42
    {
43
        return $this->getValueOrEmptyString($this->emailDomain);
44
    }
45
46
    public function getCockpitDataAsArray()
47
    {
48
        return array(
49
            'lastKnownIp' => $this->getLastKnownSenderIp(),
50
            'emailDomain' => $this->getEmailDomain()
51
        );
52
    }
53
54
    private function getValueOrEmptyString($value)
55
    {
56
        return is_null($value) ? '' : $value;
57
    }
58
}