Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
75.00% covered (warning)
75.00%
6 / 8
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
ProgressPrinter
75.00% covered (warning)
75.00%
6 / 8
0.00% covered (danger)
0.00%
0 / 1
4.25
0.00% covered (danger)
0.00%
0 / 1
 outputIndented
n/a
0 / 0
n/a
0 / 0
0
 outputProgress
75.00% covered (warning)
75.00%
6 / 8
0.00% covered (danger)
0.00%
0 / 1
4.25
1<?php
2
3namespace CirrusSearch\Maintenance;
4
5/**
6 * Simple trait to help printing progress in a maintenance script.
7 */
8trait ProgressPrinter {
9    private int $lastProgressPrinted = 0;
10
11    /**
12     * @param string $message
13     * @return void
14     */
15    abstract public function outputIndented( $message );
16
17    /**
18     * public because php 5.3 does not support accessing private
19     * methods in a closure.
20     * @param int $docsDumped
21     * @param int $limit
22     */
23    public function outputProgress( int $docsDumped, int $limit ): void {
24        if ( $docsDumped <= 0 ) {
25            return;
26        }
27        $pctDone = (int)( ( $docsDumped / $limit ) * 100 );
28        if ( $this->lastProgressPrinted == $pctDone ) {
29            return;
30        }
31        $this->lastProgressPrinted = $pctDone;
32        if ( ( $pctDone % 2 ) == 0 ) {
33            $this->outputIndented( "    $pctDone% done...\n" );
34        }
35    }
36
37}