Task::getExchange()   A
last analyzed

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
use Cmobi\RabbitmqBundle\Transport\Exception\QueueNotFoundException;
11
12
class Task implements QueueProducerInterface
13
{
14
    private $connectionManager;
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 int $expire
28
     * @param int $priority
29
     * @throws QueueNotFoundException
30
     * @throws \Cmobi\RabbitmqBundle\Connection\Exception\NotFoundAMQPConnectionFactoryException
31
     */
32
    public function publish($data, $expire = self::DEFAULT_TTL, $priority = self::PRIORITY_LOW)
33
    {
34
        /** @var CmobiAMQPConnectionInterface $connection */
35
        $connection = $this->connectionManager->getConnection();
36
        $channel = $connection->channel();
37
38
        if (! $this->queueHasExists($channel)) {
39
            throw new QueueNotFoundException("Queue $this->queueName not declared.");
40
        }
41
        $queueBag = new WorkerQueueBag($this->getQueueName());
42
        $channel->queueDeclare($queueBag->getQueueDeclare());
43
        $msg = new CmobiAMQPMessage(
44
            (string) $data,
45
            [
46
                'delivery_mode' => 2, // make message persistent
47
                'priority' => $priority,
48
            ]
49
        );
50
        $channel->basic_publish($msg, '', $this->getQueueName());
51
52
        $channel->close();
53
        $connection->close();
54
    }
55
56
    /**
57
     * @param CmobiAMQPChannel $channel
58
     * @return bool
59
     */
60 View Code Duplication
    public function queueHasExists(CmobiAMQPChannel $channel)
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...
61
    {
62
        try {
63
            $channel->queue_declare($this->queueName, true);
64
        } catch (\Exception $e) {
65
            return false;
66
        }
67
68
        return true;
69
    }
70
71
    /**
72
     * @return string
73
     */
74
    public function getQueueName()
75
    {
76
        return $this->queueName;
77
    }
78
79
    /**
80
     * @return string
81
     */
82
    public function getFromName()
83
    {
84
        return $this->fromName;
85
    }
86
87
    /**
88
     * @return string
89
     */
90
    public function getExchange()
91
    {
92
        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...
93
    }
94
95
    /**
96
     * @return string
97
     */
98
    public function getExchangeType()
99
    {
100
        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...
101
    }
102
}
103