Completed
Pull Request — master (#1936)
by
unknown
317:34 queued 252:33
created

SetCookiePreferencesController   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 30
rs 10
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A index() 0 28 3
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\Fundraising\Frontend\App\Controllers;
6
7
use Symfony\Component\HttpFoundation\Cookie;
8
use Symfony\Component\HttpFoundation\JsonResponse;
9
use Symfony\Component\HttpFoundation\Request;
10
use Symfony\Component\HttpFoundation\Response;
11
use WMDE\Fundraising\Frontend\App\CookieNames;
12
use WMDE\Fundraising\Frontend\Factories\FunFunFactory;
13
use WMDE\Fundraising\Frontend\Infrastructure\TrackingDataSelector;
14
15
class SetCookiePreferencesController {
16
17
	public function index( FunFunFactory $ffFactory, Request $request ): Response {
18
		$cookieConsent = $request->get( CookieNames::CONSENT, 'no' );
19
20
		if ( $cookieConsent === 'yes' ) {
21
			$request->attributes->set( 'trackingCode', TrackingDataSelector::getFirstNonEmptyValue( [
22
				$request->cookies->get( CookieNames::TRACKING ),
23
				TrackingDataSelector::concatTrackingFromVarTuple(
24
					$request->get( 'piwik_campaign', '' ),
25
					$request->get( 'piwik_kwd', '' )
26
				)
27
			] ) );
28
		}
29
30
		$response = JsonResponse::create( [
31
			'status' => 'OK',
32
		] );
33
34
		$response->headers->setCookie( $ffFactory->getCookieBuilder()->newCookie(
35
			CookieNames::CONSENT,
36
			$cookieConsent
37
		) );
38
39
		if ( $cookieConsent === 'no' ) {
40
			$response->headers->clearCookie( CookieNames::TRACKING );
41
			$response->headers->clearCookie( CookieNames::BUCKET_TESTING );
42
		}
43
44
		return $response;
45
	}
46
}
47