Created
October 24, 2014 06:28
-
-
Save thomaslarsson/d4d4af45863e29d8b449 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace NM\Bundle\GoogleProductsBundle\API; | |
use eZ\Publish\API\Repository\Values\Content\Query\Criterion; | |
use eZ\Publish\API\Repository\Values\Content\Query; | |
use eZ\Publish\Core\MVC\Legacy\Kernel as LegacyKernel; | |
use eZContentObjectTreeNode; | |
/** | |
* Class eZProductSearchService | |
* @package NM\Bundle\GoogleProductsBundle\API | |
* | |
* @author Netmaking AS, Thomas Maurstad Larsson<thomas@netmaking.no> | |
*/ | |
class eZProductSearchService | |
{ | |
/** | |
* @var int | |
*/ | |
protected $rootLocationId = 2; | |
/** | |
* @var array | |
*/ | |
protected $searchFilters = array( | |
'ClassFilterType' => 'include', | |
'ClassFilterArray' => array('vismaproduct'), | |
'SortBy' => array('path', true), | |
'IgnoreVisibility' => false | |
); | |
/** | |
* @var eZLegacyKernel | |
*/ | |
protected $legacyKernel; | |
/** | |
* @var array | |
*/ | |
protected $products; | |
/** | |
* @var int | |
*/ | |
protected $count; | |
/** | |
* Public constructor | |
* @param callable $legacyKernel | |
*/ | |
public function __construct( \Closure $legacyKernel ) | |
{ | |
$this->count = 0; | |
$this->products = array(); | |
$this->legacyKernel = $legacyKernel; | |
} | |
/** | |
* Performs a search using the eZ Publish legacy kernel | |
* | |
* @return $this | |
*/ | |
public function perform() | |
{ | |
$rootLocationId = $this->rootLocationId; | |
$searchFilters = $this->searchFilters; | |
$data = $this->legacyKernelClosure()->runCallback( | |
function() use ($rootLocationId, $searchFilters) | |
{ | |
return eZContentObjectTreeNode::subTreeByNodeID($searchFilters, $rootLocationId); | |
} | |
); | |
if ( $data ) | |
{ | |
$this->count = (int) count($data); | |
$this->products = $data; | |
} | |
return $this; | |
} | |
/** | |
* @return int | |
*/ | |
public function getCount() | |
{ | |
return $this->count; | |
} | |
/** | |
* @return bool | |
*/ | |
public function hasResults() | |
{ | |
return (bool) $this->getCount() > 0; | |
} | |
/** | |
* @return array | |
*/ | |
public function getResults() | |
{ | |
return $this->products; | |
} | |
/** | |
* Allows you to run calls to the Legacy Kernel as a Closure | |
* | |
* @return \Clojure $legaxyKernelClosure | |
*/ | |
public function legacyKernelClosure() | |
{ | |
$legacyKernel = $this->legacyKernel; | |
return $legacyKernel(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment