Add context to factory

The factory now accepts a context.
That allows to build different data based on that context.
The new feature is added in a backwards compatible way, just as an
optional addition.
This commit is contained in:
Daniel Siepmann 2021-09-07 16:28:59 +02:00
parent 77dd56ad43
commit 1e322668fa
Signed by: Daniel Siepmann
GPG key ID: 33D6629915560EF4
5 changed files with 208 additions and 1 deletions

View file

@ -25,7 +25,10 @@ use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Annotation as Extbase;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
use TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter;
use WerkraumMedia\Calendar\Domain\Model\Context;
use WerkraumMedia\Calendar\Domain\Model\ContextSpecificFactory;
use WerkraumMedia\Calendar\Domain\Model\Day;
use WerkraumMedia\Calendar\Domain\Model\ForeignDataFactory;
use WerkraumMedia\Calendar\Domain\Model\Month;
use WerkraumMedia\Calendar\Domain\Model\Week;
use WerkraumMedia\Calendar\Domain\Model\Year;
@ -33,6 +36,26 @@ use WerkraumMedia\Calendar\Events\AssignTemplateVariables;
class CalendarController extends ActionController
{
/**
* @var ForeignDataFactory
*/
private $foreignDataFactory;
public function __construct(
ForeignDataFactory $foreignDataFactory
) {
$this->foreignDataFactory = $foreignDataFactory;
}
public function initializeAction()
{
if ($this->foreignDataFactory instanceof ContextSpecificFactory) {
$this->foreignDataFactory->setContext(
Context::createFromContentObjectRenderer($this->configurationManager->getContentObject())
);
}
}
public function initializeYearAction()
{
if ($this->request->hasArgument('year') === false) {

View file

@ -0,0 +1,64 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2021 Daniel Siepmann <coding@daniel-siepmann.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
namespace WerkraumMedia\Calendar\Domain\Model;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
class Context
{
/**
* @var string
*/
protected $tableName = '';
/**
* @var array
*/
protected $databaseRow = [];
private function __construct()
{
}
public static function createFromContentObjectRenderer(
ContentObjectRenderer $contentObjectRenderer
): self {
$instance = new self();
$instance->tableName = $contentObjectRenderer->getCurrentTable();
$instance->databaseRow = $contentObjectRenderer->data;
return $instance;
}
public function getTableName(): string
{
return $this->tableName;
}
public function getDatabaseRow(): array
{
return $this->databaseRow;
}
}

View file

@ -0,0 +1,31 @@
<?php
namespace WerkraumMedia\Calendar\Domain\Model;
/*
* Copyright (C) 2020 Daniel Siepmann <coding@daniel-siepmann.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
interface ContextSpecificFactory
{
/**
* Might be called prior ot getData().
* Allows getData() to know the current context and make decisions.
*/
public function setContext(Context $context): void;
}

View file

@ -0,0 +1,88 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2021 Daniel Siepmann <coding@daniel-siepmann.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
namespace WerkraumMedia\Calendar\Tests\Unit\Domain\Model;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
use WerkraumMedia\Calendar\Domain\Model\Context;
/**
* @covers \WerkraumMedia\Calendar\Domain\Model\Context
*/
class ContextTest extends TestCase
{
use ProphecyTrait;
/**
* @test
*/
public function canNotBeCreatedViaNew(): void
{
$this->expectError();
$this->expectErrorMessage('Call to private WerkraumMedia\Calendar\Domain\Model\Context::__construct() from context \'WerkraumMedia\Calendar\Tests\Unit\Domain\Model\ContextTest\'');
$subject = new Context();
}
/**
* @test
*/
public function canBeCreatedFromContentObjectRenderer(): void
{
$contentObjectRenderer = $this->prophesize(ContentObjectRenderer::class);
$subject = Context::createFromContentObjectRenderer($contentObjectRenderer->reveal());
self::assertInstanceOf(Context::class, $subject);
}
/**
* @test
*/
public function providesTableNameInheritedFromContentObjectRenderer(): void
{
$contentObjectRenderer = $this->prophesize(ContentObjectRenderer::class);
$contentObjectRenderer->getCurrentTable()->willReturn('tx_calendar_example_table');
$subject = Context::createFromContentObjectRenderer($contentObjectRenderer->reveal());
self::assertSame('tx_calendar_example_table', $subject->getTableName());
}
/**
* @test
*/
public function providesDatabaseRowInheritedFromContentObjectRenderer(): void
{
$contentObjectRenderer = $this->prophesize(ContentObjectRenderer::class);
$contentObjectRenderer->data = [
'uid' => 10,
'pid' => 1,
];
$subject = Context::createFromContentObjectRenderer($contentObjectRenderer->reveal());
self::assertSame([
'uid' => 10,
'pid' => 1,
], $subject->getDatabaseRow());
}
}

View file

@ -15,7 +15,8 @@
"require": {
"php": "^7.3.0 || ^7.4.0",
"typo3/cms-core": "^10.4",
"typo3/cms-extbase": "^10.4"
"typo3/cms-extbase": "^10.4",
"typo3/cms-frontend": "^10.4"
},
"autoload": {
"psr-4": {