Debian Codegraph @

skins/Vector/includes/FeatureManagement/Requirements/ABRequirement.php

raw symbols SCIP
1 <?php
2
3 /**
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
18 *
19 * @file
20 * @since 1.35
21 */
22
23 namespace MediaWiki\Skins\Vector\FeatureManagement\Requirements;
24
25 use MediaWiki\Config\Config;
26 use MediaWiki\Skins\Vector\FeatureManagement\Requirement;
27 use MediaWiki\User\UserIdentity;
28
29 /**
30 * @package MediaWiki\Skins\Vector\FeatureManagement\Requirements
31 * @internal
32 */
33 class ABRequirement implements Requirement {
34 private Config $config;
35
36 private UserIdentity $user;
37
38 /**
39 * The name of the experiment
40 */
41 private string $experimentName;
42
43 /**
44 * The name of the requirement
45 */
46 private string $name;
47
48 /**
49 * @param Config $config
50 * @param UserIdentity $user
51 * @param string $experimentName The name of the experiment
52 * @param string|null $name The name of the requirement
53 */
54 public function __construct(
55 Config $config,
56 UserIdentity $user,
57 string $experimentName,
58 ?string $name = null
59 ) {
60 $this->config = $config;
61 $this->user = $user;
62 $this->experimentName = $experimentName;
63 $this->name = $name ?? '';
64 }
65
66 /**
67 * @inheritDoc
68 */
69 public function getName(): string {
70 return $this->name;
71 }
72
73 /**
74 * Returns true if the user is logged-in and false otherwise.
75 *
76 * @inheritDoc
77 */
78 public function isMet(): bool {
79 // Get the experiment configuration from the config object.
80 $experiment = $this->config->get( 'VectorWebABTestEnrollment' );
81
82 // Use the local user ID directly
83 $id = $this->user->getId();
84
85 // Check if the experiment is not enabled or does not match the specified name.
86 if ( !$experiment['enabled'] || $experiment['name'] !== $this->experimentName ) {
87 // If the experiment is not enabled or does not match the specified name,
88 // return true, indicating that the metric is "met"
89 return true;
90 } else {
91 // If the experiment is enabled and matches the specified name,
92 // calculate the user's variant based on their user ID
93 $variant = $id % 2;
94
95 // Cast the variant value to a boolean and return it, indicating whether
96 // the user is in the "control" or "test" group.
97 return (bool)$variant;
98 }
99 }
100 }