Use symfony
This commit is contained in:
parent
f3205fe53d
commit
7efe021fc0
10
.env
10
.env
@ -1 +1,9 @@
|
||||
APP_ENV=dev
|
||||
APP_ENV=dev
|
||||
###> symfony/framework-bundle ###
|
||||
APP_ENV=dev
|
||||
APP_SECRET=akwhdaeuifzuieshuikjfjk
|
||||
###< symfony/framework-bundle ###
|
||||
|
||||
###> application ###
|
||||
REQUIRED_API_KEY=74857389798572903480209489024
|
||||
###< application ###
|
||||
|
||||
4
.env.dev
Normal file
4
.env.dev
Normal file
@ -0,0 +1,4 @@
|
||||
|
||||
###> symfony/framework-bundle ###
|
||||
APP_SECRET=8195be78f871a87592025f227ca9c3a6
|
||||
###< symfony/framework-bundle ###
|
||||
11
.gitignore
vendored
11
.gitignore
vendored
@ -3,4 +3,13 @@
|
||||
.env.test.local
|
||||
.idea
|
||||
vendor
|
||||
var/
|
||||
var/
|
||||
###> symfony/framework-bundle ###
|
||||
/.env.local
|
||||
/.env.local.php
|
||||
/.env.*.local
|
||||
/config/secrets/prod/prod.decrypt.private.php
|
||||
/public/bundles/
|
||||
/var/
|
||||
/vendor/
|
||||
###< symfony/framework-bundle ###
|
||||
|
||||
28
README.md
Normal file
28
README.md
Normal file
@ -0,0 +1,28 @@
|
||||
# Work Calendar
|
||||
|
||||
A PHP application for managing work calendar and absences.
|
||||
|
||||
## Project Structure
|
||||
|
||||
The project follows a clean architecture pattern with the following components:
|
||||
|
||||
- **Model**: Contains domain models like `Absence`
|
||||
- **Service**: Contains service classes for business logic
|
||||
- `AbsenceClient`: Handles API communication with the absence.io service
|
||||
- `AbsenceManager`: Manages absence data retrieval and caching
|
||||
- `CalendarService`: Handles calendar generation and processing
|
||||
|
||||
## Recent Improvements
|
||||
|
||||
- Added proper domain model for `Absence` to encapsulate absence data and behavior
|
||||
- Improved service classes with better separation of concerns
|
||||
- Added caching for improved performance
|
||||
- Improved error handling in API client
|
||||
- Removed code duplication
|
||||
- Added early returns for cleaner code flow
|
||||
|
||||
## Requirements
|
||||
|
||||
- PHP 8.1 or higher
|
||||
- Composer
|
||||
- Absence.io API credentials (KEY and ID)
|
||||
21
bin/console
Executable file
21
bin/console
Executable file
@ -0,0 +1,21 @@
|
||||
#!/usr/bin/env php
|
||||
<?php
|
||||
|
||||
use App\Kernel;
|
||||
use Symfony\Bundle\FrameworkBundle\Console\Application;
|
||||
|
||||
if (!is_dir(dirname(__DIR__).'/vendor')) {
|
||||
throw new LogicException('Dependencies are missing. Try running "composer install".');
|
||||
}
|
||||
|
||||
if (!is_file(dirname(__DIR__).'/vendor/autoload_runtime.php')) {
|
||||
throw new LogicException('Symfony Runtime is missing. Try running "composer require symfony/runtime".');
|
||||
}
|
||||
|
||||
require_once dirname(__DIR__).'/vendor/autoload_runtime.php';
|
||||
|
||||
return function (array $context) {
|
||||
$kernel = new Kernel($context['APP_ENV'], (bool) $context['APP_DEBUG']);
|
||||
|
||||
return new Application($kernel);
|
||||
};
|
||||
@ -1,16 +1,45 @@
|
||||
{
|
||||
"type": "project",
|
||||
"license": "proprietary",
|
||||
"minimum-stability": "stable",
|
||||
"prefer-stable": true,
|
||||
"require": {
|
||||
"php": "^8.2",
|
||||
"ext-intl": "*",
|
||||
"dflydev/hawk": "^0.0.0",
|
||||
"guzzlehttp/guzzle": "^7.9",
|
||||
"symfony/console": "^7.1",
|
||||
"symfony/dotenv": "^7.1",
|
||||
"symfony/flex": "^2.4",
|
||||
"symfony/framework-bundle": "^7.1",
|
||||
"symfony/http-client": "^7.1",
|
||||
"symfony/http-foundation": "^7.1",
|
||||
"symfony/http-kernel": "^7.1",
|
||||
"symfony/runtime": "^7.1",
|
||||
"symfony/string": "^7.1",
|
||||
"symfony/twig-bundle": "^7.1",
|
||||
"symfony/yaml": "^7.1"
|
||||
},
|
||||
"config": {
|
||||
"allow-plugins": {
|
||||
"symfony/flex": true,
|
||||
"symfony/runtime": true
|
||||
},
|
||||
"sort-packages": true
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"App\\": "src/"
|
||||
}
|
||||
},
|
||||
"require": {
|
||||
"php": "^8.2",
|
||||
"symfony/http-client": "^7.1",
|
||||
"dflydev/hawk": "^0.0.0",
|
||||
"guzzlehttp/guzzle": "^7.9",
|
||||
"symfony/dotenv": "^7.1",
|
||||
"symfony/cache": "^7.1",
|
||||
"ext-intl": "*"
|
||||
"scripts": {
|
||||
"auto-scripts": {
|
||||
"cache:clear": "symfony-cmd",
|
||||
"assets:install %PUBLIC_DIR%": "symfony-cmd"
|
||||
}
|
||||
},
|
||||
"require-dev": {
|
||||
"symfony/stopwatch": "^7.1",
|
||||
"symfony/web-profiler-bundle": "^7.1"
|
||||
}
|
||||
}
|
||||
|
||||
2533
composer.lock
generated
2533
composer.lock
generated
File diff suppressed because it is too large
Load Diff
7
config/bundles.php
Normal file
7
config/bundles.php
Normal file
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
return [
|
||||
Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
|
||||
Symfony\Bundle\TwigBundle\TwigBundle::class => ['all' => true],
|
||||
Symfony\Bundle\WebProfilerBundle\WebProfilerBundle::class => ['dev' => true, 'test' => true],
|
||||
];
|
||||
19
config/packages/cache.yaml
Normal file
19
config/packages/cache.yaml
Normal file
@ -0,0 +1,19 @@
|
||||
framework:
|
||||
cache:
|
||||
# Unique name of your app: used to compute stable namespaces for cache keys.
|
||||
#prefix_seed: your_vendor_name/app_name
|
||||
|
||||
# The "app" cache stores to the filesystem by default.
|
||||
# The data in this cache should persist between deploys.
|
||||
# Other options include:
|
||||
|
||||
# Redis
|
||||
#app: cache.adapter.redis
|
||||
#default_redis_provider: redis://localhost
|
||||
|
||||
# APCu (not recommended with heavy random-write workloads as memory fragmentation can cause perf issues)
|
||||
#app: cache.adapter.apcu
|
||||
|
||||
# Namespaced pools use the above "app" backend by default
|
||||
#pools:
|
||||
#my.dedicated.cache: null
|
||||
11
config/packages/framework.yaml
Normal file
11
config/packages/framework.yaml
Normal file
@ -0,0 +1,11 @@
|
||||
framework:
|
||||
secret: '%env(APP_SECRET)%'
|
||||
http_method_override: false
|
||||
handle_all_throwables: true
|
||||
session:
|
||||
handler_id: null
|
||||
cookie_secure: auto
|
||||
cookie_samesite: lax
|
||||
storage_factory_id: session.storage.factory.native
|
||||
php_errors:
|
||||
log: true
|
||||
10
config/packages/routing.yaml
Normal file
10
config/packages/routing.yaml
Normal file
@ -0,0 +1,10 @@
|
||||
framework:
|
||||
router:
|
||||
# Configure how to generate URLs in non-HTTP contexts, such as CLI commands.
|
||||
# See https://symfony.com/doc/current/routing.html#generating-urls-in-commands
|
||||
#default_uri: http://localhost
|
||||
|
||||
when@prod:
|
||||
framework:
|
||||
router:
|
||||
strict_requirements: null
|
||||
6
config/packages/twig.yaml
Normal file
6
config/packages/twig.yaml
Normal file
@ -0,0 +1,6 @@
|
||||
twig:
|
||||
file_name_pattern: '*.twig'
|
||||
|
||||
when@test:
|
||||
twig:
|
||||
strict_variables: true
|
||||
17
config/packages/web_profiler.yaml
Normal file
17
config/packages/web_profiler.yaml
Normal file
@ -0,0 +1,17 @@
|
||||
when@dev:
|
||||
web_profiler:
|
||||
toolbar: true
|
||||
intercept_redirects: false
|
||||
|
||||
framework:
|
||||
profiler:
|
||||
only_exceptions: false
|
||||
collect_serializer_data: true
|
||||
|
||||
when@test:
|
||||
web_profiler:
|
||||
toolbar: false
|
||||
intercept_redirects: false
|
||||
|
||||
framework:
|
||||
profiler: { collect: false }
|
||||
5
config/preload.php
Normal file
5
config/preload.php
Normal file
@ -0,0 +1,5 @@
|
||||
<?php
|
||||
|
||||
if (file_exists(dirname(__DIR__).'/var/cache/prod/App_KernelProdContainer.preload.php')) {
|
||||
require dirname(__DIR__).'/var/cache/prod/App_KernelProdContainer.preload.php';
|
||||
}
|
||||
5
config/routes.yaml
Normal file
5
config/routes.yaml
Normal file
@ -0,0 +1,5 @@
|
||||
controllers:
|
||||
resource:
|
||||
path: ../src/Controller/
|
||||
namespace: App\Controller
|
||||
type: attribute
|
||||
4
config/routes/framework.yaml
Normal file
4
config/routes/framework.yaml
Normal file
@ -0,0 +1,4 @@
|
||||
when@dev:
|
||||
_errors:
|
||||
resource: '@FrameworkBundle/Resources/config/routing/errors.xml'
|
||||
prefix: /_error
|
||||
8
config/routes/web_profiler.yaml
Normal file
8
config/routes/web_profiler.yaml
Normal file
@ -0,0 +1,8 @@
|
||||
when@dev:
|
||||
web_profiler_wdt:
|
||||
resource: '@WebProfilerBundle/Resources/config/routing/wdt.xml'
|
||||
prefix: /_wdt
|
||||
|
||||
web_profiler_profiler:
|
||||
resource: '@WebProfilerBundle/Resources/config/routing/profiler.xml'
|
||||
prefix: /_profiler
|
||||
3
config/secrets/dev/dev.ABSENCE_API_ID.082918.php
Normal file
3
config/secrets/dev/dev.ABSENCE_API_ID.082918.php
Normal file
@ -0,0 +1,3 @@
|
||||
<?php // dev.ABSENCE_API_ID.082918 on Thu, 13 Mar 2025 20:12:07 +0000
|
||||
|
||||
return "i\xBF\x5D\x96J\xCF\x88\xD8\x8D7S\x9C\xBB\x2F3\x92\x18\xB4h\xF8f\xD8\xF5o\x95\x06\xD8b\xE2H\x0F\x21\xCA\xE8\xDB\xA7\x3A\xEB\x2CKk\xA8\xD3\xDA\xFC9b\xB8\x99\x1D\xC4\xC7\x8D\x2Bt\xFF\xED\x8BRzK\x83yAg\xE2\x3A\xBDH\x0Ay\xFF";
|
||||
3
config/secrets/dev/dev.ABSENCE_API_KEY.4be562.php
Normal file
3
config/secrets/dev/dev.ABSENCE_API_KEY.4be562.php
Normal file
@ -0,0 +1,3 @@
|
||||
<?php // dev.ABSENCE_API_KEY.4be562 on Thu, 13 Mar 2025 20:11:36 +0000
|
||||
|
||||
return "\x947\xDC\x11\xD9\x24\xC2\xA3v\x9D\x99n\x09\xC8\xFA\x5E\x60\x20R\x99\xD3\xC4\x2A\xEB\xB5-q\x26\xF3_\xF6k\x0D\xF9\x21\xFF\x93\x03Yv\x23h\x9A\xF8a\xEFK\x02\xE3\xEAH\x3Fr\xC1\xC4Z\x1AE\x18\x16\x7BF\xDEQ\x81A\x1Bq\x9EU\xD5P\xC2s\xAF\xDA\x3D\x8D\x05\xE8\xC1\x89\xC9f\x97\xE3\xF1z\x5B\x1Eh\x9A\xE7\x22u\x29\xDF\xBCN\xDC\x14\xC3\x8D\xB5\x12\xF3\xAFc\xFBxy\xAD";
|
||||
4
config/secrets/dev/dev.decrypt.private.php
Normal file
4
config/secrets/dev/dev.decrypt.private.php
Normal file
@ -0,0 +1,4 @@
|
||||
<?php // dev.decrypt.private on Thu, 13 Mar 2025 20:11:36 +0000
|
||||
|
||||
// SYMFONY_DECRYPTION_SECRET=fnNDES+LQ6IxMFuIw7u5QqqZJLKXdAt5yssDGUGUtgb0VQirIhdET3MVCC9bFDtfocoXorP8I/sxNz4WPNC2Jw==
|
||||
return "~sC\x11\x2F\x8BC\xA210\x5B\x88\xC3\xBB\xB9B\xAA\x99\x24\xB2\x97t\x0By\xCA\xCB\x03\x19A\x94\xB6\x06\xF4U\x08\xAB\x22\x17DOs\x15\x08\x2F\x5B\x14\x3B_\xA1\xCA\x17\xA2\xB3\xFC\x23\xFB17\x3E\x16\x3C\xD0\xB6\x27";
|
||||
3
config/secrets/dev/dev.encrypt.public.php
Normal file
3
config/secrets/dev/dev.encrypt.public.php
Normal file
@ -0,0 +1,3 @@
|
||||
<?php // dev.encrypt.public on Thu, 13 Mar 2025 20:11:36 +0000
|
||||
|
||||
return "\xF4U\x08\xAB\x22\x17DOs\x15\x08\x2F\x5B\x14\x3B_\xA1\xCA\x17\xA2\xB3\xFC\x23\xFB17\x3E\x16\x3C\xD0\xB6\x27";
|
||||
6
config/secrets/dev/dev.list.php
Normal file
6
config/secrets/dev/dev.list.php
Normal file
@ -0,0 +1,6 @@
|
||||
<?php
|
||||
|
||||
return [
|
||||
'ABSENCE_API_ID' => null,
|
||||
'ABSENCE_API_KEY' => null,
|
||||
];
|
||||
16
config/services.yaml
Normal file
16
config/services.yaml
Normal file
@ -0,0 +1,16 @@
|
||||
services:
|
||||
_defaults:
|
||||
autowire: true
|
||||
autoconfigure: true
|
||||
public: false
|
||||
|
||||
App\:
|
||||
resource: '../src/'
|
||||
exclude:
|
||||
- '../src/DependencyInjection/'
|
||||
- '../src/Entity/'
|
||||
- '../src/Kernel.php'
|
||||
|
||||
App\Controller\CalendarController:
|
||||
arguments:
|
||||
$requiredApiKey: '%env(REQUIRED_API_KEY)%'
|
||||
147
public/index.php
147
public/index.php
@ -1,146 +1,9 @@
|
||||
<html lang="de">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Arbeitstage FTK - Tim Lappe</title>
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<style>
|
||||
.type-urlaub {
|
||||
background-color: rgb(166, 199, 203);
|
||||
}
|
||||
.type-ftk {
|
||||
background-color: rgb(0, 124, 0);
|
||||
color: white;
|
||||
box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.5);
|
||||
z-index: 10;
|
||||
position: relative;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<?php
|
||||
|
||||
if (!isset($_GET['key']) || $_GET['key'] !== "74857389798572903480209489024") {
|
||||
echo "Kein Zugriff";
|
||||
return;
|
||||
}
|
||||
use App\Kernel;
|
||||
|
||||
ini_set('display_errors', 1);
|
||||
require_once dirname(__DIR__).'/vendor/autoload_runtime.php';
|
||||
|
||||
use App\App;
|
||||
|
||||
require_once __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
Locale::setDefault('de_DE');
|
||||
setlocale(LC_ALL, "de_DE"); //only necessary if the locale isn't already set
|
||||
|
||||
$formatter = new IntlDateFormatter(
|
||||
'de_DE',
|
||||
IntlDateFormatter::FULL,
|
||||
IntlDateFormatter::FULL,
|
||||
'Europe/Berlin',
|
||||
IntlDateFormatter::GREGORIAN,
|
||||
'EEEE'
|
||||
);
|
||||
|
||||
$app = new App();
|
||||
$app->init();
|
||||
|
||||
$data = ($app->getAbsenceClient())->getAbsences([
|
||||
'skip' => 0,
|
||||
'limit' => 50,
|
||||
'filter' => [
|
||||
'start' => ['$gte' => (new DateTime('-1 day'))->format('Y-m-d\TH:i:s.u\Z')],
|
||||
'assignedTo:user._id' => [
|
||||
'email' => 'tim.lappe@check24.de'
|
||||
]
|
||||
],
|
||||
'sortBy' => [
|
||||
'start' => 1
|
||||
],
|
||||
'relations' => ['assignedToId', 'reasonId', 'approverId']
|
||||
]);
|
||||
|
||||
$data = $data['data'];
|
||||
|
||||
// Funktion zum Erstellen einer Liste aller Tage in einem bestimmten Zeitraum
|
||||
function getAllDays($startDate, $endDate) {
|
||||
$period = new DatePeriod(
|
||||
new DateTime($startDate),
|
||||
new DateInterval('P1D'),
|
||||
(new DateTime($endDate))->modify('+1 day')
|
||||
);
|
||||
|
||||
$days = [];
|
||||
foreach ($period as $date) {
|
||||
$days[] = $date->format('Y-m-d');
|
||||
}
|
||||
return $days;
|
||||
}
|
||||
|
||||
// Beispiel: Zeitraum eines Monats (kann angepasst werden)
|
||||
$startDate = (new DateTime())->format('Y-m-d');
|
||||
$endDate = (new DateTime('+90 days'))->format('Y-m-d');
|
||||
$allDays = getAllDays($startDate, $endDate);
|
||||
|
||||
?>
|
||||
|
||||
<div class="container mt-5">
|
||||
<h1>Arbeitstage FTK - Tim Lappe</h1>
|
||||
<p>Zeitraum: <?= $startDate; ?> - <?= $endDate; ?></p>
|
||||
<table class="table table-borderless" style="table-layout: fixed">
|
||||
<thead>
|
||||
<tr>
|
||||
<th style="width: 100px">Datum</th>
|
||||
<th>Wochentag</th>
|
||||
<th>Typ</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php foreach ($allDays as $day): ?>
|
||||
<?php
|
||||
$reason = '';
|
||||
$isFtk = false;
|
||||
$isUrlaub = false;
|
||||
$class = 'bg-light text-dark';
|
||||
|
||||
// Prüfen, ob es für diesen Tag eine Abwesenheit gibt
|
||||
foreach ($data as $absence) {
|
||||
foreach ($absence['days'] as $absenceDay) {
|
||||
if ((new DateTime($absenceDay['date']))->format('Y-m-d') === $day) {
|
||||
$reason = $absence['reason']['name'];
|
||||
$isFtk = str_contains($reason, 'mobile Arbeit');
|
||||
$isUrlaub = str_contains($reason, 'Urlaub') || str_contains($reason, 'Sonderurlaub');
|
||||
$class = $isFtk ? 'type-ftk' : '';
|
||||
$class = $isUrlaub ? 'type-urlaub' : $class;
|
||||
break 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
?>
|
||||
<?php if (DateTimeImmutable::createFromFormat('Y-m-d', $day)->format('N') == 6): ?>
|
||||
<tr>
|
||||
<td></td>
|
||||
<td></td>
|
||||
</tr>
|
||||
<?php continue; ?>
|
||||
<?php endif; ?>
|
||||
<?php if (DateTimeImmutable::createFromFormat('Y-m-d', $day)->format('N') == 7): ?>
|
||||
<tr class="bg-white text-secondary">
|
||||
<td colspan="2">KW <?php echo ((int) (new DateTime($day))->format('W')) + 1; ?></td>
|
||||
</tr>
|
||||
<?php continue; ?>
|
||||
<?php endif; ?>
|
||||
<tr class="<?= $class ?>">
|
||||
<td><?= (new DateTime($day))->format('d.m.Y'); ?></td>
|
||||
<td><?= $formatter->format((new DateTime($day))) ?></td>
|
||||
<td><?php if ($isFtk): ?>FTK<?php endif; ?> <?php if($isUrlaub): ?>Urlaub<?php endif; ?></td>
|
||||
</tr>
|
||||
|
||||
<?php endforeach; ?>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
return function (array $context) {
|
||||
return new Kernel($context['APP_ENV'], (bool) $context['APP_DEBUG']);
|
||||
};
|
||||
|
||||
@ -1,70 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App;
|
||||
|
||||
use Dflydev\Hawk\Client\ClientBuilder;
|
||||
use Dflydev\Hawk\Credentials\Credentials;
|
||||
use GuzzleHttp\Client as Guzzle;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
use GuzzleHttp\Psr7\Request;
|
||||
use Psr\Cache\InvalidArgumentException;
|
||||
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
|
||||
|
||||
class AbsenceClient
|
||||
{
|
||||
private Guzzle $client;
|
||||
|
||||
private FilesystemAdapter $cache;
|
||||
|
||||
public function __construct(private string $key, private string $id)
|
||||
{
|
||||
$this->client = new Guzzle();
|
||||
$this->cache = new FilesystemAdapter(defaultLifetime: 3600, directory: __DIR__ . '/../var/cache');
|
||||
}
|
||||
|
||||
private function getRequest(string $method, string $path, array $data): Request
|
||||
{
|
||||
$credentials = new Credentials($this->key, 'sha256', $this->id);
|
||||
$client = ClientBuilder::create()->build();
|
||||
$request = $client->createRequest(
|
||||
$credentials,
|
||||
$path,
|
||||
$method,
|
||||
['content-type' => 'application/json']
|
||||
);
|
||||
|
||||
return new Request($method, $path, [
|
||||
'Authorization' => $request->header()->fieldValue(),
|
||||
'Content-Type' => 'application/json',
|
||||
], json_encode($data));
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public function getAbsences(array $filter): array
|
||||
{
|
||||
$request = $this->cache->get("get_absences", function ($item) use ($filter) {
|
||||
echo "Cache miss\n";
|
||||
$item->expiresAfter(3600);
|
||||
|
||||
$request = $this->getRequest('POST', 'https://app.absence.io/api/v2/absences', $filter);
|
||||
try {
|
||||
$res = $this->client->send($request);
|
||||
} catch (GuzzleException $e) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return json_decode($res->getBody()->getContents(), true);
|
||||
});
|
||||
|
||||
$this->cache->commit();
|
||||
|
||||
return $request;
|
||||
}
|
||||
|
||||
public function getClient(): Guzzle
|
||||
{
|
||||
return $this->client;
|
||||
}
|
||||
}
|
||||
24
src/App.php
24
src/App.php
@ -1,24 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App;
|
||||
|
||||
use Symfony\Component\Dotenv\Dotenv;
|
||||
|
||||
class App
|
||||
{
|
||||
private AbsenceClient $absenceClient;
|
||||
|
||||
public function init(): void
|
||||
{
|
||||
$dotenv = new Dotenv();
|
||||
$dotenv->load(__DIR__ . '/../.env');
|
||||
$dotenv->overload(__DIR__ . '/../.env.local');
|
||||
|
||||
$this->absenceClient = new AbsenceClient($_ENV['ABSENCE_KEY'], $_ENV['ABSENCE_ID']);
|
||||
}
|
||||
|
||||
public function getAbsenceClient(): AbsenceClient
|
||||
{
|
||||
return $this->absenceClient;
|
||||
}
|
||||
}
|
||||
43
src/Controller/CalendarController.php
Normal file
43
src/Controller/CalendarController.php
Normal file
@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Service\AbsenceManager;
|
||||
use App\Service\CalendarService;
|
||||
use DateTime;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
|
||||
final class CalendarController extends AbstractController
|
||||
{
|
||||
public function __construct(
|
||||
private readonly AbsenceManager $absenceManager,
|
||||
private readonly CalendarService $calendarService,
|
||||
private readonly string $requiredApiKey
|
||||
) {
|
||||
}
|
||||
|
||||
#[Route('/', name: 'calendar_index')]
|
||||
public function index(Request $request): Response
|
||||
{
|
||||
if (!$request->query->has('key') || $request->query->get('key') !== $this->requiredApiKey) {
|
||||
return new Response('Kein Zugriff', Response::HTTP_FORBIDDEN);
|
||||
}
|
||||
|
||||
$startDate = (new DateTime())->format('Y-m-d');
|
||||
$endDate = (new DateTime('+90 days'))->format('Y-m-d');
|
||||
|
||||
$absences = $this->absenceManager->getAbsencesForUser('tim.lappe@check24.de', new DateTime('-1 day'));
|
||||
|
||||
$days = $this->calendarService->getAllDays($startDate, $endDate);
|
||||
$days = $this->calendarService->processAbsences($days, $absences);
|
||||
|
||||
return $this->render('calendar/index.html.twig', [
|
||||
'startDate' => new DateTime($startDate),
|
||||
'endDate' => new DateTime($endDate),
|
||||
'days' => $days,
|
||||
]);
|
||||
}
|
||||
}
|
||||
11
src/Kernel.php
Normal file
11
src/Kernel.php
Normal file
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace App;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
|
||||
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
|
||||
|
||||
class Kernel extends BaseKernel
|
||||
{
|
||||
use MicroKernelTrait;
|
||||
}
|
||||
107
src/Model/Absence.php
Normal file
107
src/Model/Absence.php
Normal file
@ -0,0 +1,107 @@
|
||||
<?php
|
||||
|
||||
namespace App\Model;
|
||||
|
||||
use DateTime;
|
||||
|
||||
final class Absence
|
||||
{
|
||||
private array $days = [];
|
||||
private ?string $reasonName = null;
|
||||
|
||||
public function __construct(
|
||||
private readonly string $id,
|
||||
private readonly DateTime $startDate,
|
||||
private readonly DateTime $endDate,
|
||||
private readonly string $userId,
|
||||
private readonly string $userEmail,
|
||||
private readonly array $reason = []
|
||||
) {
|
||||
if (isset($reason['name'])) {
|
||||
$this->reasonName = $reason['name'];
|
||||
}
|
||||
}
|
||||
|
||||
public function getId(): string
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function getStartDate(): DateTime
|
||||
{
|
||||
return $this->startDate;
|
||||
}
|
||||
|
||||
public function getEndDate(): DateTime
|
||||
{
|
||||
return $this->endDate;
|
||||
}
|
||||
|
||||
public function getUserId(): string
|
||||
{
|
||||
return $this->userId;
|
||||
}
|
||||
|
||||
public function getUserEmail(): string
|
||||
{
|
||||
return $this->userEmail;
|
||||
}
|
||||
|
||||
public function getReason(): array
|
||||
{
|
||||
return $this->reason;
|
||||
}
|
||||
|
||||
public function getReasonName(): ?string
|
||||
{
|
||||
return $this->reasonName;
|
||||
}
|
||||
|
||||
public function setDays(array $days): self
|
||||
{
|
||||
$this->days = $days;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getDays(): array
|
||||
{
|
||||
return $this->days;
|
||||
}
|
||||
|
||||
public function isWorkFromHome(): bool
|
||||
{
|
||||
if ($this->reasonName === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return str_contains($this->reasonName, 'mobile Arbeit');
|
||||
}
|
||||
|
||||
public function isVacation(): bool
|
||||
{
|
||||
if ($this->reasonName === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return str_contains($this->reasonName, 'Urlaub') ||
|
||||
str_contains($this->reasonName, 'Sonderurlaub');
|
||||
}
|
||||
|
||||
public static function fromArray(array $data): self
|
||||
{
|
||||
$absence = new self(
|
||||
$data['_id'] ?? '',
|
||||
isset($data['start']) ? new DateTime($data['start']) : new DateTime(),
|
||||
isset($data['end']) ? new DateTime($data['end']) : new DateTime(),
|
||||
$data['assignedTo']['user']['_id'] ?? '',
|
||||
$data['assignedTo']['user']['email'] ?? '',
|
||||
$data['reason'] ?? []
|
||||
);
|
||||
|
||||
if (isset($data['days'])) {
|
||||
$absence->setDays($data['days']);
|
||||
}
|
||||
|
||||
return $absence;
|
||||
}
|
||||
}
|
||||
85
src/Service/AbsenceClient.php
Normal file
85
src/Service/AbsenceClient.php
Normal file
@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use Dflydev\Hawk\Client\ClientBuilder;
|
||||
use Dflydev\Hawk\Credentials\Credentials;
|
||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\ResponseInterface;
|
||||
use Symfony\Contracts\Cache\CacheInterface;
|
||||
use Symfony\Contracts\Cache\ItemInterface;
|
||||
|
||||
final class AbsenceClient
|
||||
{
|
||||
private const BASE_URL = 'https://app.absence.io/api/v2';
|
||||
|
||||
public function __construct(
|
||||
private readonly HttpClientInterface $client,
|
||||
private readonly CacheInterface $cache,
|
||||
|
||||
#[Autowire('%env(ABSENCE_API_KEY)%')]
|
||||
private readonly string $key,
|
||||
|
||||
#[Autowire('%env(ABSENCE_API_ID)%')]
|
||||
private readonly string $id
|
||||
) {
|
||||
}
|
||||
|
||||
public function getAbsences(array $params): array
|
||||
{
|
||||
try {
|
||||
$response = $this->sendRequest(
|
||||
'POST',
|
||||
self::BASE_URL . '/absences',
|
||||
$params
|
||||
);
|
||||
|
||||
return $response->toArray();
|
||||
} catch (TransportExceptionInterface $e) {
|
||||
return ['data' => []];
|
||||
}
|
||||
}
|
||||
|
||||
public function getReasons(array $params): array
|
||||
{
|
||||
$cacheKey = 'reasons_' . md5(serialize($params));
|
||||
|
||||
return $this->cache->get($cacheKey, function (ItemInterface $item) use ($params) {
|
||||
$item->expiresAfter(3600);
|
||||
|
||||
try {
|
||||
$response = $this->sendRequest(
|
||||
'POST',
|
||||
self::BASE_URL . '/reasons',
|
||||
$params
|
||||
);
|
||||
|
||||
return $response->toArray();
|
||||
} catch (TransportExceptionInterface $e) {
|
||||
return ['data' => []];
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private function sendRequest(string $method, string $url, ?array $body = null): ResponseInterface
|
||||
{
|
||||
$credentials = new Credentials($this->key, 'sha256', $this->id);
|
||||
$client = ClientBuilder::create()->build();
|
||||
$request = $client->createRequest(
|
||||
$credentials,
|
||||
$url,
|
||||
$method,
|
||||
['content-type' => 'application/json']
|
||||
);
|
||||
|
||||
return $this->client->request($method, $url, [
|
||||
'headers' => [
|
||||
'Authorization' => $request->header()->fieldValue(),
|
||||
'Content-Type' => 'application/json'
|
||||
],
|
||||
'json' => $body
|
||||
]);
|
||||
}
|
||||
}
|
||||
51
src/Service/AbsenceManager.php
Normal file
51
src/Service/AbsenceManager.php
Normal file
@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use App\Model\Absence;
|
||||
use DateTime;
|
||||
use Symfony\Contracts\Cache\CacheInterface;
|
||||
use Symfony\Contracts\Cache\ItemInterface;
|
||||
|
||||
final class AbsenceManager
|
||||
{
|
||||
public function __construct(
|
||||
private readonly AbsenceClient $absenceClient,
|
||||
private readonly CacheInterface $cache
|
||||
) {
|
||||
}
|
||||
|
||||
public function getAbsencesForUser(string $email, ?DateTime $startDate = null): array
|
||||
{
|
||||
$startDate ??= new DateTime('-1 day');
|
||||
$cacheKey = base64_encode(sprintf('absences_%s_%s', $email, $startDate->format('Y-m-d')));
|
||||
|
||||
return $this->cache->get($cacheKey, function (ItemInterface $item) use ($email, $startDate) {
|
||||
$item->expiresAfter(3600);
|
||||
|
||||
$response = $this->absenceClient->getAbsences([
|
||||
'skip' => 0,
|
||||
'limit' => 50,
|
||||
'filter' => [
|
||||
'start' => ['$gte' => $startDate->format('Y-m-d\TH:i:s.u\Z')],
|
||||
'assignedTo:user._id' => [
|
||||
'email' => $email
|
||||
]
|
||||
],
|
||||
'sortBy' => [
|
||||
'start' => 1
|
||||
],
|
||||
'relations' => ['reasonId']
|
||||
]);
|
||||
|
||||
if (!isset($response['data']) || !is_array($response['data'])) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return array_map(
|
||||
fn (array $absenceData): Absence => Absence::fromArray($absenceData),
|
||||
$response['data']
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
102
src/Service/CalendarService.php
Normal file
102
src/Service/CalendarService.php
Normal file
@ -0,0 +1,102 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use App\Model\Absence;
|
||||
use DateTime;
|
||||
use DateTimeImmutable;
|
||||
use DatePeriod;
|
||||
use DateInterval;
|
||||
use IntlDateFormatter;
|
||||
|
||||
final class CalendarService
|
||||
{
|
||||
private IntlDateFormatter $formatter;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->formatter = new IntlDateFormatter(
|
||||
'de_DE',
|
||||
IntlDateFormatter::FULL,
|
||||
IntlDateFormatter::FULL,
|
||||
'Europe/Berlin',
|
||||
IntlDateFormatter::GREGORIAN,
|
||||
'EEEE'
|
||||
);
|
||||
}
|
||||
|
||||
public function getAllDays(string $startDate, string $endDate): array
|
||||
{
|
||||
$period = new DatePeriod(
|
||||
new DateTime($startDate),
|
||||
new DateInterval('P1D'),
|
||||
(new DateTime($endDate))->modify('+1 day')
|
||||
);
|
||||
|
||||
$days = [];
|
||||
foreach ($period as $date) {
|
||||
$dateObj = DateTimeImmutable::createFromInterface($date);
|
||||
$days[] = [
|
||||
'date' => $date,
|
||||
'weekday' => (int) $dateObj->format('N'),
|
||||
'weekdayName' => $this->formatter->format($date),
|
||||
'weekNumber' => (int) $dateObj->format('W'),
|
||||
'class' => 'bg-light text-dark',
|
||||
'isFtk' => false,
|
||||
'isUrlaub' => false
|
||||
];
|
||||
}
|
||||
|
||||
return $days;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $days
|
||||
* @param Absence[] $absences
|
||||
* @return array
|
||||
*/
|
||||
public function processAbsences(array &$days, array $absences): array
|
||||
{
|
||||
if (empty($absences)) {
|
||||
return $days;
|
||||
}
|
||||
|
||||
foreach ($days as &$day) {
|
||||
foreach ($absences as $absence) {
|
||||
$isAbsenceForDay = $this->processAbsenceForDay($day, $absence);
|
||||
if ($isAbsenceForDay) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $days;
|
||||
}
|
||||
|
||||
private function processAbsenceForDay(array &$day, Absence $absence): bool
|
||||
{
|
||||
foreach ($absence->getDays() as $absenceDay) {
|
||||
if (!isset($absenceDay['date']) || !$this->isSameDay($day['date'], new DateTime($absenceDay['date']))) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$day['isFtk'] = $absence->isWorkFromHome();
|
||||
$day['isUrlaub'] = $absence->isVacation();
|
||||
|
||||
if ($day['isFtk']) {
|
||||
$day['class'] = 'type-ftk';
|
||||
} elseif ($day['isUrlaub']) {
|
||||
$day['class'] = 'type-urlaub';
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private function isSameDay(DateTime $date1, DateTime $date2): bool
|
||||
{
|
||||
return $date1->format('Y-m-d') === $date2->format('Y-m-d');
|
||||
}
|
||||
}
|
||||
85
symfony.lock
Normal file
85
symfony.lock
Normal file
@ -0,0 +1,85 @@
|
||||
{
|
||||
"symfony/console": {
|
||||
"version": "7.2",
|
||||
"recipe": {
|
||||
"repo": "github.com/symfony/recipes",
|
||||
"branch": "main",
|
||||
"version": "5.3",
|
||||
"ref": "1781ff40d8a17d87cf53f8d4cf0c8346ed2bb461"
|
||||
},
|
||||
"files": [
|
||||
"bin/console"
|
||||
]
|
||||
},
|
||||
"symfony/flex": {
|
||||
"version": "2.5",
|
||||
"recipe": {
|
||||
"repo": "github.com/symfony/recipes",
|
||||
"branch": "main",
|
||||
"version": "2.4",
|
||||
"ref": "52e9754527a15e2b79d9a610f98185a1fe46622a"
|
||||
},
|
||||
"files": [
|
||||
".env",
|
||||
".env.dev"
|
||||
]
|
||||
},
|
||||
"symfony/framework-bundle": {
|
||||
"version": "7.2",
|
||||
"recipe": {
|
||||
"repo": "github.com/symfony/recipes",
|
||||
"branch": "main",
|
||||
"version": "7.2",
|
||||
"ref": "87bcf6f7c55201f345d8895deda46d2adbdbaa89"
|
||||
},
|
||||
"files": [
|
||||
"config/packages/cache.yaml",
|
||||
"config/packages/framework.yaml",
|
||||
"config/preload.php",
|
||||
"config/routes/framework.yaml",
|
||||
"config/services.yaml",
|
||||
"public/index.php",
|
||||
"src/Controller/.gitignore",
|
||||
"src/Kernel.php"
|
||||
]
|
||||
},
|
||||
"symfony/routing": {
|
||||
"version": "7.2",
|
||||
"recipe": {
|
||||
"repo": "github.com/symfony/recipes",
|
||||
"branch": "main",
|
||||
"version": "7.0",
|
||||
"ref": "21b72649d5622d8f7da329ffb5afb232a023619d"
|
||||
},
|
||||
"files": [
|
||||
"config/packages/routing.yaml",
|
||||
"config/routes.yaml"
|
||||
]
|
||||
},
|
||||
"symfony/twig-bundle": {
|
||||
"version": "7.2",
|
||||
"recipe": {
|
||||
"repo": "github.com/symfony/recipes",
|
||||
"branch": "main",
|
||||
"version": "6.4",
|
||||
"ref": "cab5fd2a13a45c266d45a7d9337e28dee6272877"
|
||||
},
|
||||
"files": [
|
||||
"config/packages/twig.yaml",
|
||||
"templates/base.html.twig"
|
||||
]
|
||||
},
|
||||
"symfony/web-profiler-bundle": {
|
||||
"version": "7.2",
|
||||
"recipe": {
|
||||
"repo": "github.com/symfony/recipes",
|
||||
"branch": "main",
|
||||
"version": "6.1",
|
||||
"ref": "e42b3f0177df239add25373083a564e5ead4e13a"
|
||||
},
|
||||
"files": [
|
||||
"config/packages/web_profiler.yaml",
|
||||
"config/routes/web_profiler.yaml"
|
||||
]
|
||||
}
|
||||
}
|
||||
16
templates/base.html.twig
Normal file
16
templates/base.html.twig
Normal file
@ -0,0 +1,16 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>{% block title %}Welcome!{% endblock %}</title>
|
||||
<link rel="icon" href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 128 128%22><text y=%221.2em%22 font-size=%2296%22>⚫️</text><text y=%221.3em%22 x=%220.2em%22 font-size=%2276%22 fill=%22%23fff%22>sf</text></svg>">
|
||||
{% block stylesheets %}
|
||||
{% endblock %}
|
||||
|
||||
{% block javascripts %}
|
||||
{% endblock %}
|
||||
</head>
|
||||
<body>
|
||||
{% block body %}{% endblock %}
|
||||
</body>
|
||||
</html>
|
||||
87
templates/calendar/index.html.twig
Normal file
87
templates/calendar/index.html.twig
Normal file
@ -0,0 +1,87 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="de">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Arbeitskalender Tim Lappe</title>
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<style>
|
||||
.type-urlaub {
|
||||
background-color: rgb(166, 199, 203);
|
||||
}
|
||||
.type-ftk {
|
||||
background-color: rgb(0, 124, 0);
|
||||
color: white;
|
||||
z-index: 10;
|
||||
position: relative;
|
||||
}
|
||||
.calendar-grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(7, 1fr);
|
||||
gap: 4px;
|
||||
}
|
||||
.calendar-day {
|
||||
padding: 10px;
|
||||
border: 1px solid #dee2e6;
|
||||
min-height: 100px;
|
||||
}
|
||||
.calendar-header {
|
||||
text-align: center;
|
||||
font-weight: bold;
|
||||
padding: 10px;
|
||||
background-color: #f8f9fa;
|
||||
}
|
||||
.calendar-week {
|
||||
grid-column: 1 / -1;
|
||||
background-color: #f8f9fa;
|
||||
padding: 5px;
|
||||
text-align: left;
|
||||
color: #6c757d;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container mt-5">
|
||||
<h3 class="fw-bold mb-3">Arbeitskalender Tim Lappe</h3>
|
||||
<div class="calendar-grid">
|
||||
<div class="calendar-header">Mo</div>
|
||||
<div class="calendar-header">Di</div>
|
||||
<div class="calendar-header">Mi</div>
|
||||
<div class="calendar-header">Do</div>
|
||||
<div class="calendar-header">Fr</div>
|
||||
<div class="calendar-header">Sa</div>
|
||||
<div class="calendar-header">So</div>
|
||||
|
||||
{% set currentMonth = null %}
|
||||
{% set firstDay = days|first %}
|
||||
{% if firstDay.weekday > 1 %}
|
||||
{% if currentMonth != firstDay.date|date('F') %}
|
||||
{% set currentMonth = firstDay.date|date('F') %}
|
||||
<div class="calendar-week" style="background-color: #e9ecef;">{{ firstDay.date|date('F Y') }}</div>
|
||||
{% endif %}
|
||||
{% for i in 1..firstDay.weekday - 1 %}
|
||||
<div class="calendar-day"></div>
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
|
||||
{% for day in days %}
|
||||
{% if currentMonth != day.date|date('F') %}
|
||||
{% set currentMonth = day.date|date('F') %}
|
||||
<div class="calendar-week" style="background-color: #e9ecef;">{{ day.date|date('F Y') }}</div>
|
||||
{% endif %}
|
||||
<div class="calendar-day {{ day.class }}">
|
||||
<div>{{ day.date|date('d.m.Y') }}</div>
|
||||
{% if day.isFtk %}<div>FTK</div>{% endif %}
|
||||
{% if day.isUrlaub %}<div>Urlaub</div>{% endif %}
|
||||
</div>
|
||||
{% endfor %}
|
||||
|
||||
{% if days|last.weekday < 7 %}
|
||||
{% for i in days|last.weekday + 1..7 %}
|
||||
<div class="calendar-day"></div>
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
Loading…
x
Reference in New Issue
Block a user