Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
CounterFactory
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
3 / 3
3
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 createAll
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 create
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See thes
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 *
17 * @file
18 */
19
20namespace MediaWiki\Extension\WikimediaEditorTasks;
21
22use MediaWiki\Storage\NameTableStore;
23
24/**
25 * Factory for creating Counter objects based on the extension configuration.
26 * Counters definitions contain three keys:
27 * * class (required) - the class implementing the counter
28 * * counter_key (required) - string to identify the counter in the DB
29 * * target_count (optional) - count required to trigger some behavior
30 */
31class CounterFactory {
32
33    /** @var CounterDao */
34    private $dao;
35
36    /** @var NameTableStore */
37    private $nameTableStore;
38
39    /** @var bool */
40    private $editStreaksEnabled;
41
42    /** @var bool */
43    private $revertCountsEnabled;
44
45    /**
46     * @param CounterDao $dao
47     * @param NameTableStore $nameTableStore store for the wikimedia_editor_tasks_keys table
48     * @param bool $editStreaksEnabled
49     * @param bool $revertCountsEnabled
50     */
51    public function __construct(
52        CounterDao $dao,
53        NameTableStore $nameTableStore,
54        bool $editStreaksEnabled,
55        bool $revertCountsEnabled
56    ) {
57        $this->dao = $dao;
58        $this->nameTableStore = $nameTableStore;
59        $this->editStreaksEnabled = $editStreaksEnabled;
60        $this->revertCountsEnabled = $revertCountsEnabled;
61    }
62
63    /**
64     * @param array $config array of counter definitions
65     * @return Counter[] array of Counters
66     */
67    public function createAll( $config ) {
68        return array_map( function ( $definition ) {
69            return $this->create( $definition );
70        }, $config );
71    }
72
73    /**
74     * @param array $definition counter definition
75     * @return Counter
76     */
77    public function create( $definition ) {
78        return new $definition['class'](
79            $this->nameTableStore->acquireId( $definition['counter_key'] ),
80            $this->dao,
81            $this->editStreaksEnabled,
82            $this->revertCountsEnabled
83        );
84    }
85
86}