Add CookieResolver that delegates cookie handling to Meta's parameter builder - #14
Open
loevgaard wants to merge 1 commit into
Open
Add CookieResolver that delegates cookie handling to Meta's parameter builder#14loevgaard wants to merge 1 commit into
loevgaard wants to merge 1 commit into
Conversation
… builder Instead of this SDK (and its consumers) hand-parsing the _fbc/_fbp cookies out of a request, the new Setono\MetaConversionsApi\Cookie namespace hands the raw request to facebook/capi-param-builder-php, Meta's own library for exactly this job. It validates existing cookie values and upgrades them to the current format, builds a new fbc from the fbclid query parameter, generates an fbp when the request has none, and reports which cookies to set on the response. The resolver converts the results into the typed Fbc/Fbp value objects. Fbc::fromString()/Fbp::fromString() remain the strict typed layer: the parameter builder has no public single-value parser and its internal validation is structural only (it passes 'a.b.c.d' through), so it cannot replace them. facebook/capi-param-builder-php becomes a direct dependency (it only ships transitively with facebook/php-business-sdk 26.x, not 25.x). FacebookAds\CookieSettings does not comply with the package's PSR-4 mapping (it is loaded via require_once), so PHPStan scans the file explicitly and the dependency analyser ignores the class.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Follow-up to #13: instead of maintaining our own parsing of what Meta writes into
_fbc/_fbp, the newSetono\MetaConversionsApi\Cookienamespace hands the raw request tofacebook/capi-param-builder-php— Meta's own library for exactly this job — and converts the results into the typedFbc/Fbpvalue objects.The builder validates existing cookie values and upgrades legacy four-segment values to the current five-segment format, builds a new
fbcfromfbclid(also from the referer), generates anfbpwhen the request has none, and derives the cookie domain (pass your domains or anETLDPlus1Resolverto the constructor for multi-domain setups). README section added.Why
fromString()stays hand-writtenThe parameter builder cannot replace the value-object parsing: its parser (
preProcess) is private and request-oriented, and its validation is structural only — it happily passesa.b.c.dthrough as an "existing cookie". So the VO layer keeps the strict format guarantees from #13, and the resolver is where "we stop parsing": a value the builder emits that still cannot be represented as a VO comes back asnull(covered by a test).Notes
facebook/capi-param-builder-php: ^1.3.1— dependency-free (PHP ≥ 7.4). It only ships transitively withfacebook/php-business-sdk26.x, not 25.x, so declaring it directly is required for thelowestmatrix anyway.FacebookAds\CookieSettingsis not PSR-4 autoloadable (lives inmodel/, loaded byParamBuilderviarequire_once; safe at runtime because the builder always loads first). PHPStan gets the file viascanFiles; the dependency analyser ignores the class. Both config entries carry comments.getClientIpAddress()returns the_fbicookie format, which carries a trailing appendix segment — not a plain IP. Left out rather than re-parsing it here.CookieResolverinstead of reading cookies itself.Test plan
CookieResolverTestcases: five-segment pass-through (byte-for-byte), four-segment upgrade + Set-Cookie (max-age 90 days, derived domain),fbcfromfbclid,fbpgeneration, regeneration on invalid appendix,nullfor builder-accepted-but-malformed values, domain list → cookie domain + subdomain indexcomposer normalizeall greenAssertcalls guarding the untyped library's return values — not triggerable as long as the library behaveslowest(business-sdk 25.0.0 + builder 1.3.1) on PHP 8.1 — verified locally (105 tests + PHPStan green); also covered by the CI matrix