Passed
Push — master ( 2ae7fe...46729e )
by Benjamin
07:20 queued 05:43
created

Publisher::getMessageValidator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
3
/*
4
 * This file is part of the php-gelf package.
5
 *
6
 * (c) Benjamin Zikarsky <http://benjamin-zikarsky.de>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Gelf;
13
14
use Gelf\Transport\TransportInterface;
15
use Gelf\MessageValidator as DefaultMessageValidator;
16
use SplObjectStorage as Set;
17
use RuntimeException;
18
19
/**
20
 * A GELF publisher functions as a hub for pushing out a GELF message
21
 * to a least one GELF endpoint
22
 *
23
 * @author Benjamin Zikarsky <[email protected]>
24
 */
25
class Publisher implements PublisherInterface
26
{
27
    /**
28
     * @var Set
29
     */
30
    protected $transports;
31
32
    /**
33
     * @var MessageValidatorInterface
34
     */
35
    protected $messageValidator;
36
37
    /**
38
     * Creates a Publisher for GELF-messages.
39
     *
40
     * @param TransportInterface|null         $transport
41
     * @param MessageValidatorInterface|null  $messageValidator
42
     */
43
    public function __construct(
44
        TransportInterface $transport = null,
45
        MessageValidatorInterface $messageValidator = null
46
    ) {
47
        $this->transports = new Set();
48
        $this->messageValidator = $messageValidator
49
            ?: new DefaultMessageValidator();
50
51
        if (null !== $transport) {
52
            $this->addTransport($transport);
53
        }
54
    }
55
56
    /**
57
     * Publish a message over all defined transports
58
     *
59
     * @param MessageInterface $message
60
     */
61
    public function publish(MessageInterface $message)
62
    {
63
        if (count($this->transports) == 0) {
64
            throw new RuntimeException(
65
                "Publisher requires at least one transport"
66
            );
67
        }
68
69
        $reason = '';
70
        if (!$this->messageValidator->validate($message, $reason)) {
71
            throw new RuntimeException("Message is invalid: $reason");
72
        }
73
74
        foreach ($this->transports as $transport) {
75
            /* @var $transport TransportInterface */
76
            $transport->send($message);
77
        }
78
    }
79
80
    /**
81
     * Adds a transport to the publisher.
82
     *
83
     * @param TransportInterface $transport
84
     */
85
    public function addTransport(TransportInterface $transport)
86
    {
87
        $this->transports->attach($transport);
88
    }
89
90
    /**
91
     * Returns all defined transports.
92
     *
93
     * @return TransportInterface[]
94
     */
95
    public function getTransports()
96
    {
97
        return iterator_to_array($this->transports);
98
    }
99
100
    /**
101
     * Returns the current message validator.
102
     *
103
     * @return MessageValidatorInterface
104
     */
105
    public function getMessageValidator()
106
    {
107
        return $this->messageValidator;
108
    }
109
}
110