Passed
Pull Request — master (#661)
by
unknown
17:46 queued 07:49
created

Producer::setValidator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 3
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
3
namespace OldSound\RabbitMqBundle\RabbitMq;
4
5
use OldSound\RabbitMqBundle\RabbitMq\Exception\ValidationException;
6
use PhpAmqpLib\Message\AMQPMessage;
7
use PhpAmqpLib\Wire\AMQPTable;
8
use OldSound\RabbitMqBundle\RabbitMq\JsonValidator;
9
use OldSound\RabbitMqBundle\RabbitMq\XmlValidator;
10
11
/**
12
 * Producer, that publishes AMQP Messages
13
 */
14
class Producer extends BaseAmqp implements ProducerInterface
15
{
16
    protected $contentType = 'text/plain';
17
    protected $deliveryMode = 2;
18
    protected $defaultRoutingKey = '';
19
    public $validator = null;
20
21
    public function setValidator($validator_class, $schema, $definitions)
22
    {
23
        $this->validator = new $validator_class();
24
        $this->validator->setSchema($schema, $definitions);
25
    }
26
27
    public function setContentType($contentType)
28
    {
29
        $this->contentType = $contentType;
30
31
        return $this;
32
    }
33
34
    public function setDeliveryMode($deliveryMode)
35
    {
36
        $this->deliveryMode = $deliveryMode;
37
38
        return $this;
39
    }
40
41
    public function setDefaultRoutingKey($defaultRoutingKey)
42
    {
43
        $this->defaultRoutingKey = $defaultRoutingKey;
44
45
        return $this;
46
    }
47
48
    protected function getBasicProperties()
49
    {
50
        return array('content_type' => $this->contentType, 'delivery_mode' => $this->deliveryMode);
51
    }
52
53
    public function validateMessage($msg)
54
    {
55
        if ($this->contentType != $this->validator->getContentType()) {
56
            throw new ValidationException($this->contentType . " message verification failed");
57
        }
58
        
59
        $error = $this->validator->isValid($msg, $this->contentType);
60
        if ($error != null){
61
            throw new ValidationException($this->contentType . " message verification failed. Error was: " . $error);
62
        }
63
    }
64
65
    /**
66
     * Publishes the message and merges additional properties with basic properties
67
     *
68
     * @param string $msgBody
69
     * @param string $routingKey
70
     * @param array $additionalProperties
71
     * @param array $headers
72
     */
73
    public function publish($msgBody, $routingKey = null, $additionalProperties = array(), array $headers = null)
74
    {
75
        if ($this->validator != null){
76
            $this->validateMessage($msgBody);
77
        }
78
79
        if ($this->autoSetupFabric) {
80
            $this->setupFabric();
81
        }
82
83
        $msg = new AMQPMessage((string) $msgBody, array_merge($this->getBasicProperties(), $additionalProperties));
84
85
        if (!empty($headers)) {
86
            $headersTable = new AMQPTable($headers);
87
            $msg->set('application_headers', $headersTable);
88
        }
89
90
        $real_routingKey = $routingKey !== null ? $routingKey : $this->defaultRoutingKey;
91
        $this->getChannel()->basic_publish($msg, $this->exchangeOptions['name'], (string)$real_routingKey);
92
        $this->logger->debug('AMQP message published', array(
93
            'amqp' => array(
94
                'body' => $msgBody,
95
                'routingkeys' => $routingKey,
96
                'properties' => $additionalProperties,
97
                'headers' => $headers
98
            )
99
        ));
100
    }
101
}
102