After this separation, search engine may be registered once for visitor area (with one presenter) and once for admin area (with different presenter).
73 lines
1.8 KiB
PHP
73 lines
1.8 KiB
PHP
<?php
|
|
namespace App\Handlers\Registrators;
|
|
|
|
use App\Enumerations\Exceptions\EnumerationExistsException;
|
|
use App\Handlers\Contracts\SearchHandler;
|
|
use App\Handlers\Contracts\SearchPresenterHandler;
|
|
use App\Handlers\Registrators\Items\SearchEngine;
|
|
use Symfony\Component\Process\Exception\InvalidArgumentException;
|
|
|
|
class SearchEngineRegistrator
|
|
{
|
|
const ADMIN = 'admin';
|
|
const VISITOR = 'visitor';
|
|
protected $searchEngines;
|
|
|
|
function __construct($app)
|
|
{
|
|
$this->searchEngines = [
|
|
self::ADMIN => [],
|
|
self::VISITOR => []
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Method for registering new settings extensions/parts
|
|
*
|
|
* @param SearchHandler $search
|
|
* @param SearchPresenterHandler $presenter
|
|
*
|
|
* @param $type
|
|
*
|
|
* @throws EnumerationExistsException
|
|
*/
|
|
public function register(SearchHandler $search, SearchPresenterHandler $presenter, $type)
|
|
{
|
|
$this->checkIfRegistered($type, $search->getId());
|
|
|
|
// register new extension
|
|
$this->searchEngines[$type][$search->getId()] = [$search, $presenter];
|
|
}
|
|
|
|
/**
|
|
* Getter for all registered extensions
|
|
*
|
|
* @param $type
|
|
*
|
|
* @return array
|
|
*/
|
|
public function get($type)
|
|
{
|
|
if (!isset($this->searchEngines[$type])) {
|
|
throw new InvalidArgumentException('Invalid type ' . $type);
|
|
}
|
|
|
|
return $this->searchEngines[$type];
|
|
}
|
|
|
|
/**
|
|
* Checks if this section is not already defined
|
|
*
|
|
* @param $type
|
|
* @param $id
|
|
*
|
|
* @throws EnumerationExistsException
|
|
*/
|
|
private function checkIfRegistered($type, $id)
|
|
{
|
|
if (isset($this->searchEngines[$type][$id])) {
|
|
throw new EnumerationExistsException('Already existing search engine ' . $id . ' in ' . $type);
|
|
}
|
|
}
|
|
}
|