Browse Source

adding

master
Björn 3 years ago
parent
commit
47afa95e07
11 changed files with 161 additions and 106 deletions
  1. +15
    -14
      app/Commands/CrawlerRunCommand.php
  2. +0
    -26
      app/Handlers/Action/ActionHandler.php
  3. +39
    -0
      app/Handlers/Actions/ActionHandler.php
  4. +4
    -1
      app/Handlers/Actions/ActionInterface.php
  5. +0
    -42
      app/Handlers/Handler.php
  6. +72
    -0
      app/Handlers/HandlerFactory.php
  7. +5
    -4
      app/Handlers/Response/ResponseHandler.php
  8. +1
    -1
      app/Handlers/Response/ResponseHandlerInterface.php
  9. +17
    -0
      resources/CustomHandlers/Action/FileDownload.php
  10. +0
    -13
      resources/CustomHandlers/Action/FileMove.php
  11. +8
    -5
      resources/CustomHandlers/Response/Rss.php

+ 15
- 14
app/Commands/CrawlerRunCommand.php View File

@ -5,7 +5,9 @@ namespace App\Commands;
use Illuminate\Console\Scheduling\Schedule;
use LaravelZero\Framework\Commands\Command;
use Illuminate\Support\Facades\Http;
use SimpleXMLElement;
use App\Handlers\HandlerFactory;
class CrawlerRunCommand extends Command
{
@ -35,24 +37,23 @@ class CrawlerRunCommand extends Command
'https://steadyhq.com/rss/insertmoin?auth=d37bffc9-9a84-4eed-95f6-3b6cb77c2406'
];
$handlerClass = $source->response_handler_class;
foreach ($sources as $source) {
$handler = new $handlerClass($source);
$response = $handler->getResponse();
// getting handler class and create object
$handlerClass = HandlerFactory::getResponseHandlerClass($source->response_handler_class);
$handler = new $handlerClass($source);
// if it is a 200
if ($response->ok()) {
$handler->run();
$response = $handler->getResponse();
// if there is an error on the source
} else if ($response->serverError()) {
// if it is a 200
if ($response->ok()) {
$handler->run();
}
}
private function get($url, $handler)
{
// if there is an error on the source
} else if ($response->serverError()) {
}
}
}
/**


+ 0
- 26
app/Handlers/Action/ActionHandler.php View File

@ -1,26 +0,0 @@
<?php
namespace App\ResponseHandler;
use App\Handlers\Handler;
/**
*
*
*
*/
class ActionHandler extends Handler implements ActionHandlerInterface
{
protected $data;
/**
*
* @param mixed $data
*
*/
public function __construct($data)
{
$this->data = $data;
}
}

+ 39
- 0
app/Handlers/Actions/ActionHandler.php View File

@ -0,0 +1,39 @@
<?php
namespace App\Handlers\Actions;
/**
*
*
*
*/
class ActionHandler implements ActionHandlerInterface
{
// data to process in a action
protected $data;
/**
*
* @param [type] $data
* @return [type]
*/
protected function processActions($source, $response, $data)
{
foreach($this->source->actions() as $action) {
HandlerFactory::getActionHandlerClass($action->response_handler_class);
$action = new ActionClass($this->source, $data);
$action->run();
}
}
/**
*
* @param mixed $data
*
*/
public function __construct($data)
{
$this->data = $data;
}
}

app/Handlers/Action/ActionInterface.php → app/Handlers/Actions/ActionInterface.php View File

@ -1,12 +1,15 @@
<?php
namespace App\Handlers\Action;
namespace App\Handlers\Actions;
/**
*
*
*
*/
interface ActionInterface
{
// method to process data
public function run();
}

+ 0
- 42
app/Handlers/Handler.php View File

@ -1,42 +0,0 @@
<?php
namespace App\Handlers;
/**
*
*
*
*/
abstract class Handler
{
/**
*
*
* @param String $className
* @return Mixed
*
*/
protected function getHandlerClass(String $className)
{
$result = NULL;
// create class
$class = 'App\\Handlers\\'.$className);
// if not found check for custom handlers
if (class_exists($class)) {
$result = new $class();
} else {
// create class
$class = 'CustomHandlers\\'.$className;
if (class_exists($class)) {
$result = new $class();
}
}
return $result;
}
}

+ 72
- 0
app/Handlers/HandlerFactory.php View File

@ -0,0 +1,72 @@
<?php
namespace App\Handlers;
use Exception;
/**
* Factory to getting Handler-Classes
*
*
*/
class HandlerFactory
{
/**
* getting handler class if exists
*
* @param String $className
* @return Mixed
*
*/
private static function getHandlerClass(String $prefix, String $className)
{
$result = NULL;
// create class
$class = 'App\\Handlers\\'.$prefix.'\\'.$className);
// if not found check for custom handlers
if (class_exists($class)) {
$result = new $class();
} else {
// create class
$class = 'CustomHandlers\\'.$prefix.'\\'.$className;
if (class_exists($class)) {
$result = new $class();
}
}
// if there is not class throw Exception
if ($result === NULL) {
thrown new Exception($prefix.'\\'.$className.' not Found!');
}
return $result;
}
/**
*
*
* @param String $className
* @return Mixed
*
*/
public static function getResponseHandlerClass(String $className)
{
return self::getHandlerClass('Response', String $className)
}
/**
*
*
* @param String $className
* @return Mixed
*
*/
public static function getActionHandlerClass(String $className)
{
return self::getHandlerClass('Actions', String $className)
}
}

+ 5
- 4
app/Handlers/Response/ResponseHandler.php View File

@ -1,17 +1,18 @@
<?php
namespace App\ResponseHandler;
use App\Handlers\Handler;
namespace App\Handlers\Response;
/**
*
*
*
*/
class ResponseHandler extends Handler implements ResponseHandlerInterface
class ResponseHandler implements ResponseHandlerInterface
{
// response
protected $response;
//
protected $source;
/**


+ 1
- 1
app/Handlers/Response/ResponseHandlerInterface.php View File

@ -1,6 +1,6 @@
<?php
namespace App\ResponseHandler;
namespace App\Handlers\Response;
/**
*


+ 17
- 0
resources/CustomHandlers/Action/FileDownload.php View File

@ -0,0 +1,17 @@
<?php
namespace CustomHandlers\Actions;
/**
*
*
*
*/
class FileDownload extends ActionHandler
{
public function run()
{
}
}

+ 0
- 13
resources/CustomHandlers/Action/FileMove.php View File

@ -1,13 +0,0 @@
<?php
namespace CustomHandlers\Actions;
/**
*
*
*
*/
class FileMove extends ActionHandler {
}

+ 8
- 5
resources/CustomHandlers/Response/Rss.php View File

@ -2,6 +2,9 @@
namespace CustomHandlers\Response;
use App\Handlers\Response\ResponseHandler;
use SimpleXMLElement;
/**
* Handle Response of a Rss Feed,
* response will parsed to SimpleXMLElement
@ -35,13 +38,13 @@ class Rss extends ResponseHandler
*/
public function run()
{
// parse xml from body
$xml = new SimpleXMLElement($this->response->body());
foreach($xml->channel->item as $item) {
foreach($this->actions as $action) {
$actionClass = $action->action_handler_class;
$action = new ActionClass($item);
$action->run();
// only process if xml is valid
if ($xml) {
foreach($xml->channel->item as $item) {
$this->processActions($item);
}
}
}

Loading…
Cancel
Save