Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
Stats
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 4
90
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 fromJSON
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
20
 deepCopy
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
12
 clearFields
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2namespace Ably\Models;
3
4/**
5 * Model for stats
6 */
7class Stats {
8    /**
9     * @var \Ably\Models\Stats\MessageTypes $all MessageTypes representing the total of all inbound and
10     *      outbound message traffic. This is the aggregate number that is considered in applying account
11     *      message limits.
12     * @var \Ably\Models\Stats\MessageTraffic $inbound MessageTraffic representing inbound messages
13     *      (ie published by clients and sent inbound to the Ably service) by all transport types.
14     * @var \Ably\Models\Stats\MessageTraffic $outbound MessageTraffic representing outbound messages
15     *      (ie delivered by the Ably service to connected and subscribed clients).
16     * @var \Ably\Models\Stats\MessageTypes $persisted MessageTypes representing the aggregate volume
17     *      of messages persisted.
18     * @var \Ably\Models\Stats\ConnectionTypes $connections ConnectionTypes representing the usage
19     *      of connections.
20     * @var \Ably\Models\Stats\ResourceCount $channels ResourceCount representing the number of channels
21     *       activated and used.
22     * @var \Ably\Models\Stats\RequestCount $apiRequests RequestCount representing the number of requests
23     *      made to the REST API.
24     * @var \Ably\Models\Stats\RequestCount $tokenRequests RequestCount representing the number of requests
25     *      made to issue access tokens.
26     * @var string $intervalId The interval that this statistic applies to.
27     * @var string $intervalGranularity The granularity of the interval for the stat. May be one of values:
28     *      minute, hour, day, month
29     * @var int $intervalTime A timestamp representing the start of the interval.
30     */
31    public $all;
32    public $inbound;
33    public $outbound;
34    public $persisted;
35    public $connections;
36    public $channels;
37    public $apiRequests;
38    public $tokenRequests;
39    public $intervalId;
40    public $intervalGranularity;
41    public $intervalTime;
42
43    public function __construct() {
44        $this->clearFields();
45    }
46    /**
47     * Populates stats from JSON
48     * @param string|stdClass $json JSON string or an already decoded object.
49     * @throws AblyException
50     */
51    public function fromJSON( $json ) {
52        $this->clearFields();
53
54        if (is_object( $json )) {
55            $obj = $json;
56        } else {
57            $obj = @json_decode($json);
58            if (!$obj) {
59                throw new AblyException( 'Invalid object or JSON encoded object' );
60            }
61        }
62
63        // stats are usually wrapped in an array
64        if ( is_array( $obj ) ) {
65            $obj = $obj[0];
66        }
67
68        self::deepCopy( $obj, $this );
69    }
70
71    protected static function deepCopy( $target, $dst ) {
72        foreach ( $target as $key => $value ) {
73            if ( is_object( $value )) {
74                self::deepCopy( $value, $dst->$key );
75            } else {
76                $dst->$key = $value;
77            }
78        }
79    }
80
81    /**
82     * Sets all the public fields to null
83     */
84    public function clearFields() {
85        $this->all                 = new Stats\MessageTypes();
86        $this->inbound             = new Stats\MessageTraffic();
87        $this->outbound            = new Stats\MessageTraffic();
88        $this->persisted           = new Stats\MessageTypes();
89        $this->connections         = new Stats\ConnectionTypes();
90        $this->channels            = new Stats\ResourceCount();
91        $this->apiRequests         = new Stats\RequestCount();
92        $this->tokenRequests       = new Stats\RequestCount();
93        $this->intervalId          = '';
94        $this->intervalGranularity = '';
95        $this->intervalTime        = 0;
96    }
97}