Passed
Push — master ( 46729e...9da378 )
by Benjamin
05:02 queued 03:07
created

Publisher   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 19
c 2
b 0
f 0
dl 0
loc 83
ccs 24
cts 24
cp 1
rs 10
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getTransports() 0 3 1
A publish() 0 16 4
A __construct() 0 10 3
A addTransport() 0 3 1
A getMessageValidator() 0 3 1
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 6
    public function __construct(
44
        TransportInterface $transport = null,
45
        MessageValidatorInterface $messageValidator = null
46
    ) {
47 6
        $this->transports = new Set();
48 6
        $this->messageValidator = $messageValidator
49 1
            ?: new DefaultMessageValidator();
50
51 6
        if (null !== $transport) {
52 6
            $this->addTransport($transport);
53
        }
54 6
    }
55
56
    /**
57
     * Publish a message over all defined transports
58
     *
59
     * @param MessageInterface $message
60
     */
61 4
    public function publish(MessageInterface $message)
62
    {
63 4
        if (count($this->transports) == 0) {
64 1
            throw new RuntimeException(
65 1
                "Publisher requires at least one transport"
66
            );
67
        }
68
69 3
        $reason = '';
70 3
        if (!$this->messageValidator->validate($message, $reason)) {
71 1
            throw new RuntimeException("Message is invalid: $reason");
72
        }
73
74 2
        foreach ($this->transports as $transport) {
75
            /* @var $transport TransportInterface */
76 2
            $transport->send($message);
77
        }
78 2
    }
79
80
    /**
81
     * Adds a transport to the publisher.
82
     *
83
     * @param TransportInterface $transport
84
     */
85 6
    public function addTransport(TransportInterface $transport)
86
    {
87 6
        $this->transports->attach($transport);
88 6
    }
89
90
    /**
91
     * Returns all defined transports.
92
     *
93
     * @return TransportInterface[]
94
     */
95 2
    public function getTransports()
96
    {
97 2
        return iterator_to_array($this->transports);
98
    }
99
100
    /**
101
     * Returns the current message validator.
102
     *
103
     * @return MessageValidatorInterface
104
     */
105 1
    public function getMessageValidator()
106
    {
107 1
        return $this->messageValidator;
108
    }
109
}
110