Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic booked api #1

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# EditorConfig is awesome: http://EditorConfig.org

# top-most EditorConfig file
root = true

# Unix-style newlines with a newline ending every file
[*]
charset = utf-8
end_of_line = lf
indent_style = space
indent_size = 4
insert_final_newline = true
trim_trailing_whitespace = true

# TS/JS-Files
[*.{ts,js}]
indent_size = 2

# JSON-Files
[*.json]
indent_style = tab

# ReST-Files
[*.rst]
indent_size = 4
max_line_length = 80

# YAML-Files
[*.{yaml,yml}]
indent_size = 2

# NEON-Files
[*.neon]
indent_size = 2
indent_style = tab

# package.json
[package.json]
indent_size = 2

# TypoScript
[*.{typoscript,tsconfig}]
indent_size = 2

# XLF-Files
[*.xlf]
indent_style = tab

# SQL-Files
[*.sql]
indent_style = tab
indent_size = 2

# .htaccess
[{_.htaccess,.htaccess}]
indent_style = tab
49 changes: 49 additions & 0 deletions Classes/Controller/BookedController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

/*
* This file is part of the package slub/slub-profile-booked
*
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/

namespace Slub\SlubProfileBooked\Controller;

use JsonException;
use Psr\Http\Message\ResponseInterface;
use Slub\SlubProfileBooked\Mvc\View\JsonView;
use Slub\SlubProfileBooked\Service\BookedService;
use TYPO3\CMS\Core\Context\Exception\AspectNotFoundException;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;

class BookedController extends ActionController
{
protected $view;
protected $defaultViewObjectName = JsonView::class;
protected BookedService $bookedService;

/**
* @param BookedService $bookedService
*/
public function __construct(BookedService $bookedService)
{
$this->bookedService = $bookedService;
}

/**
* @return ResponseInterface
* @throws AspectNotFoundException
* @throws JsonException
*/
public function listAction(): ResponseInterface
{
$booked['booked'] = $this->bookedService->getBooked($this->request->getArguments());

$this->view->setVariablesToRender(['bookedList']);
$this->view->assign('bookedList', $booked);

return $this->jsonResponse();
}
}
97 changes: 97 additions & 0 deletions Classes/Domain/Model/Dto/ApiConfiguration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

declare(strict_types=1);

/*
* This file is part of the package slub/slub-profile-booked
*
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/

namespace Slub\SlubProfileBooked\Domain\Model\Dto;

use Exception;
use Slub\SlubProfileBooked\Utility\ConstantsUtility;
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
use TYPO3\CMS\Core\Utility\GeneralUtility;

class ApiConfiguration
{
protected string $authenticationUsername = '';
protected string $authenticationPassword = '';
protected string $requestUri = '';

public function __construct()
{
$configuration = $this->getConfiguration(ConstantsUtility::EXTENSION_KEY);

empty($configuration['authenticationUsername']) ?: $this->setAuthenticationUsername($configuration['authenticationUsername']);
empty($configuration['authenticationPassword']) ?: $this->setAuthenticationPassword($configuration['authenticationPassword']);
empty($configuration['requestUri']) ?: $this->setRequestUri($configuration['requestUri']);
}

/**
* @return string
*/
public function getAuthenticationUsername(): string
{
return $this->authenticationUsername;
}

/**
* @param string $authenticationUsername
*/
public function setAuthenticationUsername(string $authenticationUsername = ''): void
{
$this->authenticationUsername = $authenticationUsername;
}

/**
* @return string
*/
public function getAuthenticationPassword(): string
{
return $this->authenticationPassword;
}

/**
* @param string $authenticationPassword
*/
public function setAuthenticationPassword(string $authenticationPassword = ''): void
{
$this->authenticationPassword = $authenticationPassword;
}

/**
* @return string
*/
public function getRequestUri(): string
{
return $this->requestUri;
}

/**
* @param string $requestUri
*/
public function setRequestUri(string $requestUri = ''): void
{
$this->requestUri = $requestUri;
}

/**
* @param string $extensionKey
* @return array
*/
protected function getConfiguration(string $extensionKey = ''): array
{
/** @var ExtensionConfiguration $extensionConfiguration */
$extensionConfiguration = GeneralUtility::makeInstance(ExtensionConfiguration::class);

try {
return $extensionConfiguration->get($extensionKey);
} catch (Exception $e) {
return [];
}
}
}
108 changes: 108 additions & 0 deletions Classes/Http/Request.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

declare(strict_types=1);

/*
* This file is part of the package slub/slub-profile-booked
*
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/

namespace Slub\SlubProfileBooked\Http;

use GuzzleHttp\Exception\RequestException;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Log\LoggerInterface;
use TYPO3\CMS\Core\Utility\ArrayUtility;

class Request
{
public array $options = [
'headers' => [
'Cache-Control' => 'no-cache'
],
'allow_redirects' => false
];
protected LoggerInterface $logger;
protected RequestFactoryInterface $requestFactory;

/**
* @param LoggerInterface $logger
* @param RequestFactoryInterface $requestFactory
*/
public function __construct(
LoggerInterface $logger,
RequestFactoryInterface $requestFactory
) {
$this->logger = $logger;
$this->requestFactory = $requestFactory;
}

/**
* @param string $uri
* @param string $method
* @param array $options
* @return array|null
*/
public function process(string $uri = '', string $method = 'GET', array $options = []): ?array
{
try {
$options = $this->mergeOptions($this->options, $options);
$response = $this->requestFactory->request($uri, $method, $options);

return $this->getContent($response, $uri);
} catch (RequestException $e) {
/** @extensionScannerIgnoreLine */
$this->logger->error($e->getMessage());

return null;
}
}

/**
* @param array $default
* @param array $new
* @return array
*/
protected function mergeOptions(array $default, array $new): array
{
if (count($new) > 0) {
ArrayUtility::mergeRecursiveWithOverrule($default, $new);
}

return $default;
}

/**
* @param ResponseInterface $response
* @param string $uri
* @return array|null
*/
protected function getContent(ResponseInterface $response, string $uri = ''): ?array
{
$content = '';

if ($response->getStatusCode() === 200 &&
strpos($response->getHeaderLine('Content-Type'), 'application/json') === 0
) {
$content = (array)json_decode($response->getBody()->getContents(), true);
}

if (empty($content)) {
$this->logger->warning(
'Requesting {request} was not successful, got status code {status} ({reason})',
[
'request' => $uri,
'status' => $response->getStatusCode(),
'reason' => $response->getReasonPhrase(),
]
);

return null;
}

return $content;
}
}
60 changes: 60 additions & 0 deletions Classes/Mvc/View/JsonView.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

/*
* This file is part of the package slub/slub-profile-booked
*
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/

namespace Slub\SlubProfileBooked\Mvc\View;

use TYPO3\CMS\Extbase\Mvc\View\JsonView as ExtbaseJsonView;

class JsonView extends ExtbaseJsonView
{
/**
* The rendering configuration for this JSON view which
* determines which properties of each variable to render.
* In default all data are given.
*
* You can exclude fields like:
*
* 'booked' => [
* '_descendAll' => [
* '_exclude' => [
* 'categories',
* 'contact',
* 'discipline'
* ]
* ]
* ]
*/
protected array $bookedConfiguration = [
'bookedList' => [
'_only' => [
'booked'
],
'booked' => [
'_descendAll' => [
'_only' => [
'referenceNumber',
'startDate',
'endDate',
'resourceName',
'title',
'description',
'duration'
],
]
]
]
];

public function __construct()
{
$this->setConfiguration($this->bookedConfiguration);
}
}
Loading