*/ private const REL_TOKENS = array( 'nofollow', 'sponsored', 'noopener', 'noreferrer' ); /** * `target` attribute value stamped on tracked-URL results. */ private const TARGET_BLANK = '_blank'; /** * Provider-level credentials used when the caller does not pass * a per-request affiliate context. */ private ?YahooShoppingCredentials $credentials; /** * @param YahooShoppingCredentials|null $credentials Optional configured credentials. */ public function __construct( ?YahooShoppingCredentials $credentials = null ) { $this->credentials = $credentials; } /** * Mint an {@see AffiliateLinkResult} for the given snapshot * and affiliate context. * * Lifecycle: * * 1. Verify the snapshot belongs to this provider per * {@see self::PROVIDER_KEY}. A foreign-provider snapshot * raises {@see ProviderRequestException} carrying the * supplied and expected provider keys for diagnosis. * * 2. When both {@see AffiliateContext::sid()} and * {@see AffiliateContext::pid()} are configured AND the * snapshot's detail URL is non-empty, synthesize a * fresh Value Commerce redirect URL. * * 3. When the snapshot already carries a pre-minted * affiliate URL and the context's sid/pid pair is not * both present, forward the pre-minted URL verbatim. * * 4. Otherwise, fall back to * {@see AffiliateLinkResult::passthrough()} on the * snapshot's detail URL with an administrator-facing * warning. * * 5. Stamp the canonical `rel` token set on tracked * results; the passthrough route inherits the empty * `rel` list from {@see AffiliateLinkResult::passthrough()}. * * @param ProductSnapshot $snapshot Snapshot whose detail URL * should be tracked. * @param AffiliateContext $context Per-request affiliate * context. * * @throws ProviderRequestException When `$snapshot` belongs to * a provider other than * {@see self::PROVIDER_KEY}. */ public function build( ProductSnapshot $snapshot, AffiliateContext $context ): AffiliateLinkResult { if ( $snapshot->provider() !== self::PROVIDER_KEY ) { throw new ProviderRequestException( self::PROVIDER_KEY, sprintf( 'ProductSnapshot belongs to provider "%s", not "%s".', $snapshot->provider(), self::PROVIDER_KEY ), array( 'feature' => 'affiliate_link', 'expected_provider' => self::PROVIDER_KEY, 'actual_provider' => $snapshot->provider(), ) ); } $sid = $context->sid(); $pid = $context->pid(); $detailUrl = $snapshot->detailUrl(); if ( ( $sid === null || $pid === null ) && $this->credentials !== null && $this->credentials->hasAffiliateCredentials() ) { $sid = $this->credentials->affiliateSid(); $pid = $this->credentials->affiliatePid(); } // Route 1: context-supplied sid+pid pair wins. Synthesize // a fresh Value Commerce redirect URL targeting the // snapshot's detail URL. if ( $sid !== null && $pid !== null && $detailUrl !== '' ) { $redirectUrl = self::isValueCommerceRedirectUrl( $detailUrl ) ? $detailUrl : self::buildRedirectUrl( $sid, $pid, $detailUrl ); return new AffiliateLinkResult( $redirectUrl, self::REL_TOKENS, self::TARGET_BLANK, true, array() ); } // Route 2: no context-supplied id pair, but the snapshot // carries a pre-minted affiliate URL from a prior sync. // Forward it verbatim. $snapshotAffiliateUrl = $snapshot->affiliateUrl(); if ( $snapshotAffiliateUrl !== null ) { return new AffiliateLinkResult( $snapshotAffiliateUrl, self::REL_TOKENS, self::TARGET_BLANK, true, array() ); } // Route 3: no tracking applies. Fall back to a passthrough // result on the detail URL and warn the administrator so // the diagnostics surface can prompt configuration. return new AffiliateLinkResult( $detailUrl, self::REL_TOKENS, null, false, array( 'Yahoo!ショッピング affiliate sid/pid is not configured; ' . 'falling back to the unaffiliated detail URL.', ) ); } /** * Build a Value Commerce affiliate referral base URL for the * supplied sid / pid pair. * * The redirect URL has the shape: * * https://ck.jp.ap.valuecommerce.com/servlet/referral? * sid=&pid=&vc_url= * * The sid / pid values are URL-encoded so any embedded special * characters round-trip correctly. * * @param string $sid Value Commerce site identifier. * @param string $pid Value Commerce program / placement id. */ public static function buildReferralBaseUrl( string $sid, string $pid ): string { return sprintf( '%s?sid=%s&pid=%s&vc_url=', self::REDIRECT_ENDPOINT, rawurlencode( $sid ), rawurlencode( $pid ) ); } /** * Report whether a URL already points at the ValueCommerce referral endpoint. * * @param string $url URL to inspect. */ public static function isValueCommerceRedirectUrl( string $url ): bool { return preg_match( '#\Ahttps?://ck\.jp\.ap\.valuecommerce\.com/servlet/referral(?:\?|$)#i', $url ) === 1; } /** * Build a Value Commerce affiliate redirect URL for a destination URL. * * @param string $sid Value Commerce site identifier. * @param string $pid Value Commerce program / placement id. * @param string $destinationUrl Detail URL to redirect to. */ private static function buildRedirectUrl( string $sid, string $pid, string $destinationUrl ): string { return sprintf( '%s%s', self::buildReferralBaseUrl( $sid, $pid ), rawurlencode( $destinationUrl ) ); } }