Completed
Pull Request — master (#30)
by
unknown
02:28
created

CheckIpAddressIsBlacklistedCommand::execute()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 34
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 34
ccs 0
cts 19
cp 0
rs 8.439
cc 6
eloc 16
nc 7
nop 2
crap 42
1
<?php
2
namespace Azine\MailgunWebhooksBundle\Command;
3
4
use Azine\MailgunWebhooksBundle\Services\AzineMailgunService;
5
use Azine\MailgunWebhooksBundle\Services\HetrixtoolsService\HetrixtoolsServiceResponse;
6
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
7
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Azine\MailgunWebhooksBundle\Entity\Repositories\MailgunEventRepository;
12
use Doctrine\ORM\EntityManager;
13
use Azine\MailgunWebhooksBundle\Services\HetrixtoolsService\AzineMailgunHetrixtoolsService;
14
use Azine\MailgunWebhooksBundle\Services\AzineMailgunMailerService;
15
16
/**
17
 * Checks if the last ip address from MailgunEvent entity is in blacklist
18
 *
19
 */
20
class CheckIpAddressIsBlacklistedCommand extends ContainerAwareCommand
21
{
22
    protected function configure()
23
    {
24
        $this
25
            ->setName('mailgun:check-ip-in-blacklist')
26
            ->setDescription('Checks if the last ip address from MailgunEvent entity is in blacklist');
27
        ;
28
    }
29
30
    protected function execute(InputInterface $input, OutputInterface $output)
31
    {
32
        $eventRepository = $this->getEntityManager()->getRepository('AzineMailgunWebhooksBundle:MailgunEvent');
33
        $translator = $this->getContainer()->get('translator');
34
        $ipAddress = $eventRepository->getLastEvent()->getIp();
35
36
        $response = $this->getHetrixtoolsService()->checkIpAddressInBlacklist($ipAddress);
37
38
        if(!$response instanceof HetrixtoolsServiceResponse){
39
40
            $output->write($translator->trans("No response from Hetrixtools service, try later."), true);
41
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type of the parent method Symfony\Component\Console\Command\Command::execute of type null|integer.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
42
        }
43
44
        if($response->status == HetrixtoolsServiceResponse::RESPONSE_STATUS_SUCCESS){
45
46
            if($response->blacklisted_count > 0){
47
48
                try{
49
50
                    $messagesSent = $this->getAzineMailgunServiceService()->sendBlacklistNotification($response);
51
52
                    if($messagesSent > 0){
53
54
                        $output->write($translator->trans("Blacklist report was sent."), true);
55
                    }
56
                }
57
                catch (\Exception $e){
58
59
                    $output->write($e->getMessage(), true);
60
                }
61
            }
62
        }
63
    }
64
65
    /**
66
     * Get EntityManager
67
     * @return EntityManager
68
     */
69
    private function getEntityManager()
70
    {
71
        return $this->getContainer()->get('doctrine')->getManager();
72
    }
73
74
    /**
75
     * Get AzineMailgunHetrixtoolsService
76
     * @return AzineMailgunHetrixtoolsService
77
     */
78
    private function getHetrixtoolsService()
79
    {
80
        return $this->getContainer()->get('azine.mailgun.hetrixtools.service');
81
    }
82
83
    /**
84
     * Get AzineMailgunMailerService
85
     * @return AzineMailgunMailerService
86
     */
87
    private function getAzineMailgunServiceService()
88
    {
89
        return $this->getContainer()->get('azine.mailgun.mailer');
90
    }
91
}