File: D:/HostingSpaces/SBogers10/topswtw.komma.pro/app/KommaApp/Shop/Modules/Kiyoh/KiyohService.php
<?php
namespace KommaApp\Shop\Modules\Kiyoh;
use Carbon\Carbon;
use Illuminate\Support\Facades\Log;
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();
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
Log::debug('Got data from Kiyoh: ');
Log::debug('Score: ');
Log::debug($xml->averageRating);
Log::debug('Score last 12 months: ');
Log::debug($xml->last12MonthAverageRating);
Log::debug('Total reviews: ');
Log::debug($xml->numberReviews);
Log::debug('Total reviews last 12 months: ');
Log::debug($xml->last12MonthNumberReviews);
// $this->data->url = $xml->company->url;
//Check if the total_score is not 0
if ($xml->last12MonthAverageRating != 0) {
//if is not null update the totalScore
$this->data->totalScore = $xml->last12MonthAverageRating;
}
//Check if the total_reviews is not 0
if ($xml->last12MonthNumberReviews != 0) {
//if is not null update the total_reviews
$this->data->totalReviews = $xml->last12MonthNumberReviews;
}
// Save data
$this->data->save();
}
return true;
}
/**
* @return SimpleXMLElement
*/
protected function loadXML()
{
try {
$shopSlug = \Shop::getShop()->slug;
// $connectorCode = \Config::get('komma/kiyoh.' . $shopSlug . '.connector'); //Old technique
// $companyId = \Config::get('komma/kiyoh.' . $shopSlug . '.company_id'); //Old technique
$hash = \Config::get('komma/kiyoh.' . $shopSlug . '.hash'); //New technique
switch ($shopSlug) {
case 'nl':
if (!$xml = simplexml_load_file("https://www.kiyoh.com/v1/review/feed.xml?hash=".$hash)) {
return false;
}
break;
case 'de':
if (!$xml = simplexml_load_file("https://www.kiyoh.com/v1/review/feed.xml?hash=".$hash)) {
return false;
}
break;
case 'uk':
if (!$xml = simplexml_load_file("https://www.kiyoh.com/v1/review/feed.xml?hash=".$hash)) {
return false;
}
break;
default:
if (!$xml = simplexml_load_file("https://www.kiyoh.com/v1/review/feed.xml?hash=".$hash)) {
return false;
}
}
return $xml;
} catch (\Exception $exception) {
Log::debug($exception->getMessage());
return false;
}
}
/*
* 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();
}
}