Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 42
0.00% covered (danger)
0.00%
0 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
Channel
0.00% covered (danger)
0.00%
0 / 42
0.00% covered (danger)
0.00%
0 / 9
650
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 __get
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 publish
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 1
272
 history
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getPath
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getCipherParams
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getOptions
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setOptions
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2namespace Ably;
3
4use Ably\Exceptions\AblyException;
5use Ably\Models\ChannelOptions;
6use Ably\Models\Message;
7use Ably\Models\PaginatedResult;
8
9/**
10 * Represents a channel
11 * @property-read Presence $presence Presence object for this channel
12 * @method publish(Message $message)
13 * @method publish(string $name, string $data)
14 */
15class Channel {
16
17    private $name;
18    private $channelPath;
19    private $ably;
20    private $presence;
21    /**
22     * @var Ably\Models\ChannelOptions
23     */
24    public $options;
25
26    /**
27     * Constructor
28     * @param AblyRest $ably Ably API instance
29     * @param string $name Channel's name
30     * @param ChannelOptions|array|null $options Channel options (for encrypted channels)
31     * @throws AblyException
32     */
33    public function __construct( AblyRest $ably, $name, $options = [] ) {
34        $this->ably = $ably;
35        $this->name = $name;
36        $this->channelPath = "/channels/" . urlencode( $name );
37        $this->presence = new Presence( $ably, $this );
38
39        $this->setOptions( $options );
40    }
41
42    /**
43     * Magic getter for the $presence property
44     */
45    public function __get( $name ) {
46        if ($name == 'presence') {
47            return $this->presence;
48        }
49
50        throw new AblyException( 'Undefined property: '.__CLASS__.'::'.$name );
51    }
52
53    /**
54     * Posts a message to this channel
55     * @param mixed ... Either a Message, array of Message-s, or (string eventName, string data, [string clientId])
56     * @throws \Ably\Exceptions\AblyException
57     */
58    public function publish() {
59
60        $args = func_get_args();
61        $json = '';
62        
63        if ( count($args) == 1 && is_a( $args[0], 'Ably\Models\Message' ) ) { // single Message
64            $msg = $args[0];
65
66            if ( $this->options->cipher ) {
67                $msg->setCipherParams( $this->options->cipher );
68            }
69
70            $json = $msg->toJSON();
71        } else if ( count($args) == 1 && is_array( $args[0] ) ) { // array of Messages
72            $jsonArray = [];
73
74            foreach ( $args[0] as $msg ) {
75                if ( $this->options->cipher ) {
76                    $msg->setCipherParams( $this->options->cipher );
77                }
78
79                $jsonArray[] = $msg->toJSON();
80            }
81            
82            $json = '[' . implode( ',', $jsonArray ) . ']';
83        } else if ( count($args) >= 2 && count($args) <= 3 ) { // eventName, data[, clientId]
84            $msg = new Message();
85            $msg->name = $args[0];
86            $msg->data = $args[1];
87            if ( count($args) == 3 ) $msg->clientId = $args[2];
88
89            if ( $this->options->cipher ) {
90                $msg->setCipherParams( $this->options->cipher );
91            }
92
93            $json = $msg->toJSON();
94        } else {
95            throw new AblyException( 'Wrong parameters provided, use either Message, array of Messages, or name and data', 40003, 400 );
96        }
97        
98        $authClientId = $this->ably->auth->clientId;
99        // if the message has a clientId set and we're using token based auth, the clientIds must match unless we're a wildcard client
100        if ( !empty( $msg->clientId ) && !$this->ably->auth->isUsingBasicAuth() && $authClientId != '*' && $msg->clientId != $authClientId) {
101            throw new AblyException( 'Message\'s clientId does not match the clientId of the authorisation token.', 40102, 401 );
102        }
103
104        $this->ably->post( $this->channelPath . '/messages', $headers = [], $json );
105        return true;
106    }
107
108    /**
109     * Retrieves channel's history of messages
110     * @param array $params Parameters to be sent with the request
111     * @return PaginatedResult
112     */
113    public function history( $params = [] ) {
114        return new PaginatedResult( $this->ably, 'Ably\Models\Message', $this->getCipherParams(), 'GET', $this->getPath() . '/messages', $params );
115    }
116
117    /**
118     * @return string Channel's name
119     */
120    public function getName() {
121        return $this->name;
122    }
123
124    /**
125     * @return string Channel portion of the request URI
126     */
127    public function getPath() {
128        return $this->channelPath;
129    }
130
131    /**
132     * @return CipherParams|null Cipher params if the channel is encrypted
133     */
134    public function getCipherParams() {
135        return $this->options->cipher;
136    }
137
138    /**
139     * @return \Ably\Models\ChannelOptions
140     */
141    public function getOptions() {
142        return $this->options;
143    }
144
145    /**
146     * Sets channel options
147     * @param array|null $options channel options
148     * @throws AblyException
149     */
150    public function setOptions( $options = [] ) {
151        $this->options = new ChannelOptions( $options );
152    }
153}