# Synchronization

## syncDomains()

This function is used for the "domain synchronization". This is a feature which runs once per day on the hostware environment to get the current domain settings to keep the hostware database up to date. You can see in the response which data gets updated.

This should also return the domain handle data for all 4 handles. If hostware has no inside the&#x20;

### Parameters

This function gets a collection of domains as a parameter which need to be checked. You can return MORE domains, but hostware will only check and update the domains which are provided inside the input collection.&#x20;

<table><thead><tr><th width="142">Type</th><th width="166">Name</th><th>Description</th></tr></thead><tbody><tr><td>Collection</td><td>$domains</td><td>Collection of Domain Models which should be queried</td></tr></tbody></table>

### Response

Array of DomainSyncResponse() class. **The array key must be the domain id.**

{% hint style="info" %}
If a requested domain is not available inside the response, hostware will assume it was **transferred away** and will delete it inside the hostware database.
{% endhint %}

<table><thead><tr><th width="142">Type</th><th width="166">Name</th><th>Description</th></tr></thead><tbody><tr><td>DomainSyncStatusEnum</td><td>$domainStatus</td><td>The status of the domain (ACTIVE, INACTIVE, TRANSFERRED_AWAY)</td></tr><tr><td>Bool</td><td>$autoRenewActive</td><td>The auto renew status</td></tr><tr><td>Carbon or Null</td><td>$expiryDate</td><td>The date of domain expiry</td></tr><tr><td>Carbon or Null</td><td>$registrationDate</td><td>The date of domain creation</td></tr><tr><td>Bool</td><td>$transferLockActive</td><td>The status of transfer lock</td></tr><tr><td>Bool</td><td>$privacyActive</td><td>The status of privacy protection</td></tr></tbody></table>

### Example Code

```php
public function syncDomains(Collection $domains): array {
		$responseData = [];

		$xml = <<<XML
<?xml version="1.0" encoding="utf-8" ?>
<request>
    	{$this->getXmlHeader()}
    <transaction>
        <report>
            <group>domain</group>
            <data_type>csv</data_type>
        </report>
    </transaction>
</request>

XML;


		$response = $this->getClient()->callreportRaw($xml, $this->config['sandbox']);
		$explode = explode("\n", $response->result->detail->report->domain->data);

		foreach ($explode as $domainItem) {
			$domainItemExploded = explode(';', $domainItem);
			if ($domainItemExploded[0] == 'domain') continue;
			if ($domainItemExploded[0] == '') continue;

			$allDomainsStructured[strtolower($domainItemExploded[0])] = $domainItemExploded;
		}
		
				/**
				 * Get all domain handles
				 */
				$xml = <<<XML
		<?xml version="1.0" encoding="utf-8" ?>
		<request>
			{$this->getXmlHeader()}
		    <transaction>
		        <report>
		            <group>contact</group>
		            <data_type>csv</data_type>
		        </report>
		    </transaction>
		</request>
		XML;

		$explodedresponse = Cache::remember("hw-CPS-contactCsv", 60 * 5, function () use ($xml) {
			$response = $this->getClient()->callreportRaw($xml, $this->config['sandbox']);
			return explode("\n", $response->result->detail->report->contact->data);
		});

		$contacts = collect([]);

		foreach ($explodedresponse as $lineItem) {
			$lineItem = explode(";", $lineItem);
			if($lineItem[0] === "cid") continue;
			if(count($lineItem) !== 16) continue;

//			dd($lineItem);
			$contacts->add($this->_getContactFromHandle($lineItem));
		}

		$contacts = $contacts->keyBy(function ($item) {
			return (string)$item->getAttributes()['id'];
		});

		foreach ($domains as $domain) {
			$resItem = $allDomainsStructured[strtolower($domain->fqdn)] ?? $allDomainsStructured[strtolower($domain->punycode_fqdn)] ?? null;

			if ($resItem === null) {
				$responseData[$domain->id] = new DomainSyncResponse(DomainSyncStatusEnum::INACTIVE);
				continue;
			}

			$responseData[$domain->id] = new DomainSyncResponse(
				DomainSyncStatusEnum::ACTIVE,
				$resItem[16] === "active",
				Carbon::parse($resItem[10]),
				Carbon::parse($resItem[7]),
				$resItem[14] === "active",
				false,

				$contacts->get($resItem[20]),
				$contacts->get($resItem[21]),
				$contacts->get($resItem[22]),
				$contacts->get($resItem[23]),
			);
		}

//		dd($responseData);

		return $responseData;
	}
	
	
	private function _getContactFromHandle(array $reportLine) {
		$type = ContactTypeEnum::PERSON;
		if ($reportLine[4] === "organisation") {
			$type = ContactTypeEnum::ORG;
		} elseif ($reportLine[4] === "role") {
			$type = ContactTypeEnum::ROLE;
		}

		$street = $reportLine[8];
		$streetNumber = null;

		if (preg_match('/^(.+?)\s+(\d+[\w\-\/]*)$/u', $street, $matches)) {
			$street = $matches[1];
			$streetNumber = $matches[2];
		}

		if($reportLine[7] === "-" || $type === ContactTypeEnum::PERSON) {
			$reportLine[7] = null;
		}

//		if($reportLine[2] === "EVMI7") {
//			dd($reportLine);
//		}

		return new DomainContact([
			"id" => $reportLine[2],
			"type" => $type,
			"sex" => ContactSexEnum::MALE,
			"first_name" => $reportLine[5],
			"last_name" => $reportLine[6],
			"organisation" => $reportLine[7],
			"street" => $street,
			"street_number" => $streetNumber,
			"zipcode" => $reportLine[9],
			"city" => $reportLine[10],
			"state" => $reportLine[11],
			"country" => $reportLine[12],
			"telephone" => $reportLine[13],
			"telefax" => $reportLine[14],
			"email" => $reportLine[15]
		]);
	}
```

### Testing

This function gets executed daily in the cronjob / background. Every domain has a column "synced=true/false" inside the database. Every day, this status gets set to false so the domain will be synced once again. After the sync, the column will be set to true.

**Reset the domain sync column:**

```bash
php artisan hw:resetDomainSync
```

**Run the domain sync for a specific domain. You can also remove the domain parameter to let the sync run for all domains inside your portfolio.**

```bash
php artisan hw:initiateDomainSync --sync YOUR_DOMAIN.de
```
