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

AzineMailgunCockpitService   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
c 2
b 0
f 0
lcom 1
cbo 2
dl 0
loc 49
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getLastKnownSenderIp() 0 13 2
A getEmailDomain() 0 4 1
A getCockpitDataAsArray() 0 7 1
A getValueOrEmptyString() 0 4 2
A __construct() 0 6 1
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
}