Skip to content

Commit

Permalink
[TASK] Reformat code according to TYPO3 Coding Guidelines
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicole Cordes committed Oct 29, 2015
1 parent e8ce704 commit b2b4a74
Show file tree
Hide file tree
Showing 8 changed files with 350 additions and 317 deletions.
110 changes: 57 additions & 53 deletions Classes/Controller/DomainController.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,66 +35,70 @@
/**
* Controller for backend module
*/
class DomainController extends ActionController {
class DomainController extends ActionController
{

/**
* @var DomainRepository;
*/
protected $domainRepository;
/**
* @var DomainRepository;
*/
protected $domainRepository;

/**
* @var PatternService
*/
protected $patternService;
/**
* @var PatternService
*/
protected $patternService;

/**
* View object name
*
* @var string
*/
protected $defaultViewObjectName = BackendTemplateView::class;
/**
* View object name
*
* @var string
*/
protected $defaultViewObjectName = BackendTemplateView::class;

/**
* @param DomainRepository $domainRepository
*/
public function injectDomainRepository(DomainRepository $domainRepository) {
$this->domainRepository = $domainRepository;
}
/**
* @param DomainRepository $domainRepository
*/
public function injectDomainRepository(DomainRepository $domainRepository)
{
$this->domainRepository = $domainRepository;
}

/**
* @param PatternService $patternService
* @return void
*/
public function injectPatternService(PatternService $patternService) {
$this->patternService = $patternService;
}
/**
* @param PatternService $patternService
* @return void
*/
public function injectPatternService(PatternService $patternService)
{
$this->patternService = $patternService;
}

/**
* @return void
*/
public function indexAction() {
$domains = $this->domainRepository->findAll();
if (!count($domains)) {
$domain = $this->objectManager->get(Domain::class);
$domain->setDomainName(GeneralUtility::getIndpEnv('HTTP_HOST'));
$domains = array($domain);
}
$pattern = $this->patternService->generatePattern($domains);
/**
* @return void
*/
public function indexAction()
{
$domains = $this->domainRepository->findAll();
if (!count($domains)) {
$domain = $this->objectManager->get(Domain::class);
$domain->setDomainName(GeneralUtility::getIndpEnv('HTTP_HOST'));
$domains = array($domain);
}
$pattern = $this->patternService->generatePattern($domains);

if ($this->request->hasArgument('write')) {
/** @var ConfigurationManager $configurationManager */
$configurationManager = $this->objectManager->get(ConfigurationManager::class);
$configurationManager->setLocalConfigurationValueByPath('SYS/trustedHostsPattern', $pattern);
$this->addFlashMessage(
htmlspecialchars('$GLOBALS[TYPO3_CONF_VARS][SYS][trustedHostsPattern] = ' . $pattern),
'Trusted Hosts Pattern was changed'
);
}
if ($this->request->hasArgument('write')) {
/** @var ConfigurationManager $configurationManager */
$configurationManager = $this->objectManager->get(ConfigurationManager::class);
$configurationManager->setLocalConfigurationValueByPath('SYS/trustedHostsPattern', $pattern);
$this->addFlashMessage(
htmlspecialchars('$GLOBALS[TYPO3_CONF_VARS][SYS][trustedHostsPattern] = ' . $pattern),
'Trusted Hosts Pattern was changed'
);
}

$this->view->assignMultiple(array(
'domains' => $domains,
'pattern' => $pattern,
));
}
$this->view->assignMultiple(array(
'domains' => $domains,
'pattern' => $pattern,
));
}

}
39 changes: 21 additions & 18 deletions Classes/Domain/Model/Domain.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,29 @@
/**
* Domain model
*/
class Domain extends AbstractEntity {
class Domain extends AbstractEntity
{

/**
* @var string
*/
protected $domainName;
/**
* @var string
*/
protected $domainName;

/**
* @return string
*/
public function getDomainName() {
return $this->domainName;
}
/**
* @return string
*/
public function getDomainName()
{
return $this->domainName;
}

/**
* @param string $domainName
* @return void
*/
public function setDomainName($domainName) {
$this->domainName = $domainName;
}
/**
* @param string $domainName
* @return void
*/
public function setDomainName($domainName)
{
$this->domainName = $domainName;
}

}
22 changes: 12 additions & 10 deletions Classes/Domain/Repository/DomainRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,18 @@
/**
* Repository for domain records
*/
class DomainRepository extends Repository {
class DomainRepository extends Repository
{

/**
* @return void
*/
public function initializeObject() {
/** @var QuerySettingsInterface $defaultQuerySettings */
$defaultQuerySettings = $this->objectManager->get(QuerySettingsInterface::class);
$defaultQuerySettings->setRespectStoragePage(FALSE);
$this->setDefaultQuerySettings($defaultQuerySettings);
}
/**
* @return void
*/
public function initializeObject()
{
/** @var QuerySettingsInterface $defaultQuerySettings */
$defaultQuerySettings = $this->objectManager->get(QuerySettingsInterface::class);
$defaultQuerySettings->setRespectStoragePage(false);
$this->setDefaultQuerySettings($defaultQuerySettings);
}

}
133 changes: 69 additions & 64 deletions Classes/Service/PatternService.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,77 +31,82 @@
/**
* Service to generate regular expression pattern for trustedHostsPattern
*/
class PatternService {
class PatternService
{

/**
* @param Domain[] $domainArray
* @return string
*/
public function generatePattern($domainArray) {
if (empty($domainArray)) {
$domain = new Domain();
$domain->setDomainName(GeneralUtility::getIndpEnv('HTTP_HOST'));
$domainArray = array($domain);
}
/**
* @param Domain[] $domainArray
* @return string
*/
public function generatePattern($domainArray)
{
if (empty($domainArray)) {
$domain = new Domain();
$domain->setDomainName(GeneralUtility::getIndpEnv('HTTP_HOST'));
$domainArray = array($domain);
}

$patternArray = array();
$extractedDomainsArray = $this->extractDomains($domainArray);
foreach ($extractedDomainsArray as $domainName => $subdomainArray) {
$patternArray[] = $this->getPatternForDomain($domainName, $subdomainArray);
}
unset($domain);
$patternArray = array();
$extractedDomainsArray = $this->extractDomains($domainArray);
foreach ($extractedDomainsArray as $domainName => $subdomainArray) {
$patternArray[] = $this->getPatternForDomain($domainName, $subdomainArray);
}
unset($domain);

return count($patternArray) > 1 ? '(' . implode('|', $patternArray) . ')' : $patternArray[0];
}
return count($patternArray) > 1 ? '(' . implode('|', $patternArray) . ')' : $patternArray[0];
}

/**
* @param Domain[] $domainArray
* @return array
*/
protected function extractDomains($domainArray) {
$extractedDomainArray = array();
/**
* @param Domain[] $domainArray
* @return array
*/
protected function extractDomains($domainArray)
{
$extractedDomainArray = array();

/** @var Domain $domain */
foreach ($domainArray as $domain) {
$reverseHost = array_reverse(explode('.', $domain->getDomainName()));
$domainName = preg_quote($reverseHost[1] . '.' . $reverseHost[0]);
unset($reverseHost[0], $reverseHost[1]);
$subdomain = '';
if (!empty($reverseHost)) {
$subdomain = implode('.', array_reverse($reverseHost));
}
if (!is_array($extractedDomainArray[$domainName])) {
$extractedDomainArray[$domainName] = array();
}
$extractedDomainArray[$domainName][] = preg_quote($subdomain);
}
unset($domain);
/** @var Domain $domain */
foreach ($domainArray as $domain) {
$reverseHost = array_reverse(explode('.', $domain->getDomainName()));
$domainName = preg_quote($reverseHost[1] . '.' . $reverseHost[0]);
unset($reverseHost[0], $reverseHost[1]);
$subdomain = '';
if (!empty($reverseHost)) {
$subdomain = implode('.', array_reverse($reverseHost));
}
if (!is_array($extractedDomainArray[$domainName])) {
$extractedDomainArray[$domainName] = array();
}
$extractedDomainArray[$domainName][] = preg_quote($subdomain);
}
unset($domain);

return $extractedDomainArray;
}
return $extractedDomainArray;
}

/**
* @param string $domainName
* @param array $subdomainArray
* @return string
*/
protected function getPatternForDomain($domainName, $subdomainArray) {
$hasDomainWithoutSubdomain = FALSE;
if (in_array('', $subdomainArray)) {
$hasDomainWithoutSubdomain = TRUE;
$subdomainArray = array_filter($subdomainArray, 'strlen');
}
$hasMultipleSubdomains = count($subdomainArray) > 1 ? TRUE : FALSE;
$pattern = ($hasMultipleSubdomains ? '(' : '') . implode('|', $subdomainArray) . ($hasMultipleSubdomains ? ')' : '');
if (!empty($subdomainArray)) {
$pattern .= '\\.';
}
if ($hasDomainWithoutSubdomain && !empty($subdomainArray)) {
$pattern = '(' . $pattern . ')?';
}
$pattern .= $domainName;
/**
* @param string $domainName
* @param array $subdomainArray
* @return string
*/
protected function getPatternForDomain($domainName, $subdomainArray)
{
$hasDomainWithoutSubdomain = false;
if (in_array('', $subdomainArray)) {
$hasDomainWithoutSubdomain = true;
$subdomainArray = array_filter($subdomainArray, 'strlen');
}
$hasMultipleSubdomains = count($subdomainArray) > 1 ? true : false;
$pattern = ($hasMultipleSubdomains ? '(' : '') . implode('|',
$subdomainArray) . ($hasMultipleSubdomains ? ')' : '');
if (!empty($subdomainArray)) {
$pattern .= '\\.';
}
if ($hasDomainWithoutSubdomain && !empty($subdomainArray)) {
$pattern = '(' . $pattern . ')?';
}
$pattern .= $domainName;

return $pattern;
}
return $pattern;
}

}
Loading

0 comments on commit b2b4a74

Please sign in to comment.