Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Activate existing key-values #10

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions app/AdManager/KeyManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

require __DIR__.'/../../vendor/autoload.php';

use Google\AdsApi\AdManager\v201811\ActivateCustomTargetingKeys;
use Google\AdsApi\AdManager\v201811\CustomTargetingKey;
use Google\AdsApi\AdManager\v201811\CustomTargetingKeyType;
use Google\AdsApi\AdManager\Util\v201811\StatementBuilder;
Expand All @@ -15,6 +16,8 @@ public function setUpCustomTargetingKey($keyName)
{
if (empty(($foo = $this->getCustomTargetingKey($keyName)))) {
$foo = $this->createCustomTargetingKey($keyName);
} elseif ($foo[0]['keyStatus'] === 'INACTIVE') {
$this->activateCustomTargetingKey($keyName);
}

return $foo[0]['keyId'];
Expand All @@ -34,6 +37,7 @@ public function createCustomTargetingKey($keyName)
$foo = [
'keyId' => $key->getId(),
'keyName' => $key->getName(),
'keyStatus' => $key->getStatus(),
'keyDisplayNameId' => $key->getDisplayName(),
];
array_push($output, $foo);
Expand All @@ -55,6 +59,7 @@ public function getAllCustomTargetingKeys()
$foo = [
'keyId' => $key->getId(),
'keyName' => $key->getName(),
'keyStatus' => $key->getStatus(),
'keyDisplayNameId' => $key->getDisplayName(),
];
array_push($output, $foo);
Expand All @@ -63,6 +68,18 @@ public function getAllCustomTargetingKeys()
return $output;
}

public function activateCustomTargetingKey($keyName)
{
$action = new ActivateCustomTargetingKeys();

$statementBuilder = (new StatementBuilder())
->where('name = :name')
->WithBindVariableValue('name', $keyName);

$customTargetingService = $this->serviceFactory->createCustomTargetingService($this->session);
$result = $customTargetingService->performCustomTargetingKeyAction($action, $statementBuilder->toStatement());
}

public function getCustomTargetingKey($keyName)
{
$output = [];
Expand All @@ -77,6 +94,7 @@ public function getCustomTargetingKey($keyName)
$foo = [
'keyId' => $key->getId(),
'keyName' => $key->getName(),
'keyStatus' => $key->getStatus(),
'keyDisplayNameId' => $key->getDisplayName(),
];
array_push($output, $foo);
Expand Down
29 changes: 29 additions & 0 deletions app/AdManager/ValueManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
require __DIR__.'/../../vendor/autoload.php';


use Google\AdsApi\AdManager\v201811\ActivateCustomTargetingValues;
use Google\AdsApi\AdManager\v201811\CustomTargetingValue;
use Google\AdsApi\AdManager\v201811\CustomTargetingValueMatchType;
use Google\AdsApi\AdManager\v201811\Statement;
use Google\AdsApi\AdManager\Util\v201811\StatementBuilder;

class ValueManager extends Manager
Expand All @@ -33,29 +35,41 @@ public function convertValuesListToDFPValuesList($valuesList)

//We create a table with only existing keys
$existingValuesList = [];
$inactiveValuesList = [];
$output = [];

foreach ($existing as $foo) {
array_push($existingValuesList, $foo['valueName']);
if (in_array($foo['valueName'], $valuesList)) {
array_push($output, $foo);
if ($foo['valueStatus'] === 'INACTIVE') {
$inactiveValuesList[$foo['valueName']] = $foo['valueId'];
}
}
}

//We create a list with values to be created
$valuesToBeCreated = [];
$valuesToBeActivated = [];
foreach ($valuesList as $element) {
if (!in_array($element, $existingValuesList)) {
array_push($valuesToBeCreated, $element);
} else if (isset($inactiveValuesList[$element])) {
array_push($valuesToBeActivated, $inactiveValuesList[$element]);
}
}

if (!empty($valuesToBeCreated)) {
$foo = $this->createCustomTargetingValues($valuesToBeCreated);
foreach ($foo as $bar) {
array_push($output, $bar);
}
}

if (!empty($valuesToBeActivated)) {
$this->activateCustomTargetingValues($valuesToBeActivated);
}

return $output;
}

Expand Down Expand Up @@ -98,6 +112,20 @@ public function createCustomTargetingValues($valuesToBeCreated)
return $output;
}

public function activateCustomTargetingValues($valuesToBeActivated)
{
$action = new ActivateCustomTargetingValues();

$statementText = sprintf("WHERE id IN (%s)", implode(',', $valuesToBeActivated));
$statement = new Statement($statementText);

$customTargetingService = $this->serviceFactory->createCustomTargetingService($this->session);
$result = $customTargetingService->performCustomTargetingValueAction($action, $statement);
if ($result->getNumChanges() !== count($valuesToBeActivated)) {
throw new \Exception("Failed to activate key values");
}
}

public function getExistingValuesFromAdManager()
{
$output = [];
Expand All @@ -119,6 +147,7 @@ public function getExistingValuesFromAdManager()
$foo = [
'valueId' => $value->getId(),
'valueName' => $value->getName(),
'valueStatus' => $value->getStatus(),
'valueDisplayName' => $value->getDisplayName(),
];
array_push($output, $foo);
Expand Down
2 changes: 1 addition & 1 deletion script/tests/ConnexionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require __DIR__.'/../scriptLoader.php';

$traffickerId = (new \App\Dfp\UserManager())->getUserId();
$traffickerId = (new \App\AdManager\UserManager())->getUserId();

if (is_numeric($traffickerId)) {
echo "\n====Connexion OK====\n\n";
Expand Down