File: D:/HostingSpaces/SBogers10/topswtwmobile.komma.pro/app/KommaApp/Shop/Modules/Kiyoh/KiyohService.php
<?php
namespace KommaApp\Shop\Modules\Kiyoh;
use Carbon\Carbon;
class KiyohService
{
/**
* @var Kiyoh
*/
protected $kiyoh;
/**
* @var $array
*/
protected $data;
/**
* @var KiyohMailConnector
*/
private $kiyohMailConnector;
/**
* @param Kiyoh $kiyoh
* @param KiyohMailConnector $kiyohMailConnector
*/
public function __construct(Kiyoh $kiyoh, KiyohMailConnector $kiyohMailConnector)
{
$this->kiyoh = $kiyoh;
$this->kiyohMailConnector = $kiyohMailConnector;
}
/**
* @return KiyohEntity
*/
public function get()
{
$this->update();
//var_dump($this->data);
return $this->data;
}
/**
* @return Array
*/
public function getStars()
{
// Set Score data
$scoreMax = 10;
// Set star data
$starMax = 5;
$starFull = 'star full';
$starHalf = 'star half';
$starNone = 'star';
// Calculate score
$score = $this->data->totalScore;
$starScore = $score * $starMax / $scoreMax;
// Generate array
$starClasses = [];
for ($i = 0; $i < $starMax; $i++) {
if ($starScore - $i >= 1)
$starClasses[] = $starFull;
else if ($starScore - $i >= .5)
$starClasses[] = $starHalf;
else
$starClasses[] = $starNone;
}
return $starClasses;
}
/**
* Update database table
*/
public function update()
{
// Get from database table
$this->data = $this->kiyoh->where('shop_id', \Shop::getShop()->id)->first();
// Update when totalReviews =0 or only every 12 hours
if ($this->data->totalReviews == 0 || Carbon::now() > $this->data->updated_at->addHours(12)) {
if (!$xml = $this->loadXML()) return false;
// Set data
// $this->data->url = $xml->company->url;
//Check if the total_score is not 0
if ($xml->company->total_score != 0) {
//if is not null update the totalScore
$this->data->totalScore = $xml->company->total_score;
}
//Check if the total_reviews is not 0
if ($xml->company->total_reviews != 0) {
//if is not null update the total_reviews
$this->data->totalReviews = $xml->company->total_reviews;
} //Check if the total_reviews was already 0 and the review_amount of the averge_scores is not 0
elseif ($this->data->totalReviews == 0 && $xml->company->average_scores->review_amount != 0) {
//Set the average_score review_amount as fallback
$this->data->totalReviews = $xml->company->average_scores->review_amount;
}
// Save data
$this->data->save();
}
return true;
}
/**
* @return SimpleXMLElement
*/
protected function loadXML()
{
$shopSlug = \Shop::getShop()->slug;
$connectorCode = \Config::get('komma/kiyoh.' . $shopSlug . '.connector');
$companyId = \Config::get('komma/kiyoh.' . $shopSlug . '.company_id');
switch ($shopSlug) {
case 'nl':
//Added @ to function to skip external faults in the xml
if (!$xml = @simplexml_load_file("https://www.kiyoh.nl/xml/recent_company_reviews.xml?connectorcode=" . $connectorCode . "&company_id=" . $companyId)) return false;
break;
case 'de':
//Added @ to function to skip external faults in the xml
if (!$xml = @simplexml_load_file("https://www.kiyoh.com/xml/recent_company_reviews.xml?connectorcode=" . $connectorCode . "&company_id=" . $companyId))return false;
break;
case 'uk':
//Added @ to function to skip external faults in the xml
if (!$xml = @simplexml_load_file("https://www.kiyoh.com/xml/recent_company_reviews.xml?connectorcode=" . $connectorCode . "&company_id=" . $companyId)) return false;
break;
default:
//Fallback if a shop country is not set
if (!$xml = @simplexml_load_file("https://www.kiyoh.nl/xml/recent_company_reviews.xml?connectorcode=" . $connectorCode . "&company_id=" . $companyId)) return false;
}
return $xml;
}
/*
* Sends a email to the user
*/
public
function sendReviewEmailToCustomer($order)
{
// Call to Kiyoh
$response = $this->kiyohMailConnector->callUrl($order->invoice_email);
// Save response in database
$order->kiyoh_response = json_encode($response);
$order->save();
}
}