Skip to content

Commit

Permalink
[TASK] Improve code quality
Browse files Browse the repository at this point in the history
  • Loading branch information
georgringer committed Nov 20, 2018
1 parent 0c920bc commit ca74c7d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 33 deletions.
15 changes: 6 additions & 9 deletions Classes/Controller/AddressController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* The TYPO3 project - inspiring people to share!
*/

use FriendsOfTYPO3\TtAddress\Domain\Repository\AddressRepository;
use FriendsOfTYPO3\TtAddress\Utility\TypoScript;
use TYPO3\CMS\Core\Database\QueryGenerator;
use TYPO3\CMS\Core\Utility\GeneralUtility;
Expand All @@ -27,9 +28,7 @@
class AddressController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController
{

/**
* @var \FriendsOfTYPO3\TtAddress\Domain\Repository\AddressRepository
*/
/** @var AddressRepository */
protected $addressRepository;

/** @var QueryGenerator */
Expand Down Expand Up @@ -62,7 +61,7 @@ public function listAction()
{
// set singlePid if empty
if ($this->settings['singlePid'] == '') {
$this->settings['singlePid'] = \intval($GLOBALS['TSFE']->id);
$this->settings['singlePid'] = (int)$GLOBALS['TSFE']->id;
}

// set default sortBy to last_name, or singleSelection if singleRecords are there
Expand Down Expand Up @@ -125,9 +124,7 @@ public function listAction()
*
* @param ConfigurationManagerInterface $configurationManager Instance of the Configuration Manager
*/
public function injectConfigurationManager(
ConfigurationManagerInterface $configurationManager
)
public function injectConfigurationManager(ConfigurationManagerInterface $configurationManager)
{
$this->configurationManager = $configurationManager;

Expand Down Expand Up @@ -162,9 +159,9 @@ public function injectConfigurationManager(
}

/**
* @param \FriendsOfTYPO3\TtAddress\Domain\Repository\AddressRepository $addressRepository
* @param AddressRepository $addressRepository
*/
public function injectAddressRepository(\FriendsOfTYPO3\TtAddress\Domain\Repository\AddressRepository $addressRepository)
public function injectAddressRepository(AddressRepository $addressRepository)
{
$this->addressRepository = $addressRepository;
}
Expand Down
3 changes: 2 additions & 1 deletion Classes/Domain/Repository/AddressRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ protected function createCategoryConstraint(QueryInterface $query, $categories)
{
$constraints = [];

$categoriesRecursive = CategoryService::getChildrenCategories($categories);
$categoryService = GeneralUtility::makeInstance(CategoryService::class);
$categoriesRecursive = $categoryService->getChildrenCategories($categories);

if (!\is_array($categoriesRecursive)) {
$categoriesRecursive = GeneralUtility::intExplode(',', $categoriesRecursive, true);
Expand Down
43 changes: 22 additions & 21 deletions Classes/Service/CategoryService.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/

use TYPO3\CMS\Core\Cache\CacheManager;
use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
use TYPO3\CMS\Core\Database\Connection;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\TimeTracker\TimeTracker;
Expand All @@ -27,6 +28,18 @@
class CategoryService
{

/** @var TimeTracker */
protected $timeTracker;

/** @var FrontendInterface */
protected $cache;

public function __construct()
{
$this->timeTracker = GeneralUtility::makeInstance(TimeTracker::class);
$this->cache = GeneralUtility::makeInstance(CacheManager::class)->getCache('cache_ttaddress_category');
}

/**
* Get child categories by calling recursive function
* and using the caching framework to save some queries
Expand All @@ -36,18 +49,14 @@ class CategoryService
* @param string $additionalWhere additional where clause
* @return string comma separated list of category ids
*/
public static function getChildrenCategories(
$idList,
$counter = 0
) {
/** @var \TYPO3\CMS\Core\Cache\Frontend\FrontendInterface $cache */
$cache = GeneralUtility::makeInstance(CacheManager::class)->getCache('cache_ttaddress_category');
public function getChildrenCategories($idList, int $counter = 0)
{
$cacheIdentifier = sha1('children' . $idList);

$entry = $cache->get($cacheIdentifier);
$entry = $this->cache->get($cacheIdentifier);
if (!$entry) {
$entry = self::getChildrenCategoriesRecursive($idList, $counter);
$cache->set($cacheIdentifier, $entry);
$entry = $this->getChildrenCategoriesRecursive($idList, $counter);
$this->cache->set($cacheIdentifier, $entry);
}

return $entry;
Expand All @@ -60,11 +69,11 @@ public static function getChildrenCategories(
* @param int $counter
* @return string comma separated list of category ids
*/
private static function getChildrenCategoriesRecursive($idList, $counter = 0): string
protected function getChildrenCategoriesRecursive($idList, $counter = 0): string
{
$result = [];

// add id list to the output too
// add id list to the output
if ($counter === 0) {
$result[] = $idList;
}
Expand All @@ -82,22 +91,14 @@ private static function getChildrenCategoriesRecursive($idList, $counter = 0): s
while ($row = $res->fetch()) {
$counter++;
if ($counter > 10000) {
GeneralUtility::makeInstance(TimeTracker::class)->setTSlogMessage('EXT:news: one or more recursive categories where found');
$this->timeTracker->setTSlogMessage('EXT:news: one or more recursive categories where found');
return implode(',', $result);
}
$subcategories = self::getChildrenCategoriesRecursive($row['uid'], $counter);
$subcategories = $this->getChildrenCategoriesRecursive($row['uid'], $counter);
$result[] = $row['uid'] . ($subcategories ? ',' . $subcategories : '');
}

$result = implode(',', $result);
return $result;
}

/**
* @return TimeTracker
*/
protected static function getTimeTracker(): TimeTracker
{
return $GLOBALS['TT'];
}
}
2 changes: 0 additions & 2 deletions Tests/Unit/Controller/AddressControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ public function injectAddressRepositoryWorks()
$this->assertEquals($mockedRepository, $subject->_get('addressRepository'));
}


/**
* @test
*/
Expand Down Expand Up @@ -150,5 +149,4 @@ public function settingsAreProperlyInjected()
$subject->injectConfigurationManager($mockedConfigurationManager);
$this->assertEquals($expectedSettings, $subject->_get('settings'));
}

}

0 comments on commit ca74c7d

Please sign in to comment.