Skip to content

Commit

Permalink
[Cloudflare] Static Analysis Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
onairmarc committed Jul 31, 2024
1 parent 188b3bc commit 664b9e4
Showing 1 changed file with 42 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,52 @@
namespace PHPGenesis\Services\Cloudflare\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Http;

/** @experimental */
class UpdateCloudflareDomainContactsCommand extends Command
{

protected string $accountId;
protected string $email;
protected string $apiKey;
protected string $defaultProfile;
protected $signature = 'cloudflare:updateDomainContacts';
protected $description = 'Iterates over all domains in a Cloudflare account and updates their contacts';

public function __construct()
{
parent::__construct();

$this->accountId = Config::get('phpgenesis.cloudflare.accountId');
$this->email = Config::get('phpgenesis.cloudflare.email');
$this->apiKey = Config::get('phpgenesis.cloudflare.apiKey');
$this->defaultProfile = Config::get('phpgenesis.cloudflare.defaultContactProfile');
}

public function handle(): void
{
$this->info('Fetching domains...');
$domains = $this->fetchDomains();

foreach ($domains as $domain) {
$this->info('Processing domain: ' . $domain['name']);
$this->updateDomainContacts($domain['name']);
}
if ($domains != []) {
foreach ($domains as $domain) {
$this->info('Processing domain: ' . $domain['name']);
$this->updateDomainContacts($domain['name']);
}

$this->info('All domains processed.');
$this->info('All domains processed.');
}
}

private function fetchDomains(): array
{
$accountId = config('phpgenesis.cloudflare.accountId');
$email = config('phpgenesis.cloudflare.email');
$apiKey = config('phpgenesis.cloudflare.apiKey');

$response = Http::withHeaders([
'X-Auth-Email' => $email,
'X-Auth-Key' => $apiKey,
'X-Auth-Email' => $this->email,
'X-Auth-Key' => $this->apiKey,
'Content-Type' => 'application/json',
])->get("https://api.cloudflare.com/client/v4/accounts/{$accountId}/registrar/domains");
])->get("https://api.cloudflare.com/client/v4/accounts/{$this->accountId}/registrar/domains");

$data = $response->json();

Expand All @@ -50,24 +64,19 @@ private function fetchDomains(): array
return $data['result'];
}

private function updateDomainContacts($domainId): void
private function updateDomainContacts(string $domain): void
{
$accountId = config('phpgenesis.cloudflare.accountId');
$email = config('phpgenesis.cloudflare.email');
$apiKey = config('phpgenesis.cloudflare.apiKey');
$defaultProfile = config('phpgenesis.cloudflare.defaultContactProfile');

$contact = [
'first_name' => config("phpgenesis.cloudflare.contactProfiles.{$defaultProfile}.firstName"),
'last_name' => config("phpgenesis.cloudflare.contactProfiles.{$defaultProfile}.lastName"),
'email' => config("phpgenesis.cloudflare.contactProfiles.{$defaultProfile}.email"),
'phone' => config("phpgenesis.cloudflare.contactProfiles.{$defaultProfile}.phone"),
'address' => config("phpgenesis.cloudflare.contactProfiles.{$defaultProfile}.addressLine1"),
'address2' => config("phpgenesis.cloudflare.contactProfiles.{$defaultProfile}.addressLine2"),
'city' => config("phpgenesis.cloudflare.contactProfiles.{$defaultProfile}.city"),
'state' => config("phpgenesis.cloudflare.contactProfiles.{$defaultProfile}.state"),
'zip' => config("phpgenesis.cloudflare.contactProfiles.{$defaultProfile}.postalCode"),
'country' => config("phpgenesis.cloudflare.contactProfiles.{$defaultProfile}.country"),
'first_name' => Config::get("phpgenesis.cloudflare.contactProfiles.{$this->defaultProfile}.firstName"),
'last_name' => Config::get("phpgenesis.cloudflare.contactProfiles.{$this->defaultProfile}.lastName"),
'email' => Config::get("phpgenesis.cloudflare.contactProfiles.{$this->defaultProfile}.email"),
'phone' => Config::get("phpgenesis.cloudflare.contactProfiles.{$this->defaultProfile}.phone"),
'address' => Config::get("phpgenesis.cloudflare.contactProfiles.{$this->defaultProfile}.addressLine1"),
'address2' => Config::get("phpgenesis.cloudflare.contactProfiles.{$this->defaultProfile}.addressLine2"),
'city' => Config::get("phpgenesis.cloudflare.contactProfiles.{$this->defaultProfile}.city"),
'state' => Config::get("phpgenesis.cloudflare.contactProfiles.{$this->defaultProfile}.state"),
'zip' => Config::get("phpgenesis.cloudflare.contactProfiles.{$this->defaultProfile}.postalCode"),
'country' => Config::get("phpgenesis.cloudflare.contactProfiles.{$this->defaultProfile}.country"),
];

$contacts = [
Expand All @@ -78,15 +87,15 @@ private function updateDomainContacts($domainId): void
];

$response = Http::withHeaders([
'X-Auth-Email' => $email,
'X-Auth-Key' => $apiKey,
'X-Auth-Email' => $this->email,
'X-Auth-Key' => $this->apiKey,
'Content-Type' => 'application/json',
])->put("https://api.cloudflare.com/client/v4/accounts/$accountId/registrar/domains/{$domainId}/contacts", $contacts);
])->put("https://api.cloudflare.com/client/v4/accounts/{$this->accountId}/registrar/domains/{$domain}/contacts", $contacts);

if ($response->successful()) {
$this->info('Contacts updated successfully for domain: ' . $domainId);
$this->info('Contacts updated successfully for domain: ' . $domain);
} else {
$this->error('Failed to update contacts for domain: ' . $domainId . '. Error: ' . $response->json()['errors'][0]['message']);
$this->error('Failed to update contacts for domain: ' . $domain . '. Error: ' . $response->json()['errors'][0]['message']);
}
}
}

0 comments on commit 664b9e4

Please sign in to comment.