flexible-shipping
/
vendor_prefixed
/
wpdesk
/
wp-abtesting
/
src
/
ABTest
/
EqualGroupsRandomABTest.php
EqualGroupsRandomABTest.php
104 lines
| 1 | <?php |
| 2 | |
| 3 | namespace FSVendor\WPDesk\ABTesting\ABTest; |
| 4 | |
| 5 | use FSVendor\WPDesk\ABTesting\ABTest; |
| 6 | use FSVendor\WPDesk\Persistence\ElementNotExistsException; |
| 7 | use FSVendor\WPDesk\Persistence\PersistentContainer; |
| 8 | /** |
| 9 | * Base class for AB tests with equal groups generated by random number generator |
| 10 | * |
| 11 | * @package WPDesk\ABTesting\ABTest |
| 12 | */ |
| 13 | abstract class EqualGroupsRandomABTest implements ABTest |
| 14 | { |
| 15 | const CONTAINER_VALUE_PREFIX = 'ab_test'; |
| 16 | /** |
| 17 | * Used to save info about generated variant |
| 18 | * |
| 19 | * @var PersistentContainer |
| 20 | */ |
| 21 | private $container; |
| 22 | /** |
| 23 | * How many groups/variants are in the whole test |
| 24 | * |
| 25 | * @var int |
| 26 | */ |
| 27 | private $variant_count; |
| 28 | /** |
| 29 | * Some unique test name for persitence pusposes and for higher readability |
| 30 | * |
| 31 | * @var string |
| 32 | */ |
| 33 | private $test_name; |
| 34 | /** |
| 35 | * Derived classes can use this value to know what variant id is generated |
| 36 | * |
| 37 | * @var int |
| 38 | */ |
| 39 | protected $current_variant_id; |
| 40 | /** |
| 41 | * @param int $variant_count Number of equal size groups to generate. |
| 42 | * @param string $test_name Specific test name. Should be unique. Used to persist. |
| 43 | * @param PersistentContainer $container Container to persist data. |
| 44 | */ |
| 45 | public function __construct($variant_count, $test_name, PersistentContainer $container) |
| 46 | { |
| 47 | assert(is_int($variant_count) && $variant_count > 0, '$variant_count makes no sense'); |
| 48 | $this->variant_count = $variant_count; |
| 49 | $this->test_name = $test_name; |
| 50 | $this->container = $container; |
| 51 | $this->initialize_variant_id(); |
| 52 | } |
| 53 | /** |
| 54 | * Clears info about variant and draws again |
| 55 | */ |
| 56 | public function reset() |
| 57 | { |
| 58 | $this->container->set($this->get_container_key(), null); |
| 59 | $this->initialize_variant_id(); |
| 60 | } |
| 61 | /** |
| 62 | * @return void |
| 63 | */ |
| 64 | private function initialize_variant_id() |
| 65 | { |
| 66 | $variant_id = $this->get_variant_id_from_container(); |
| 67 | if ($variant_id === null) { |
| 68 | $variant_id = $this->generate_variant_id(); |
| 69 | $this->container->set($this->get_container_key(), $variant_id); |
| 70 | } |
| 71 | $this->current_variant_id = $variant_id; |
| 72 | } |
| 73 | /** |
| 74 | * Returns variant id if exists. |
| 75 | * |
| 76 | * @return int|null Returns null if not exists. |
| 77 | */ |
| 78 | private function get_variant_id_from_container() |
| 79 | { |
| 80 | try { |
| 81 | $variant_id = $this->container->get($this->get_container_key()); |
| 82 | return (int) $variant_id; |
| 83 | } catch (ElementNotExistsException $e) { |
| 84 | return null; |
| 85 | } |
| 86 | } |
| 87 | /** |
| 88 | * Key for where variant id should be saved in the persistence container |
| 89 | * |
| 90 | * @return string |
| 91 | */ |
| 92 | protected function get_container_key() |
| 93 | { |
| 94 | return implode('_', [self::CONTAINER_VALUE_PREFIX, $this->test_name, 'variant_id']); |
| 95 | } |
| 96 | /** |
| 97 | * @return int |
| 98 | */ |
| 99 | private function generate_variant_id() |
| 100 | { |
| 101 | return mt_rand(1, $this->variant_count); |
| 102 | } |
| 103 | } |
| 104 |