* initial commit
* get list of activities * get activity by id
This commit is contained in:
92
src/App/Action/AbstractAction.php
Normal file
92
src/App/Action/AbstractAction.php
Normal file
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
|
||||
namespace App\Action;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Zend\Diactoros\Response\JsonResponse;
|
||||
use Zend\Stratigility\MiddlewareInterface;
|
||||
|
||||
abstract class AbstractAction implements MiddlewareInterface
|
||||
{
|
||||
const IDENTIFIER_NAME = 'id';
|
||||
|
||||
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||
{
|
||||
$requestMethod = strtoupper($request->getMethod());
|
||||
$id = $request->getAttribute(static::IDENTIFIER_NAME);
|
||||
|
||||
switch ($requestMethod) {
|
||||
case 'GET':
|
||||
return isset($id)
|
||||
? $this->get($request, $response, $next)
|
||||
: $this->getList($request, $response, $next);
|
||||
case 'POST':
|
||||
return $this->create($request, $response, $next);
|
||||
case 'PUT':
|
||||
return $this->update($request, $response, $next);
|
||||
case 'DELETE':
|
||||
return isset($id)
|
||||
? $this->delete($request, $response, $next)
|
||||
: $this->deleteList($request, $response, $next);
|
||||
case 'HEAD':
|
||||
return $this->head($request, $response, $next);
|
||||
case 'OPTIONS':
|
||||
return $this->options($request, $response, $next);
|
||||
case 'PATCH':
|
||||
return $this->patch($request, $response, $next);
|
||||
default:
|
||||
return $next($request, $response);
|
||||
}
|
||||
}
|
||||
|
||||
public function get(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||
{
|
||||
return $this->createResponse(['content' => 'Method not allowed'], 405);
|
||||
}
|
||||
|
||||
public function getList(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||
{
|
||||
return $this->createResponse(['content' => 'Method not allowed'], 405);
|
||||
}
|
||||
|
||||
public function create(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||
{
|
||||
return $this->createResponse(['content' => 'Method not allowed'], 405);
|
||||
}
|
||||
|
||||
public function update(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||
{
|
||||
return $this->createResponse(['content' => 'Method not allowed'], 405);
|
||||
}
|
||||
|
||||
public function delete(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||
{
|
||||
return $this->createResponse(['content' => 'Method not allowed'], 405);
|
||||
}
|
||||
|
||||
public function deleteList(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||
{
|
||||
return $this->createResponse(['content' => 'Method not allowed'], 405);
|
||||
}
|
||||
|
||||
public function head(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||
{
|
||||
return $this->createResponse(['content' => 'Method not allowed'], 405);
|
||||
}
|
||||
|
||||
public function options(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||
{
|
||||
return $this->createResponse(['content' => 'Method not allowed'], 405);
|
||||
}
|
||||
|
||||
public function patch(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||
{
|
||||
return $this->createResponse(['content' => 'Method not allowed'], 405);
|
||||
}
|
||||
|
||||
final protected function createResponse($data, $status = 200)
|
||||
{
|
||||
return new JsonResponse($data, $status);
|
||||
}
|
||||
}
|
||||
34
src/App/Action/ActivityAction.php
Normal file
34
src/App/Action/ActivityAction.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace App\Action;
|
||||
|
||||
use App\Service\SkiesService;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Zend\Diactoros\Response\JsonResponse;
|
||||
|
||||
class ActivityAction extends AbstractAction
|
||||
{
|
||||
private $skiesService;
|
||||
|
||||
public function __construct(SkiesService $skiesService)
|
||||
{
|
||||
$this->skiesService = $skiesService;
|
||||
}
|
||||
|
||||
public function getList(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||
{
|
||||
return new JsonResponse($this->skiesService->getActivityList());
|
||||
}
|
||||
|
||||
public function get(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||
{
|
||||
$id = $request->getAttribute(self::IDENTIFIER_NAME);
|
||||
return new JsonResponse($this->skiesService->getActivity($id));
|
||||
}
|
||||
|
||||
public function options(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||
{
|
||||
return new JsonResponse(true);
|
||||
}
|
||||
}
|
||||
15
src/App/Action/ActivityFactory.php
Normal file
15
src/App/Action/ActivityFactory.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace App\Action;
|
||||
|
||||
use App\Service\SkiesService;
|
||||
use Interop\Container\ContainerInterface;
|
||||
|
||||
class ActivityFactory
|
||||
{
|
||||
public function __invoke(ContainerInterface $container)
|
||||
{
|
||||
$skiesService = $container->get(SkiesService::class);
|
||||
return new ActivityAction($skiesService);
|
||||
}
|
||||
}
|
||||
62
src/App/Action/HomePageAction.php
Normal file
62
src/App/Action/HomePageAction.php
Normal file
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
namespace App\Action;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Zend\Diactoros\Response\HtmlResponse;
|
||||
use Zend\Diactoros\Response\JsonResponse;
|
||||
use Zend\Expressive\Router;
|
||||
use Zend\Expressive\Template;
|
||||
use Zend\Expressive\Plates\PlatesRenderer;
|
||||
use Zend\Expressive\Twig\TwigRenderer;
|
||||
use Zend\Expressive\ZendView\ZendViewRenderer;
|
||||
|
||||
class HomePageAction
|
||||
{
|
||||
private $router;
|
||||
|
||||
private $template;
|
||||
|
||||
public function __construct(Router\RouterInterface $router, Template\TemplateRendererInterface $template = null)
|
||||
{
|
||||
$this->router = $router;
|
||||
$this->template = $template;
|
||||
}
|
||||
|
||||
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||
{
|
||||
$data = [];
|
||||
|
||||
if ($this->router instanceof Router\AuraRouter) {
|
||||
$data['routerName'] = 'Aura.Router';
|
||||
$data['routerDocs'] = 'http://auraphp.com/packages/2.x/Router.html';
|
||||
} elseif ($this->router instanceof Router\FastRouteRouter) {
|
||||
$data['routerName'] = 'FastRoute';
|
||||
$data['routerDocs'] = 'https://github.com/nikic/FastRoute';
|
||||
} elseif ($this->router instanceof Router\ZendRouter) {
|
||||
$data['routerName'] = 'Zend Router';
|
||||
$data['routerDocs'] = 'http://framework.zend.com/manual/current/en/modules/zend.mvc.routing.html';
|
||||
}
|
||||
|
||||
if ($this->template instanceof PlatesRenderer) {
|
||||
$data['templateName'] = 'Plates';
|
||||
$data['templateDocs'] = 'http://platesphp.com/';
|
||||
} elseif ($this->template instanceof TwigRenderer) {
|
||||
$data['templateName'] = 'Twig';
|
||||
$data['templateDocs'] = 'http://twig.sensiolabs.org/documentation';
|
||||
} elseif ($this->template instanceof ZendViewRenderer) {
|
||||
$data['templateName'] = 'Zend View';
|
||||
$data['templateDocs'] = 'http://framework.zend.com/manual/current/en/modules/zend.view.quick-start.html';
|
||||
}
|
||||
|
||||
if (!$this->template) {
|
||||
return new JsonResponse([
|
||||
'welcome' => 'Congratulations! You have installed the zend-expressive skeleton application.',
|
||||
'docsUrl' => 'zend-expressive.readthedocs.org',
|
||||
]);
|
||||
}
|
||||
|
||||
return new HtmlResponse($this->template->render('app::home-page', $data));
|
||||
}
|
||||
}
|
||||
20
src/App/Action/HomePageFactory.php
Normal file
20
src/App/Action/HomePageFactory.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace App\Action;
|
||||
|
||||
use Interop\Container\ContainerInterface;
|
||||
use Zend\Expressive\Router\RouterInterface;
|
||||
use Zend\Expressive\Template\TemplateRendererInterface;
|
||||
|
||||
class HomePageFactory
|
||||
{
|
||||
public function __invoke(ContainerInterface $container)
|
||||
{
|
||||
$router = $container->get(RouterInterface::class);
|
||||
$template = ($container->has(TemplateRendererInterface::class))
|
||||
? $container->get(TemplateRendererInterface::class)
|
||||
: null;
|
||||
|
||||
return new HomePageAction($router, $template);
|
||||
}
|
||||
}
|
||||
17
src/App/Action/PingAction.php
Normal file
17
src/App/Action/PingAction.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\Action;
|
||||
|
||||
use Zend\Diactoros\Response\JsonResponse;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
|
||||
class PingAction
|
||||
{
|
||||
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||
{
|
||||
return new JsonResponse([
|
||||
'ack' => time(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
187
src/App/Service/SkiesService.php
Normal file
187
src/App/Service/SkiesService.php
Normal file
@@ -0,0 +1,187 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use Interop\Container\ContainerInterface;
|
||||
use Symfony\Component\CssSelector\CssSelectorConverter;
|
||||
use Zend\Http\Client;
|
||||
|
||||
/**
|
||||
* Class SkiesService
|
||||
*
|
||||
* @package App\Service
|
||||
* @todo handle errors in http response
|
||||
*/
|
||||
class SkiesService
|
||||
{
|
||||
const BASE_URI = 'http://skies.sigmatechnology.se/';
|
||||
|
||||
/**
|
||||
* @var Client
|
||||
*/
|
||||
private $httpClient;
|
||||
/**
|
||||
* @var ContainerInterface
|
||||
*/
|
||||
private $container;
|
||||
|
||||
public function __construct(ContainerInterface $container)
|
||||
{
|
||||
$this->container = $container;
|
||||
$this->httpClient = new Client();
|
||||
$headers = $this->httpClient->getRequest()->getHeaders();
|
||||
$headers->addHeaderLine('Authorization', 'Basic ' . $this->getAuthToken());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of all activities
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getActivityList()
|
||||
{
|
||||
$this->httpClient->setUri(self::BASE_URI . "main.asp?rID=2");
|
||||
$skiesResponse = $this->httpClient->send();
|
||||
$skiesHtmlBody = $skiesResponse->getBody();
|
||||
|
||||
$cssToXpathConverter = new CssSelectorConverter();
|
||||
$xpathQuery = $cssToXpathConverter->toXPath('html > body > div > div.row > div.col-md-10 > div.row > div.col-md-9 > div.box > table.table-striped > tr');
|
||||
|
||||
$doc = new \DOMDocument();
|
||||
$doc->loadHTML($skiesHtmlBody);
|
||||
|
||||
$xpath = new \DOMXPath($doc);
|
||||
$elements = $xpath->query($xpathQuery);
|
||||
|
||||
$parsed = [];
|
||||
/** @var \DOMNode $domElement */
|
||||
foreach ($elements as $domElement) {
|
||||
if($domElement->childNodes->item(0)->nodeName != 'td') {
|
||||
continue;
|
||||
}
|
||||
$url = $domElement->childNodes->item(0)->childNodes->item(0)->attributes->getNamedItem('href')->textContent;
|
||||
preg_match("/aktid=([0-9]+)/msi", $url, $match);
|
||||
$parsed[] = [
|
||||
'id' => (int)$match[1],
|
||||
'label' => $domElement->childNodes->item(0)->childNodes->item(0)->textContent,
|
||||
'date' => $domElement->childNodes->item(2)->textContent,
|
||||
'time' => trim($domElement->childNodes->item(3)->textContent, " \t\n\r\0\x0B\xc2\xa0"),
|
||||
];
|
||||
}
|
||||
|
||||
return $parsed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a single activity
|
||||
*
|
||||
* @param int $id
|
||||
* @return array
|
||||
*/
|
||||
public function getActivity(int $id)
|
||||
{
|
||||
$this->httpClient->setUri(self::BASE_URI . "main.asp?rID=2&alt=1&aktID=" . $id);
|
||||
$skiesResponse = $this->httpClient->send();
|
||||
$skiesHtmlBody = $skiesResponse->getBody();
|
||||
|
||||
$cssToXpathConverter = new CssSelectorConverter();
|
||||
$xpathQuery = $cssToXpathConverter->toXPath('div.container > div.row > div.col-md-10 > div.row > div.col-md-9 > div.box');
|
||||
$xpathCommentsQuery = $cssToXpathConverter->toXPath('div.container div.paragraph > div.onecomment');
|
||||
|
||||
$doc = new \DOMDocument();
|
||||
$doc->loadHTML($skiesHtmlBody);
|
||||
|
||||
$xpath = new \DOMXPath($doc);
|
||||
$elements = $xpath->query($xpathQuery);
|
||||
|
||||
$h5Elements = $elements->item(0)->getElementsByTagName('h5');
|
||||
$title = $h5Elements->item(0)->textContent;
|
||||
|
||||
$detailDivElements = $elements->item(0)->getElementsByTagName('div');
|
||||
$commentElements = $xpath->query($xpathCommentsQuery);
|
||||
|
||||
$eventDetails = $this->parseActivityDetails($detailDivElements->item(1)->textContent);
|
||||
$eventDescription = $this->parseActivityDescription($elements->item(0));
|
||||
$eventComments = $this->parseActivityComments($commentElements);
|
||||
|
||||
return [
|
||||
'title' => $title,
|
||||
'details' => $eventDetails,
|
||||
'description' => $eventDescription,
|
||||
'comments' => $eventComments,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return null|string
|
||||
* @todo real auth token from whatever...
|
||||
*/
|
||||
private function getAuthToken(): ?string
|
||||
{
|
||||
$config = $this->container->get('config');
|
||||
return $config['authKey'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the activity information details returning the date, time finaly entry and the person accountable
|
||||
*
|
||||
* @param string $divText
|
||||
* @return array|null
|
||||
*/
|
||||
private function parseActivityDetails(string $divText): ?array
|
||||
{
|
||||
preg_match("#Date:.*?([0-9]{4}-[0-9]{2}-[0-9]{2}).*?Time: ([0-9]{1,2}:[0-9]{2}).*?Final entry day:.*?([0-9]{4}-[0-9]{2}-[0-9]{2}).*?Accountable: (.*?) / ([0-9 +]*)#msi", $divText, $matches);
|
||||
return [
|
||||
'date' => $matches[1],
|
||||
'time' => $matches[2],
|
||||
'finalEntry' => $matches[3],
|
||||
'accountable' => str_replace(" ", " ", $matches[5] ? sprintf("%s (%s)", $matches[4], $matches[5]) : $matches[4]),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses comment block. Comment section is pretty much unformatted text
|
||||
* between a <hr> tag and a <form> tag in the passed $element
|
||||
*
|
||||
* @param \DOMElement $element
|
||||
* @return null|string
|
||||
*/
|
||||
private function parseActivityDescription(\DOMElement $element): ?string
|
||||
{
|
||||
$description = "";
|
||||
$isContent = false;
|
||||
/** @var \DOMElement $childNode */
|
||||
foreach($element->childNodes as $childNode) {
|
||||
if ($childNode->nodeName == "hr") { $isContent = true; }
|
||||
if ($childNode->nodeName == "form") { break; }
|
||||
if ($isContent) {
|
||||
$description .= $childNode->nodeName == "br"
|
||||
? "\n"
|
||||
: rtrim($childNode->textContent);
|
||||
}
|
||||
}
|
||||
return trim($description);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse $commentElements
|
||||
*
|
||||
* @param \DOMNodeList $commentElements
|
||||
* @return array|null
|
||||
*/
|
||||
private function parseActivityComments(\DOMNodeList $commentElements): ?array
|
||||
{
|
||||
$comments = [];
|
||||
/** @var \DOMElement $commentElement */
|
||||
foreach($commentElements as $commentElement) {
|
||||
preg_match("#(.*)\s/\s([0-9]{4}-[0-9]{2}-[0-9]{2})\s([0-9]{1,2}:[0-9]{1,2})#msi", str_replace(" "," ", $commentElement->getElementsByTagName("div")->item(2)->textContent), $matches);
|
||||
$comments[] = [
|
||||
'comment' => $commentElement->getElementsByTagName("div")->item(1)->textContent,
|
||||
'user' => $matches[1],
|
||||
'date' => $matches[2],
|
||||
'time' => $matches[3],
|
||||
];
|
||||
}
|
||||
return $comments;
|
||||
}
|
||||
}
|
||||
13
src/App/Service/SkiesServiceFactory.php
Normal file
13
src/App/Service/SkiesServiceFactory.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use Interop\Container\ContainerInterface;
|
||||
|
||||
class SkiesServiceFactory
|
||||
{
|
||||
public function __invoke(ContainerInterface $container)
|
||||
{
|
||||
return new SkiesService($container);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user