| 107 | | $variantFactory = new $nqClassname(); |
|---|
| 108 | | $variant = null; |
|---|
| 109 | | $cookieName = stubRegistry::getConfig('net.stubbles.websites.variantmanager.cookie.name', 'variant'); |
|---|
| 110 | | if ($this->request->hasValue($cookieName, stubRequest::SOURCE_COOKIE) == true) { |
|---|
| 111 | | $variant = $variantFactory->getVariantsMap()->getEnforcingVariant($this->session, $this->request); |
|---|
| 112 | | if (null == $variant) { |
|---|
| 113 | | $variantName = $this->request->getValidatedValue(new stubPreSelectValidator($variantFactory->getVariantNames()), $cookieName, stubRequest::SOURCE_COOKIE); |
|---|
| 114 | | if (null == $variantName) { |
|---|
| 115 | | $variant = $variantFactory->getVariantByName($variantName); |
|---|
| 116 | | } |
|---|
| 117 | | } |
|---|
| | 113 | $variantFactory = $this->createVariantFactory($nqClassname); |
|---|
| | 114 | if (($variantFactory instanceof stubVariantFactory) == false) { |
|---|
| | 115 | throw new stubVariantConfigurationException('Configured variant factory is not an instance of net.stubbles.websites.variantmanager.stubVariantFactory.'); |
|---|
| | 116 | } |
|---|
| | 117 | |
|---|
| | 118 | $variant = null; |
|---|
| | 119 | $cookieName = stubRegistry::getConfig('net.stubbles.websites.variantmanager.cookie.name', 'variant'); |
|---|
| | 120 | if ($variantFactory->getVariantsMap()->shouldUsePersistence() == true) { |
|---|
| | 121 | $variant = $this->getVariantFromCookie($variantFactory, $cookieName); |
|---|
| 125 | | $expiring = stubRegistry::getConfig('net.stubbles.websites.variantmanager.cookie.expiring', (86400 * 90)); // 90 days default |
|---|
| 126 | | $this->response->setCookie(stubCookie::create($cookieName, $variant->getName())->expiringAt(time() + $expiring)->forPath('/')); |
|---|
| | 129 | $expiring = stubRegistry::getConfig('net.stubbles.websites.variantmanager.cookie.expiring', (86400 * 90)); // 90 days default |
|---|
| | 130 | $cookieURL = stubRegistry::getConfig('net.stubbles.websites.variantmanager.cookie.url', null); |
|---|
| | 131 | $cookiePath = stubRegistry::getConfig('net.stubbles.websites.variantmanager.cookie.path', '/'); |
|---|
| | 132 | $this->response->setCookie($this->createCookie($cookieName, $variant->getName(), $expiring, $cookieURL, $cookiePath)); |
|---|
| | 133 | } |
|---|
| | 134 | |
|---|
| | 135 | /** |
|---|
| | 136 | * creates the variant factory |
|---|
| | 137 | * |
|---|
| | 138 | * @param string $classname classname of the variant factory to create |
|---|
| | 139 | * @return stubVariantFactory |
|---|
| | 140 | */ |
|---|
| | 141 | protected function createVariantFactory($classname) |
|---|
| | 142 | { |
|---|
| | 143 | return new $classname(); |
|---|
| | 144 | } |
|---|
| | 145 | |
|---|
| | 146 | /** |
|---|
| | 147 | * tries to get the variant from the cookie |
|---|
| | 148 | * |
|---|
| | 149 | * @param stubVariantFactory $variantFactory the variant factory to use |
|---|
| | 150 | * @param string $cookieName name of the cookie |
|---|
| | 151 | * @return stubVariant |
|---|
| | 152 | */ |
|---|
| | 153 | protected function getVariantFromCookie(stubVariantFactory $variantFactory, $cookieName) |
|---|
| | 154 | { |
|---|
| | 155 | if ($this->request->hasValue($cookieName, stubRequest::SOURCE_COOKIE) == false) { |
|---|
| | 156 | return null; |
|---|
| | 157 | } |
|---|
| | 158 | |
|---|
| | 159 | $variantName = $this->request->getValidatedValue(new stubPreSelectValidator($variantFactory->getVariantNames()), $cookieName, stubRequest::SOURCE_COOKIE); |
|---|
| | 160 | if (null == $variantName) { |
|---|
| | 161 | return null; |
|---|
| | 162 | } |
|---|
| | 163 | |
|---|
| | 164 | $cookieVariant = $variantFactory->getVariantByName($variantName); |
|---|
| | 165 | $enforcingVariant = $variantFactory->getVariantsMap()->getEnforcingVariant($this->session, $this->request); |
|---|
| | 166 | if (null == $enforcingVariant) { |
|---|
| | 167 | return $cookieVariant; |
|---|
| | 168 | } |
|---|
| | 169 | |
|---|
| | 170 | if (substr($cookieVariant->getName(), 0, strlen($enforcingVariant->getName())) == $enforcingVariant->getName()) { |
|---|
| | 171 | return $enforcingVariant; |
|---|
| | 172 | } |
|---|
| | 173 | |
|---|
| | 174 | return $cookieVariant; |
|---|
| | 175 | } |
|---|
| | 176 | |
|---|
| | 177 | /** |
|---|
| | 178 | * creates the cookie |
|---|
| | 179 | * |
|---|
| | 180 | * @param string $cookieName |
|---|
| | 181 | * @param string $variantName |
|---|
| | 182 | * @param int $expiring |
|---|
| | 183 | * @param string $cookieURL |
|---|
| | 184 | * @param string $cookiePath |
|---|
| | 185 | * @return stubCookie |
|---|
| | 186 | */ |
|---|
| | 187 | protected function createCookie($cookieName, $variantName, $expiring, $cookieURL, $cookiePath) |
|---|
| | 188 | { |
|---|
| | 189 | return stubCookie::create($cookieName, $variantName)->expiringAt(time() + $expiring)->forPath($cookiePath)->forDomain($cookieURL); |
|---|