Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
94.12% covered (success)
94.12%
16 / 17
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
WebABTestArticleIdFactory
94.12% covered (success)
94.12%
16 / 17
50.00% covered (danger)
50.00%
1 / 2
3.00
0.00% covered (danger)
0.00%
0 / 1
 filterExcludedBucket
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 makeWebABTestArticleIdStrategy
91.67% covered (success)
91.67%
11 / 12
0.00% covered (danger)
0.00%
0 / 1
2.00
1<?php
2
3namespace WikimediaEvents\WebABTest;
4
5use MediaWiki\Context\IContextSource;
6use WikimediaEvents\PageSplitter\PageHashGenerate;
7use WikimediaEvents\PageSplitter\PageSplitterInstrumentation;
8
9/**
10 * Factory class that can be shared as a service so that the
11 * WebABTestArticleIdStrategy can be instantiated easily outside of this
12 * extension.
13 */
14final class WebABTestArticleIdFactory {
15    /**
16     * @param string[] $buckets An array of bucket name strings.
17     * @return string[] Array of bucket name string excluding the `excluded bucket`.
18     */
19    public function filterExcludedBucket( $buckets ): array {
20        return array_values(
21            array_filter( $buckets, static function ( $bucket ) {
22                return $bucket !== WebABTestArticleIdStrategy::EXCLUDED_BUCKET_NAME;
23            }, ARRAY_FILTER_USE_BOTH )
24        );
25    }
26
27    /**
28     * @param string[] $buckets An array of bucket name strings. E.g., ['control',
29     * 'treatment']. If the query param override is suppplied, the returned bucket
30     * will be the index of the buckets array.
31     * @param float $samplingRatio
32     * @param string $overrideName Name of query param override.
33     * @param IContextSource $context
34     * @return ?WebABTestArticleIdStrategy
35     */
36    public function makeWebABTestArticleIdStrategy(
37        array $buckets,
38        float $samplingRatio,
39        string $overrideName,
40        IContextSource $context
41    ): ?WebABTestArticleIdStrategy {
42        $title = $context->getTitle();
43
44        if ( !$title ) {
45            return null;
46        }
47
48        $request = $context->getRequest();
49
50        return new WebABTestArticleIdStrategy(
51            $buckets,
52            $title,
53            $request,
54            $overrideName,
55            new PageSplitterInstrumentation( $samplingRatio, $buckets ),
56            new PageHashGenerate()
57        );
58    }
59}