Merge "Special:Newpages feed now shows first revision instead of latest revision"
[lhc/web/wiklou.git] / includes / libs / stats / BufferingStatsdDataFactory.php
index 9c18b10..73c6a8f 100644 (file)
@@ -32,8 +32,17 @@ use Liuggio\StatsdClient\Factory\StatsdDataFactory;
  *
  * @since 1.25
  */
-class BufferingStatsdDataFactory extends StatsdDataFactory {
+class BufferingStatsdDataFactory extends StatsdDataFactory implements IBufferingStatsdDataFactory {
        protected $buffer = [];
+       /**
+        * Collection enabled?
+        * @var bool
+        */
+       protected $enabled = true;
+       /**
+        * @var string
+        */
+       private $prefix;
 
        public function __construct( $prefix ) {
                parent::__construct();
@@ -49,6 +58,7 @@ class BufferingStatsdDataFactory extends StatsdDataFactory {
         *
         * @param string $key
         * @since 1.26
+        * @return string
         */
        private static function normalizeMetricKey( $key ) {
                $key = preg_replace( '/[:.]+/', '.', $key );
@@ -61,6 +71,9 @@ class BufferingStatsdDataFactory extends StatsdDataFactory {
                $key, $value = 1, $metric = StatsdDataInterface::STATSD_METRIC_COUNT
        ) {
                $entity = $this->produceStatsdDataEntity();
+               if ( !$this->enabled ) {
+                       return $entity;
+               }
                if ( $key !== null ) {
                        $key = self::normalizeMetricKey( "{$this->prefix}.{$key}" );
                        $entity->setKey( $key );
@@ -79,9 +92,35 @@ class BufferingStatsdDataFactory extends StatsdDataFactory {
        }
 
        /**
+        * @deprecated Use getData()
         * @return StatsdData[]
         */
        public function getBuffer() {
                return $this->buffer;
        }
+
+       /**
+        * Check whether this data factory has any data.
+        * @return boolean
+        */
+       public function hasData() {
+               return !empty( $this->buffer );
+       }
+
+       /**
+        * Return data from the factory.
+        * @return StatsdData[]
+        */
+       public function getData() {
+               return $this->buffer;
+       }
+
+       /**
+        * Set collection enable status.
+        * @param bool $enabled Will collection be enabled?
+        * @return void
+        */
+       public function setEnabled( $enabled ) {
+               $this->enabled = $enabled;
+       }
 }