/**
 * Runs the example.
 * @param AdWordsUser $user the user to run the example with
 * @param string $adGroupId the id to add placements to
 */
function HandlePartialFailuresExample(AdWordsUser $user, $adGroupId)
{
    // Get the service, which loads the required classes. Passing true for the
    // parameter $partialFailure enables partial failure behavior.
    $adGroupCriterionService = $user->GetService('AdGroupCriterionService', ADWORDS_VERSION, null, null, null, true);
    // Create placements.
    $placements = array();
    $placements[] = new Placement('mars.google.com');
    $placements[] = new Placement('bad.url');
    $placements[] = new Placement('venus.google.com');
    $placements[] = new Placement('example.com/invalid<character');
    // Create ad group criteria and operations.
    $operations = array();
    foreach ($placements as $placement) {
        $adGroupCriterion = new BiddableAdGroupCriterion();
        $adGroupCriterion->adGroupId = $adGroupId;
        $adGroupCriterion->criterion = $placement;
        $operation = new AdGroupCriterionOperation();
        $operation->operand = $adGroupCriterion;
        $operation->operator = 'ADD';
        $operations[] = $operation;
    }
    // Make the mutate request.
    $result = $adGroupCriterionService->mutate($operations);
    // Display results.
    foreach ($result->value as $adGroupCriterion) {
        if ($adGroupCriterion->AdGroupCriterionType == 'BiddableAdGroupCriterion') {
            printf("Placement with URL '%s' and ID '%s' was added.\n", $adGroupCriterion->criterion->url, $adGroupCriterion->criterion->id);
        }
    }
    // Check for partial failures.
    if (isset($result->partialFailureErrors)) {
        foreach ($result->partialFailureErrors as $error) {
            $index = ErrorUtils::GetSourceOperationIndex($error);
            if (isset($index)) {
                $adGroupCriterion = $operations[$index]->operand;
                printf("Placement with URL '%s' failed with error '%s'.\n", $adGroupCriterion->criterion->url, $error->errorString);
            } else {
                printf("Operations failed with error '%s'.\n", $error->errorString);
            }
        }
    }
}
/**
 * Runs the example.
 * @param AdWordsUser $user the user to run the example with
 * @param string $adGroupId the id to add keywords to
 */
function HandlePartialFailuresExample(AdWordsUser $user, $adGroupId)
{
    // Get the service, which loads the required classes. Passing true for the
    // parameter $partialFailure enables partial failure behavior.
    $adGroupCriterionService = $user->GetService('AdGroupCriterionService', ADWORDS_VERSION, NULL, NULL, NULL, TRUE);
    // Create keywords.
    $keywords = array();
    $keywords[] = new Keyword('mars cruise', 'BROAD');
    $keywords[] = new Keyword('inv@lid cruise', 'BROAD');
    $keywords[] = new Keyword('venus cruise', 'BROAD');
    $keywords[] = new Keyword('b(a)d keyword cruise', 'BROAD');
    // Create ad group criteria and operations.
    $operations = array();
    foreach ($keywords as $keyword) {
        $adGroupCriterion = new BiddableAdGroupCriterion();
        $adGroupCriterion->adGroupId = $adGroupId;
        $adGroupCriterion->criterion = $keyword;
        $operation = new AdGroupCriterionOperation();
        $operation->operand = $adGroupCriterion;
        $operation->operator = 'ADD';
        $operations[] = $operation;
    }
    // Make the mutate request.
    $result = $adGroupCriterionService->mutate($operations);
    // Display results.
    foreach ($result->value as $adGroupCriterion) {
        if ($adGroupCriterion->AdGroupCriterionType == 'BiddableAdGroupCriterion') {
            printf("Keyword with text '%s', match type '%s', and id '%s' was " . "added.\n", $adGroupCriterion->criterion->text, $adGroupCriterion->criterion->matchType, $adGroupCriterion->criterion->id);
        }
    }
    // Check for partial failures.
    if (isset($result->partialFailureErrors)) {
        foreach ($result->partialFailureErrors as $error) {
            $index = ErrorUtils::GetSourceOperationIndex($error);
            if (isset($index)) {
                $adGroupCriterion = $operations[$index]->operand;
                printf("Keyword with text '%s' and match type '%s' failed with error " . "'%s'.\n", $adGroupCriterion->criterion->text, $adGroupCriterion->criterion->matchType, $error->errorString);
            } else {
                printf("Operations failed with error '%s'.\n", $error->errorString);
            }
        }
    }
}
/**
 * Runs the example.
 * @param AdWordsUser $user the user to run the example with
 * @param string $adGroupId the if the ad group to add the text ads to
 */
function HandlePolicyViolationErrorExample(AdWordsUser $user, $adGroupId)
{
    // Get the service, which loads the required classes.
    $adGroupAdService = $user->GetService('AdGroupAdService', ADWORDS_VERSION);
    // Get validateOnly version of the AdGroupAdService.
    $adGroupAdValidationService = $user->GetService('AdGroupAdService', ADWORDS_VERSION, null, null, true);
    // Create text ad that violates an exemptable policy. This ad will only
    // trigger an error in the production environment.
    $textAd = new TextAd();
    $textAd->headline = 'Mars Cruise !!!';
    $textAd->description1 = 'Visit the Red Planet in style.';
    $textAd->description2 = 'Low-gravity fun for everyone!';
    $textAd->displayUrl = 'www.example.com';
    $textAd->finalUrls = array('http://www.example.com/');
    // Create ad group ad.
    $adGroupAd = new AdGroupAd();
    $adGroupAd->adGroupId = $adGroupId;
    $adGroupAd->ad = $textAd;
    // Create operation.
    $operation = new AdGroupAdOperation();
    $operation->operand = $adGroupAd;
    $operation->operator = 'ADD';
    $operations = array($operation);
    try {
        // Make the mutate request.
        $result = $adGroupAdValidationService->mutate($operations);
    } catch (SoapFault $fault) {
        $errors = ErrorUtils::GetApiErrors($fault);
        if (sizeof($errors) == 0) {
            // Not an API error, so throw fault.
            throw $fault;
        }
        $operationIndicesToRemove = array();
        foreach ($errors as $error) {
            if ($error->ApiErrorType == 'PolicyViolationError') {
                $operationIndex = ErrorUtils::GetSourceOperationIndex($error);
                $operation = $operations[$operationIndex];
                printf("Ad with headline '%s' violated %s policy '%s'.\n", $operation->operand->ad->headline, $error->isExemptable ? 'exemptable' : 'non-exemptable', $error->externalPolicyName);
                if ($error->isExemptable) {
                    // Add exemption request to the operation.
                    printf("Adding exemption request for policy name '%s' on text " . "'%s'.\n", $error->key->policyName, $error->key->violatingText);
                    $operation->exemptionRequests[] = new ExemptionRequest($error->key);
                } else {
                    // Remove non-exemptable operation.
                    print "Removing the operation from the request.\n";
                    $operationIndicesToRemove[] = $operationIndex;
                }
            } else {
                // Non-policy error returned, throw fault.
                throw $fault;
            }
        }
        $operationIndicesToRemove = array_unique($operationIndicesToRemove);
        rsort($operationIndicesToRemove, SORT_NUMERIC);
        if (sizeof($operationIndicesToRemove) > 0) {
            foreach ($operationIndicesToRemove as $operationIndex) {
                unset($operations[$operationIndex]);
            }
        }
    }
    if (sizeof($operations) > 0) {
        // Retry the mutate request.
        $result = $adGroupAdService->mutate($operations);
        // Display results.
        foreach ($result->value as $adGroupAd) {
            printf("Text ad with headline '%s' and ID '%s' was added.\n", $adGroupAd->ad->headline, $adGroupAd->ad->id);
        }
    } else {
        print "All the operations were invalid with non-exemptable errors.\n";
    }
}
 /**
  * Test getting the index of the operation referenced by an error.
  * @param ApiError $error the ApiError to get the operation for
  * @param int $expected the expected operation
  * @covers ErrorUtils::GetSourceOperationIndex
  * @dataProvider SourceOperationIndexProvider
  */
 public function testGetSourceOperationIndex($error, $expected)
 {
     $index = ErrorUtils::GetSourceOperationIndex($error);
     $this->assertEquals($expected, $index);
 }