Passed
Pull Request — master (#661)
by
unknown
13:38 queued 06:33
created

Producer::validateMessage()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 8
c 0
b 0
f 0
nc 9
nop 1
dl 0
loc 16
ccs 0
cts 9
cp 0
crap 30
rs 9.6111
1
<?php
2
3
namespace OldSound\RabbitMqBundle\RabbitMq;
4
5
use Exception;
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 $validatorCheck = false;
20
    public $validatorFile = "";
21
22
    public function setValidatorFile($validatorFile){
23
        $this->validatorFile = $validatorFile;
24
    }
25
26
    public function setValidatorCheck($validatorCheck)
27
    {
28
        $this->validatorCheck = $validatorCheck;
29
    }
30
31
    public function setContentType($contentType)
32
    {
33
        $this->contentType = $contentType;
34
35
        return $this;
36
    }
37
38
    public function setDeliveryMode($deliveryMode)
39
    {
40
        $this->deliveryMode = $deliveryMode;
41
42
        return $this;
43
    }
44
45
    public function setDefaultRoutingKey($defaultRoutingKey)
46
    {
47
        $this->defaultRoutingKey = $defaultRoutingKey;
48
49
        return $this;
50
    }
51
52
    protected function getBasicProperties()
53
    {
54
        return array('content_type' => $this->contentType, 'delivery_mode' => $this->deliveryMode);
55
    }
56
57
    public function validateMessage($msg)
58
    {
59
        if (!array_key_exists($this->contentType, $this->validatorFile)){
0 ignored issues
show
Bug introduced by
$this->validatorFile of type string is incompatible with the type ArrayObject|array expected by parameter $array of array_key_exists(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

59
        if (!array_key_exists($this->contentType, /** @scrutinizer ignore-type */ $this->validatorFile)){
Loading history...
60
            throw new Exception('Cannot find validator file of ' . $this->contentType);
61
        }
62
        // Insert new validator here
63
        if ($this->contentType == 'application/json'){
64
            $validatorEngine = new JsonValidator();
65
        }
66
67
        if ($this->contentType == 'application/xml'){
68
            $validatorEngine = new xmlValidator();
69
        }
70
        
71
        if (!$validatorEngine->isValid($msg, $this->validatorFile[$this->contentType])){
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $validatorEngine does not seem to be defined for all execution paths leading up to this point.
Loading history...
72
            throw new Exception($this->contentType . " message verification failed");
73
        }
74
75
    }
76
77
    /**
78
     * Publishes the message and merges additional properties with basic properties
79
     *
80
     * @param string $msgBody
81
     * @param string $routingKey
82
     * @param array $additionalProperties
83
     * @param array $headers
84
     */
85
    public function publish($msgBody, $routingKey = null, $additionalProperties = array(), array $headers = null)
86
    {
87
        if ($this->validatorCheck){
88
            $this->validateMessage($msgBody);
89
        }
90
91
        if ($this->autoSetupFabric) {
92
            $this->setupFabric();
93
        }
94
95
        $msg = new AMQPMessage((string) $msgBody, array_merge($this->getBasicProperties(), $additionalProperties));
96
97
        if (!empty($headers)) {
98
            $headersTable = new AMQPTable($headers);
99
            $msg->set('application_headers', $headersTable);
100
        }
101
102
        $real_routingKey = $routingKey !== null ? $routingKey : $this->defaultRoutingKey;
103
        $this->getChannel()->basic_publish($msg, $this->exchangeOptions['name'], (string)$real_routingKey);
104
        $this->logger->debug('AMQP message published', array(
105
            'amqp' => array(
106
                'body' => $msgBody,
107
                'routingkeys' => $routingKey,
108
                'properties' => $additionalProperties,
109
                'headers' => $headers
110
            )
111
        ));
112
    }
113
}
114