-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implements console action 'update-crawler'
(refs #14)
- Loading branch information
Showing
13 changed files
with
1,372 additions
and
60 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
<?php | ||
/** | ||
* YAWIK SimpleImport | ||
* | ||
* @filesource | ||
* @license MIT | ||
* @copyright 2013 - 2018 Cross Solution <http://cross-solution.de> | ||
*/ | ||
|
||
/** */ | ||
namespace SimpleImport\Controller; | ||
|
||
use SimpleImport\InputFilter\CrawlerInputFilter; | ||
use Zend\Console\ColorInterface; | ||
use Zend\InputFilter\InputFilter; | ||
use Zend\Mvc\Console\Controller\AbstractConsoleController; | ||
|
||
/** | ||
* Update crawler configuration or displays crawler information. | ||
* | ||
* @author Mathias Gelhausen <[email protected]> | ||
*/ | ||
class UpdateCrawlerConsoleController extends AbstractConsoleController | ||
{ | ||
/** | ||
* @var CrawlerInputFilter | ||
*/ | ||
private $inputFilter; | ||
|
||
/** | ||
* @param CrawlerInputFilter $inputFilter | ||
* | ||
* @return self | ||
*/ | ||
public function setInputFilter(CrawlerInputFilter $inputFilter) | ||
{ | ||
$this->inputFilter = $inputFilter; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* displays list of crawlers or information about one crawler | ||
* | ||
* @return string|null | ||
*/ | ||
public function indexAction() | ||
{ | ||
/* @var \SimpleImport\Controller\Plugin\LoadCrawler $loader */ | ||
|
||
$loader = $this->plugin('siLoadCrawler'); | ||
|
||
if ($this->params('name')) { | ||
$crawler = $loader(); | ||
|
||
return $this->info($crawler); | ||
} | ||
|
||
$set = $loader->loadAll(); | ||
|
||
echo PHP_EOL; | ||
foreach ($set as $crawler) { | ||
printf("%'.-40s (%s)" . PHP_EOL, $crawler->getName(), $crawler->getId()); | ||
} | ||
} | ||
|
||
/** | ||
* generates cralwer information message. | ||
* | ||
* @param \SimpleImport\Entity\Crawler $crawler | ||
* | ||
* @return string | ||
*/ | ||
private function info(\SimpleImport\Entity\Crawler $crawler) | ||
{ | ||
return sprintf(<<<EOF | ||
Name: %s (%s) [%s] | ||
Organization: %s (%s) | ||
Feed-URI: %s | ||
Run delay: %s | ||
Jobs initial state: %s | ||
Date last run: %s | ||
EOF | ||
, $crawler->getName(), $crawler->getId(), $crawler->getType(), | ||
$crawler->getOrganization()->getOrganizationName()->getName(), | ||
$crawler->getOrganization()->getId(), | ||
$crawler->getFeedUri(), | ||
$crawler->getRunDelay(), | ||
$crawler->getOptions()->getInitialState(), | ||
$crawler->getDateLastRun()->format('d.m.Y H:i:s') | ||
); | ||
|
||
} | ||
|
||
|
||
/** | ||
* update the configuration of a crawler | ||
* | ||
* @return string | ||
*/ | ||
public function updateAction() | ||
{ | ||
/* @var \SimpleImport\Entity\Crawler $crawler */ | ||
|
||
$loader = $this->plugin('siLoadCrawler'); | ||
$crawler = $loader(); | ||
$values = $this->validateInput($crawler); | ||
$console = $this->getConsole(); | ||
|
||
$crawler->setName($values['name']); | ||
/** @noinspection PhpParamsInspection */ | ||
$crawler->setOrganization($values['organization']); | ||
$crawler->setFeedUri($values['feedUri']); | ||
$crawler->setRunDelay((int) $values['runDelay']); | ||
$crawler->setType($values['type']); | ||
$crawler->setOptionsFromArray((array) $values['options']); | ||
|
||
$loader->store($crawler); | ||
|
||
$console->writeLine('Crawler updated.', ColorInterface::GREEN); | ||
return $this->info($crawler); | ||
} | ||
|
||
/** | ||
* Validates rhe command line arguments. | ||
* | ||
* @param \SimpleImport\Entity\Crawler $crawler | ||
* | ||
* @return array | ||
* @throws \RuntimeException if validation fails. | ||
*/ | ||
private function validateInput(\SimpleImport\Entity\Crawler $crawler) | ||
{ | ||
$params = $this->plugin('params'); | ||
|
||
$this->inputFilter->setData([ | ||
'name' => $params('rename', $crawler->getName()), | ||
'feedUri' => $params('feed-uri', $crawler->getFeedUri()), | ||
'organization' => $params('organization', $crawler->getOrganization()), | ||
'runDelay' => $params('rundelay', $crawler->getRunDelay()), | ||
'type' => $params('type', $crawler->getType()), | ||
'options' => ['initialState' => $params('jobInitialState', $crawler->getOptions()->getInitialState())] | ||
]); | ||
|
||
if (!$this->inputFilter->isValid()) { | ||
$message = 'Invalid parameters!' . PHP_EOL . PHP_EOL; | ||
foreach ($this->inputFilter->getMessages() as $name => $messages) { | ||
$message .= sprintf(' - %s: %s', $name, join(', ', $messages)) . PHP_EOL; | ||
} | ||
|
||
throw new \RuntimeException($message); | ||
} | ||
|
||
$values = array_filter($this->inputFilter->getValues(), function($i) { return !empty($i); }); | ||
return $values; | ||
} | ||
|
||
} |
53 changes: 53 additions & 0 deletions
53
src/Factory/Controller/UpdateCrawlerConsoleControllerFactory.php
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
/** | ||
* YAWIK | ||
* | ||
* @filesource | ||
* @license MIT | ||
* @copyright 2013 - 2018 Cross Solution <http://cross-solution.de> | ||
*/ | ||
|
||
/** */ | ||
namespace SimpleImport\Factory\Controller; | ||
|
||
use SimpleImport\Controller\UpdateCrawlerConsoleController; | ||
use Interop\Container\ContainerInterface; | ||
use SimpleImport\InputFilter\CrawlerInputFilter; | ||
use Zend\ServiceManager\Factory\FactoryInterface; | ||
|
||
/** | ||
* Factory for \SimpleImport\Controller\UpdateCrawlerConsoleController | ||
* | ||
* @author Mathias Gelhausen <[email protected]> | ||
* @todo write test | ||
*/ | ||
class UpdateCrawlerConsoleControllerFactory implements FactoryInterface | ||
{ | ||
|
||
/** | ||
* @param ContainerInterface $container | ||
* @param string $requestedName | ||
* @param array|null $options | ||
* | ||
* @return UpdateCrawlerConsoleController | ||
*/ | ||
public function __invoke(ContainerInterface $container, $requestedName, array $options = null) | ||
{ | ||
/* @var CrawlerInputFilter $filter | ||
* @var \Zend\Router\RouteMatch $routeMatch | ||
*/ | ||
|
||
$controller = new UpdateCrawlerConsoleController(); | ||
$application = $container->get('Application'); | ||
$routeMatch = $application->getMvcEvent()->getRouteMatch(); | ||
|
||
if ('index' != $routeMatch->getParam('action')) { | ||
$filters = $container->get('InputFilterManager'); | ||
$filter = $filters->get(CrawlerInputFilter::class); | ||
|
||
$controller->setInputFilter($filter); | ||
} | ||
|
||
return $controller; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
<?php | ||
/** | ||
* YAWIK | ||
* | ||
* @filesource | ||
* @license MIT | ||
* @copyright 2013 - 2018 Cross Solution <http://cross-solution.de> | ||
*/ | ||
|
||
/** */ | ||
namespace SimpleImport\Filter; | ||
|
||
use Doctrine\ODM\MongoDB\DocumentRepository; | ||
use Zend\Filter\FilterInterface; | ||
|
||
/** | ||
* Filter to load an entity by its id. | ||
* | ||
* @author Mathias Gelhausen <[email protected]> | ||
*/ | ||
class IdToEntity implements FilterInterface | ||
{ | ||
/** | ||
* @var DocumentRepository | ||
*/ | ||
private $repository; | ||
|
||
/** | ||
* Custom value to return if no entity is found. | ||
* | ||
* @var mixed | ||
*/ | ||
private $notFoundValue; | ||
|
||
/** | ||
* IdToEntity constructor. | ||
* | ||
* @param DocumentRepository $repository | ||
*/ | ||
public function __construct(DocumentRepository $repository) | ||
{ | ||
$this->repository = $repository; | ||
} | ||
|
||
/** | ||
* Allows direct invokation. | ||
* | ||
* Proxies to {@łink filter()} | ||
* | ||
* @param mixed $value | ||
* | ||
* @return mixed|null | ||
* @see filter | ||
*/ | ||
public function __invoke($value) | ||
{ | ||
return $this->filter($value); | ||
} | ||
|
||
/** | ||
* @param mixed $notFoundValue | ||
* | ||
* @return self | ||
*/ | ||
public function setNotFoundValue($notFoundValue) | ||
{ | ||
$this->notFoundValue = $notFoundValue; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @param $value | ||
* | ||
* @return mixed | ||
*/ | ||
private function getNotFoundValue($value) | ||
{ | ||
return $this->notFoundValue ?: $value; | ||
} | ||
|
||
/** | ||
* Filters an id to an entity instance. | ||
* | ||
* If empty($value) is true, returns null. | ||
* | ||
* The id can be given as a string or as an instance of \MongoId. | ||
* If an entity is passed, and it is of the type managed by {@link repository}, it is | ||
* returned as is. | ||
* | ||
* If no entity with the given Id is found, the {@link notFoundValue} is returned | ||
* which defaults to $value | ||
* | ||
* If $value is not an entity that {@link repository} manages, and $value is not a string | ||
* or an instance of \MongoId, an exception is thrown. | ||
* | ||
* @param mixed $value | ||
* | ||
* @return mixed|null|object | ||
* @throws \InvalidArgumentException | ||
*/ | ||
public function filter($value) | ||
{ | ||
if (empty($value)) { return null; } | ||
|
||
if (is_string($value) || $value instanceOf \MongoId) { | ||
return $this->repository->find($value) ?: $this->getNotFoundValue($value); | ||
} | ||
|
||
if (!is_a($value, $this->repository->getDocumentName())) { | ||
throw new \InvalidArgumentException(sprintf( | ||
'Value must either be a string or an instance of \MongoId or %s', | ||
$this->repository->getDocumentName()) | ||
); | ||
} | ||
|
||
return $value; | ||
} | ||
|
||
} |
Oops, something went wrong.