Completed
Pull Request — master (#1)
by Daniel
15:18 queued 11:09
created

Task::getFromName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Cmobi\RabbitmqBundle\Transport\Worker;
4
5
use Cmobi\RabbitmqBundle\Connection\CmobiAMQPChannel;
6
use Cmobi\RabbitmqBundle\Connection\CmobiAMQPConnectionInterface;
7
use Cmobi\RabbitmqBundle\Connection\ConnectionManager;
8
use Cmobi\RabbitmqBundle\Queue\CmobiAMQPMessage;
9
use Cmobi\RabbitmqBundle\Queue\QueueProducerInterface;
10
11
class Task implements QueueProducerInterface
12
{
13
    private $connectionManager;
14
    private $channel;
15
    private $fromName;
16
    private $queueName;
17
18
    public function __construct($queueName, ConnectionManager $manager, $fromName = '')
19
    {
20
        $this->queueName = $queueName;
21
        $this->fromName = $fromName;
22
        $this->connectionManager = $manager;
23
    }
24
25
    /**
26
     * @param $data
27
     * @param $expire
28
     * @param $priority
29
     */
30
    public function publish($data, $expire = self::DEFAULT_TTL, $priority = self::PRIORITY_LOW)
31
    {
32
        $this->refreshChannel();
33
        $queueBag = new WorkerQueueBag($this->getQueueName());
34
        $this->getChannel()->queueDeclare($queueBag->getQueueDeclare());
35
        $msg = new CmobiAMQPMessage(
36
            (string) $data,
37
            [
38
                'delivery_mode' => 2, // make message persistent
39
                'priority' => $priority,
40
            ]
41
        );
42
        $this->getChannel()->basic_publish($msg, '', $this->getQueueName());
43
44
        $this->getChannel()->close();
45
        $this->connectionManager->getConnection()->close();
46
    }
47
48
    /**
49
     * @return CmobiAMQPChannel
50
     */
51 View Code Duplication
    public function refreshChannel()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52
    {
53
        /** @var CmobiAMQPConnectionInterface $connection */
54
        $connection = $this->connectionManager->getConnection();
55
56
        if (!$connection->isConnected()) {
57
            $connection->reconnect();
58
        }
59
        $this->channel = $connection->channel();
60
61
        return $this->channel;
62
    }
63
64
    /**
65
     * @return string
66
     */
67
    public function getQueueName()
68
    {
69
        return $this->queueName;
70
    }
71
72
    /**
73
     * @return CmobiAMQPChannel
74
     */
75
    public function getChannel()
76
    {
77
        return $this->channel;
78
    }
79
80
    /**
81
     * @return string
82
     */
83
    public function getFromName()
84
    {
85
        return $this->fromName;
86
    }
87
88
    /**
89
     * @return string
90
     */
91
    public function getExchange()
92
    {
93
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type declared by the interface Cmobi\RabbitmqBundle\Que...rInterface::getExchange of type string.

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...
94
    }
95
96
    /**
97
     * @return string
98
     */
99
    public function getExchangeType()
100
    {
101
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type declared by the interface Cmobi\RabbitmqBundle\Que...erface::getExchangeType of type string.

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...
102
    }
103
}
104