Skip to content

CookieAssignmentStore ​

Rasuvaeff\Yii3AbTestingWeb\CookieAssignmentStore

Class · rasuvaeff/yii3-ab-testing-web · Source · implements ConfigurationAwareAssignmentStore, AssignmentStore

Sticky-variant store backed by one bounded signed cookie. Legacy entries are a JSON {experiment: variant} map; configuration-aware entries also carry the core configuration id.

The store is request-scoped: build it from the incoming request with fromRequest(), resolve assignments through it, then write any new variants back with applyToResponse() (typically from a middleware).

The cookie is browser-scoped, so the $subjectId argument of get() / put() is ignored — the cookie itself identifies the subject. A visitor StickyAssignmentMiddleware applies the configured authentication transition when a visitor logs in.

Constructor ​

php
new CookieAssignmentStore(
    Yiisoft\Cookies\CookieSigner $signer,
    string $cookieName = 'ab_variants',
    array $variants = [],
    bool $dirty = false,
    DateInterval $maxAge = new DateInterval(),
    bool $secure = true,
    string $sameSite = Yiisoft\Cookies\Cookie::SAME_SITE_LAX,
    int $maxEntries = 50,
    int $maxCookieBytes = 3800,
    array $configurationIds = [],
)

Methods ​

fromRequest() ​

php
static fromRequest(
    Psr\Http\Message\ServerRequestInterface $request,
    Yiisoft\Cookies\CookieSigner $signer,
    string $cookieName = 'ab_variants',
    int $maxEntries = 50,
    int $maxCookieBytes = 3800,
): self

Builds a store from the signed cookie on the request. A missing, tampered, or malformed cookie yields an empty (clean) store.

get() ​

php
get(string $experiment, string $subjectId): ?string

put() ​

php
put(
    string $experiment,
    string $subjectId,
    string $variant,
): void

getForConfiguration() ​

php
getForConfiguration(
    string $experiment,
    string $subjectId,
    ?string $configurationId,
): ?string

putForConfiguration() ​

php
putForConfiguration(
    string $experiment,
    string $subjectId,
    string $variant,
    ?string $configurationId,
): void

prune() ​

php
prune(ExperimentRegistry $registry): void

Drops stored variants of experiments that no longer exist, so entries of removed experiments do not ride along in the cookie for its whole max-age (and the cookie stays clear of the 4 KB browser limit). Call before applyToResponse(); the rewrite happens only when something was actually removed (or stored).

applyToResponse() ​

php
applyToResponse(
    Psr\Http\Message\ResponseInterface $response,
): Psr\Http\Message\ResponseInterface

Writes the signed cookie when changed. Entries over configured count or byte limits are evicted oldest-first; updating an entry makes it newest.

applyDeletionToResponse() ​

php
applyDeletionToResponse(
    Psr\Http\Message\ResponseInterface $response,
): Psr\Http\Message\ResponseInterface

Expires the cookie the browser still holds. Used when consent is withdrawn: applyToResponse() cannot do it, because a store built under a denied policy is never dirty and its writes are the very thing consent forbids. The deletion carries the same secure and SameSite attributes the store writes with, since a browser matches on those before it replaces anything, and no signature — an empty value carries nothing to sign.