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

refactor deprecated function and support dependency injections #42

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from
59 changes: 6 additions & 53 deletions Classes/Controller/RedirectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,21 @@

use Psr\Http\Message\ResponseInterface;
use In2code\Ipandlanguageredirect\Domain\Service\RedirectService;
use In2code\Ipandlanguageredirect\Utility\FrontendUtility;
use In2code\Ipandlanguageredirect\Utility\ObjectUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\HttpUtility;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
use TYPO3\CMS\Extbase\Mvc\Exception\InvalidArgumentNameException;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;

/**
* Class RedirectController
*/
class RedirectController extends ActionController
{
public function __construct(
private readonly RedirectService $redirectService
) {}

/**
* @var array
*/
protected $testArguments = [
[
'browserLanguage' => 'de',
'referrer' => 'http://www.google.de?foo=bar',
'ipAddress' => '192.168.0.1',
'languageUid' => '0',
'rootpageUid' => '1'
],
[
'browserLanguage' => 'de',
'referrer' => 'http://www.google.de?foo=bar',
'ipAddress' => '',
'languageUid' => '0',
'rootpageUid' => '1'
]
];

/**
* Enrich call with ip-address if not given
Expand Down Expand Up @@ -79,8 +62,7 @@ public function redirectAction(
string $domain = ''
): ResponseInterface {
/** @noinspection PhpMethodParametersCountMismatchInspection */
$redirectService = $this->objectManager->get(
RedirectService::class,
$this->redirectService->set(
$browserLanguage,
$referrer,
$ipAddress,
Expand All @@ -89,35 +71,6 @@ public function redirectAction(
$countryCode,
$domain
);
return $this->jsonResponse(json_encode($redirectService->buildParameters()));
}

/**
* Test the redirectAction directly with some predefined parameters from a given set
* call index.php?id=2&type=1556&tx_ipandlanguageredirect_pi1[set]=1
*
* @param int $set
* @return void
*/
public function testAction($set = 0): ResponseInterface
{
$configuration = [
'parameter' => ObjectUtility::getTyposcriptFrontendController()->id,
'additionalParams' =>
FrontendUtility::getParametersStringFromArray($this->testArguments[$set]) . '&type=1555'
];
$uri = ObjectUtility::getContentObject()->typoLink_URL($configuration);
HttpUtility::redirect($uri, HttpUtility::HTTP_STATUS_307);
return $this->htmlResponse();
}

/**
* Render a suggest container that can be slided down in FE
*
* @return void
*/
public function suggestAction(): ResponseInterface
{
return $this->htmlResponse();
return $this->jsonResponse(json_encode($this->redirectService->buildParameters()));
}
}
26 changes: 26 additions & 0 deletions Classes/Controller/SuggestController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace In2code\Ipandlanguageredirect\Controller;

use Psr\Http\Message\ResponseInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\HttpUtility;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
use TYPO3\CMS\Extbase\Mvc\Exception\InvalidArgumentNameException;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;

/**
* Class RedirectController
*/
class SuggestController extends ActionController
{
/**
* Render a suggest container that can be slided down in FE
*
* @return void
*/
public function suggestAction(): ResponseInterface
{
return $this->htmlResponse();
}
}
75 changes: 75 additions & 0 deletions Classes/Controller/TestController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace In2code\Ipandlanguageredirect\Controller;

use Psr\Http\Message\ResponseInterface;
use In2code\Ipandlanguageredirect\Utility\FrontendUtility;
use In2code\Ipandlanguageredirect\Utility\ObjectUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\HttpUtility;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
use TYPO3\CMS\Extbase\Mvc\Exception\InvalidArgumentNameException;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
use TYPO3\CMS\Extbase\Http\ForwardResponse;

/**+
* Class RedirectController
*/
class TestController extends ActionController
{

/**
* @var array
*/
protected $testArguments = [
[
'browserLanguage' => 'de',
'referrer' => 'http://www.google.de?foo=bar',
'ipAddress' => '192.168.0.1',
'languageUid' => '0',
'rootpageUid' => '1'
],
[
'browserLanguage' => 'de',
'referrer' => 'http://www.google.de?foo=bar',
'ipAddress' => '',
'languageUid' => '0',
'rootpageUid' => '1'
]
];

public function __construct(
private readonly ContentObjectRenderer $contentObjectRenderer
)
{
}

/**
* Test the redirectAction directly with some predefined parameters from a given set
* call index.php?id=2&type=1556&tx_ipandlanguageredirect_pi1[set]=1
*
* @param int $set
* @return void
*/
public function testAction($set = 0): ResponseInterface
{
$configuration = [
'parameter' => ObjectUtility::getTyposcriptFrontendController()->id,
'additionalParams' =>
FrontendUtility::getParametersStringFromArray($this->testArguments[$set]) . '&type=1555'
];

$uri = $this->contentObjectRenderer->typoLink_URL($configuration);

// Create redirect response
$response = $this->responseFactory
->createResponse(303)
->withAddedHeader('location', $uri);

// Return Response directly
return $response;

// or throw PropagateResponseException
throw new PropagateResponseException($response);
}
}
3 changes: 2 additions & 1 deletion Classes/Domain/Model/ActionSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace In2code\Ipandlanguageredirect\Domain\Model;

use In2code\Ipandlanguageredirect\Utility\ObjectUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
* Class ActionSet
Expand Down Expand Up @@ -34,7 +35,7 @@ public function __construct(array $configuration)
$this->rawQuantifierConfiguration = $configuration['quantifier'];
$this->rawActionsConfiguration = $configuration['actions'];
foreach ($this->rawActionsConfiguration as $actionConfiguration) {
$action = ObjectUtility::getObjectManager()->get(
$action = GeneralUtility::makeInstance(
Action::class,
$actionConfiguration
);
Expand Down
3 changes: 2 additions & 1 deletion Classes/Domain/Model/ConfigurationSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace In2code\Ipandlanguageredirect\Domain\Model;

use In2code\Ipandlanguageredirect\Utility\ObjectUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
* Class ConfigurationSet
Expand Down Expand Up @@ -44,7 +45,7 @@ public function __construct(array $configuration, int $rootpageUid)
$this->rawRedirectConfiguration = $configuration['redirectConfiguration'];
foreach ($this->rawRedirectConfiguration as $pageIdentifier => $treeConfiguration) {
foreach ($treeConfiguration as $languageParameter => $setConfiguration) {
$configuration = ObjectUtility::getObjectManager()->get(
$configuration = GeneralUtility::makeInstance(
Configuration::class,
$pageIdentifier,
$languageParameter,
Expand Down
4 changes: 3 additions & 1 deletion Classes/Domain/Service/IpToCountry.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
use In2code\Ipandlanguageredirect\Domain\Service\IpToCountry\IpToCountryInterface;
use In2code\Ipandlanguageredirect\Utility\ConfigurationUtility;
use In2code\Ipandlanguageredirect\Utility\ObjectUtility;
use Psr\Container\ContainerInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Object\Container\Container as ExtbaseContainer;

/**
* Class IpToCountry
Expand All @@ -25,7 +27,7 @@ public function getCountryFromIp(string $ipAddress = ''): string
$countryCode = '';
foreach ($this->getClasses() as $class) {
/** @var IpToCountryInterface $countryFromIp */
$countryFromIp = ObjectUtility::getObjectManager()->get($class, $ipAddress);
$countryFromIp = GeneralUtility::makeInstance($class, $ipAddress);
try {
$countryCode = $countryFromIp->getCountryCodeFromIp();
} catch (\Exception $exception) {
Expand Down
8 changes: 4 additions & 4 deletions Classes/Domain/Service/IpToCountry/AbstractIpToCountry.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
declare(strict_types=1);
namespace In2code\Ipandlanguageredirect\Domain\Service\IpToCountry;

use In2code\Ipandlanguageredirect\Utility\ObjectUtility;
use TYPO3\CMS\Core\SingletonInterface;
use TYPO3\CMS\Core\Utility\ArrayUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;

/**
* Class AbstractIpToCountry
*/
abstract class AbstractIpToCountry
abstract class AbstractIpToCountry implements SingletonInterface
{

/**
Expand All @@ -23,7 +23,7 @@ abstract class AbstractIpToCountry
/**
* @param string $ipAddress
*/
public function __construct(string $ipAddress)
public function __construct(string $ipAddress = '')
{
$this->ipAddress = $ipAddress;
}
Expand All @@ -48,7 +48,7 @@ protected function getCurrentIpAddress(): string
protected function getConfiguration(string $className, string $path = '')
{
$settings = [];
$configurationManager = ObjectUtility::getObjectManager()->get(ConfigurationManagerInterface::class);
$configurationManager = GeneralUtility::makeInstance(ConfigurationManagerInterface::class);
$setup = $configurationManager->getConfiguration(
ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS,
'Ipandlanguageredirect'
Expand Down
22 changes: 13 additions & 9 deletions Classes/Domain/Service/RedirectService.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ class RedirectService
'events' => ['none']
];


public function __construct(
private readonly UriBuilder $uriBuilder
) {}

/**
* @param string $browserLanguage
* @param string $referrer
Expand All @@ -113,7 +118,7 @@ class RedirectService
* @param string $countryCode
* @param string $domain
*/
public function __construct(
public function set(
string $browserLanguage,
string $referrer,
string $ipAddress,
Expand All @@ -129,7 +134,7 @@ public function __construct(
$this->rootpageUid = $rootpageUid;
$this->countryCodeOverlay = $countryCode;
if ($this->countryCodeOverlay === '') {
$ipToCountry = ObjectUtility::getObjectManager()->get(IpToCountry::class);
$ipToCountry = GeneralUtility::makeInstance(IpToCountry::class);
$this->countryCode = $ipToCountry->getCountryFromIp($ipAddress);
} else {
$this->countryCode = $this->countryCodeOverlay;
Expand Down Expand Up @@ -214,7 +219,7 @@ protected function getBestMatchingLanguageParameter()
protected function getBestConfiguration()
{
if ($this->bestConfiguration === null) {
$configurationSet = ObjectUtility::getObjectManager()->get(
$configurationSet = GeneralUtility::makeInstance(
ConfigurationSet::class,
$this->configuration,
$this->rootpageUid
Expand All @@ -238,11 +243,10 @@ protected function getBestConfiguration()
*/
protected function getUriToPageAndLanguage($pageIdentifier = 0, $languageParameter = 0): string
{
$uriBuilder = ObjectUtility::getObjectManager()->get(UriBuilder::class);
$uriBuilder->setTargetPageUid($this->getTargetPageForUriCreation($pageIdentifier));
$uriBuilder->setCreateAbsoluteUri(true);
$uriBuilder->setArguments([$this->languageParameter => $languageParameter]);
return $uriBuilder->buildFrontendUri();
$this->uriBuilder->setTargetPageUid($this->getTargetPageForUriCreation($pageIdentifier));
$this->uriBuilder->setCreateAbsoluteUri(true);
$this->uriBuilder->setArguments([$this->languageParameter => $languageParameter]);
return $this->uriBuilder->buildFrontendUri();
}

/**
Expand All @@ -251,7 +255,7 @@ protected function getUriToPageAndLanguage($pageIdentifier = 0, $languageParamet
protected function getEvents()
{
if ($this->bestEvents === null) {
$actionSet = ObjectUtility::getObjectManager()->get(ActionSet::class, $this->configuration);
$actionSet = GeneralUtility::makeInstance(ActionSet::class, $this->configuration);
$actionSet->calculateQuantifiers($this->referrer);
$events = $actionSet->getEvents();
} else {
Expand Down
18 changes: 0 additions & 18 deletions Classes/Utility/ObjectUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
use TYPO3\CMS\Core\Log\Logger;
use TYPO3\CMS\Core\Log\LogManager;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;

/**
Expand All @@ -14,14 +12,6 @@
class ObjectUtility
{

/**
* @return ObjectManager
*/
public static function getObjectManager()
{
return GeneralUtility::makeInstance(ObjectManager::class);
}

/**
* @param string $className
* @return Logger
Expand All @@ -31,14 +21,6 @@ public static function getLogger(string $className): Logger
return GeneralUtility::makeInstance(LogManager::class)->getLogger($className);
}

/**
* @return ContentObjectRenderer
*/
public static function getContentObject()
{
return self::getObjectManager()->get(ContentObjectRenderer::class);
}

/**
* @return TypoScriptFrontendController
* @SuppressWarnings(PHPMD.Superglobals)
Expand Down
8 changes: 8 additions & 0 deletions Configuration/Services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
services:
_defaults:
autowire: true
autoconfigure: true
public: false

In2code\Ipandlanguageredirect\:
resource: '../Classes/*'
Loading