File: D:/HostingSpaces/SBogers85/equichecker.com/app/KommaApp/Stats/StatsService.php
<?php
namespace KommaApp\Stats;
/**
* This class bundles al the statistics methods
*
* @author Tim Van Samang <timvansamang@komma.pro>
* @copyright (c) 2012-2015, Komma Mediadesign
*/
use KommaApp\Stats\Models\DetailPageStats;
use KommaApp\Stats\Models\SearchStats;
/**
* Class StatsService
* @package KommaApp\Stats
*/
class StatsService
{
private $ourCustomerId = [143, 146, 150, 151, 167, 169, 177];
/**
* When a customer searches for a company.
* We save the search_term and the result.
*
* @param $search_term
* @param $result
* @param $customer
*/
public function saveSearchStats($search_term, $result, $customer)
{
//If there is no account, return (normally not possible)
if (!$customer) return;
SearchStats::create([
'customer_id' => $customer->id,
'customer_country_id' => $customer->country,
'search_term' => $search_term,
'result_count' => $result->count(),
'result_ids' => json_encode($result->modelKeys())
]
);
}
/**
* When a customer opens an company detail page
* We save the company id and the customer
*
* @param $debtor
* @param $customer
* @param $searchTerm
*/
public function saveDetailPageStats($debtor, $customer, $searchTerm)
{
//If there is no account, return (normally not possible)
if (!$customer) return;
DetailPageStats::create([
'customer_id' => $customer->id,
'customer_country_id' => $customer->country,
'search_term' => $searchTerm,
'debtor_id' => $debtor->id,
]
);
}
/**
* Get the search terms for a specific customer.
* This is a wrapper for getSearchTermsBy()
* @param $customerId
* @return mixed
*/
public function getSearchTermsByCustomerId($customerId)
{
$searchStats = $this->getSearchTermsBy('customer_id', $customerId);
return $searchStats;
}
/**
* Get the search terms for a specific
* field value combination.
*
* @param $field
* @param $value
* @param string $operator
* @return mixed
*/
public function getSearchTermsBy($field, $value, $operator = '=')
{
$searchStats = SearchStats::where($field, $operator, $value)
//By default orderBy created
->orderBy('created_at', 'desc')
->get();
return $searchStats;
}
/**
* This is a wrapper for getDetailPagesBy
* and gets the visited detail pages
* for a specific customer
*
* @param $customerId
* @return mixed
*/
public function getDetailPageByCustomerId($customerId)
{
$detailPageStats = DetailPageStats::where('customer_id', '=', $customerId)
->orderBy('created_at', 'desc')
->get();
return $detailPageStats;
}
/**
* Get the detail page stats for a specific
* field value combination.
*
* @param $field
* @param $value
* @param string $operator
* @return mixed
*/
public function getDetailPageBy($field, $value, $operator = '=')
{
$detailPageStats = DetailPageStats::where($field, $operator, $value)
->orderBy('created_at', 'desc')
->whereNotIn('customer_id', $this->ourCustomerId)
->get();
return $detailPageStats;
}
/**
* Get the search term statistics
*
* @param 0 $minResult
* @return array
*/
public function getSearchTermsStats($resultNull = false)
{
//Select, the search term and a jound iso_2 from the country
$searchStats = SearchStats::select('search_stats.search_term as search_term', 'countries.iso_2 as iso')
->leftJoin('countries', 'countries.id', '=', 'search_stats.customer_country_id')
->whereNotIn('customer_id', $this->ourCustomerId);
//If $maxResult is set, only results where page searches have less then the given results
if ($resultNull) {
$searchStats = $searchStats->where('result_count', 0);
}
else{
$searchStats = $searchStats->where('result_count', '!=', 0);
}
//Get the stats
$searchStats = $searchStats->get();
//Sort by search_term and sort by the count of the items
$global = $this->groupAndSortByCount($searchStats, 'search_term');
//Countries
//Group by the iso (country.iso_2)
$country = $searchStats->groupBy('iso')
//Transform the items
->transform(function ($items) {
//Sort by search_term and sort by the count of the items
return $this->groupAndSortByCount($items, 'search_term');
})->sortBy('iso');
return [
'global' => $global,
'country' => $country
];
}
/**
* Get the detail page statistics
*
* @return array
*/
public function getDetailPageStats()
{
//Select the debtor_id, and the countries.iso_2 aa iao
$detailPageStats = DetailPageStats::select('debtor_id', 'countries.iso_2 as iso')
->leftJoin('countries', 'countries.id', '=', 'detail_page_stats.customer_country_id')
->whereNotIn('customer_id', $this->ourCustomerId)
//include the debtor
->with('debtor');
//Get the stats
$detailPageStats = $detailPageStats->get();
//Sort by detail_page and sort by the count of the items
$global = $this->groupAndSortByCount($detailPageStats, 'debtor_id');
//Countries
//Group by the iso (country.iso_2)
$country = $detailPageStats->groupBy('iso')
//Transform the items
->transform(function ($element) {
//Sort by detail_page and sort by the count of the items
return $this->groupAndSortByCount($element, 'debtor_id');
})->sortBy('iso');
return [
'global' => $global,
'country' => $country
];
}
public function groupAndSortByCount($collection, $group_key)
{
//Group by group_key
$collection = $collection->groupBy($group_key)
//Sort by the count
->sortBy(function ($items) {
return $items->count();
})
//Reverse highest first
->reverse();
return $collection;
}
}