Skip to content

Commit

Permalink
Refactoring - coding standards (#402)
Browse files Browse the repository at this point in the history
* Refactoring - coding standards

* Add escaping to templates

* CSS coding styles
  • Loading branch information
Jan Petr authored Jan 23, 2018
1 parent 8fd90d0 commit 58899f7
Show file tree
Hide file tree
Showing 46 changed files with 1,150 additions and 533 deletions.
4 changes: 3 additions & 1 deletion Adapter/Algolia.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,9 @@ private function isReplaceAdvancedSearch($storeId)

private function getDocument20($document)
{
return new \Magento\Framework\Search\Document($document['entity_id'], ['score' => new \Magento\Framework\Search\DocumentField('score', $document['score'])]);
return new \Magento\Framework\Search\Document($document['entity_id'], [
'score' => new \Magento\Framework\Search\DocumentField('score', $document['score']),
]);
}

private function getDocument21($document)
Expand Down
9 changes: 6 additions & 3 deletions Block/Algolia.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,14 @@ public function getGroupId()
public function getPriceKey()
{
if ($this->priceKey === null) {
$groupId = $this->getGroupId();
$currencyCode = $this->getCurrencyCode();

$this->priceKey = $this->config->isCustomerGroupsEnabled($this->getStore()->getStoreId()) ? '.' . $currencyCode . '.group_' . $groupId : '.' . $currencyCode . '.default';
$this->priceKey = '.' . $currencyCode . '.default';

if ($this->config->isCustomerGroupsEnabled($this->getStore()->getStoreId())) {
$groupId = $this->getGroupId();
$this->priceKey = '.' . $currencyCode . '.group_' . $groupId;;
}
}

return $this->priceKey;
Expand Down Expand Up @@ -170,4 +174,3 @@ private function getAddToCartUrl($additional = [])
return $this->_urlBuilder->getUrl('checkout/cart/add', $routeParams);
}
}

16 changes: 11 additions & 5 deletions Block/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ public function getConfiguration()
/**
* Handle category replacement
*/
if ($config->isInstantEnabled() && $config->replaceCategories() && $request->getControllerName() == 'category') {
if ($config->isInstantEnabled()
&& $config->replaceCategories()
&& $request->getControllerName() == 'category') {
$category = $this->getCurrentCategory();

if ($category && $category->getDisplayMode() !== 'PAGE') {
Expand Down Expand Up @@ -112,8 +114,7 @@ public function getConfiguration()
if ($refinementKey !== null) {
$refinementValue = $query;
$query = "";
}
else {
} else {
$refinementKey = "";
}
}
Expand Down Expand Up @@ -145,11 +146,16 @@ public function getConfiguration()
'extensionVersion' => $config->getExtensionVersion(),
'applicationId' => $config->getApplicationID(),
'indexName' => $coreHelper->getBaseIndexName(),
'apiKey' => $algoliaHelper->generateSearchSecuredApiKey($config->getSearchOnlyAPIKey(), $config->getAttributesToRetrieve($customerGroupId)),
'apiKey' => $algoliaHelper->generateSearchSecuredApiKey(
$config->getSearchOnlyAPIKey(),
$config->getAttributesToRetrieve($customerGroupId)
),
'facets' => $facets,
'areCategoriesInFacets' => $areCategoriesInFacets,
'hitsPerPage' => (int) $config->getNumberOfProductResults(),
'sortingIndices' => array_values($config->getSortingIndices($coreHelper->getIndexName($productHelper->getIndexNameSuffix()))),
'sortingIndices' => array_values($config->getSortingIndices(
$coreHelper->getIndexName($productHelper->getIndexNameSuffix())
)),
'isSearchPage' => $this->isSearchPage(),
'isCategoryPage' => $isCategoryPage,
'removeBranding' => (bool) $config->isRemoveBranding(),
Expand Down
3 changes: 2 additions & 1 deletion Block/Instant/Hit.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public function getPriceKey()
$store = $this->_storeManager->getStore();

$currencyCode = $store->getCurrentCurrencyCode();
$this->priceKey = $this->config->isCustomerGroupsEnabled($store->getStoreId()) ? '.' . $currencyCode . '.group_' . $groupId : '.' . $currencyCode . '.default';
$this->priceKey = $this->config->isCustomerGroupsEnabled($store->getStoreId())
? '.' . $currencyCode . '.group_' . $groupId : '.' . $currencyCode . '.default';
}

return $this->priceKey;
Expand Down
8 changes: 6 additions & 2 deletions Controller/Adminhtml/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ abstract class Queue extends Action
protected $tableName;
protected $db;

public function __construct(Context $context, ConfigHelper $configHelper, JsonFactory $resultJsonFactory, ResourceConnection $resourceConnection)
{
public function __construct(
Context $context,
ConfigHelper $configHelper,
JsonFactory $resultJsonFactory,
ResourceConnection $resourceConnection
) {
parent::__construct($context);

$this->configHelper = $configHelper;
Expand Down
4 changes: 2 additions & 2 deletions Controller/Adminhtml/Queue/Clear.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ public function execute()
try {
$this->db->query('TRUNCATE TABLE '.$this->tableName);

$status = array('status' => 'ok');
$status = ['status' => 'ok'];
} catch (\Exception $e) {
$status = array('status' => 'ko', 'message' => $e->getMessage());
$status = ['status' => 'ko', 'message' => $e->getMessage()];
}

/** @var \Magento\Framework\Controller\Result\Json $result */
Expand Down
4 changes: 2 additions & 2 deletions Controller/Adminhtml/Queue/Info.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ public function execute()
$eta = $hours . ' hours ' . $restMinutes . ' minutes';
}

$queueInfo = array(
$queueInfo = [
'isEnabled' => $this->configHelper->isQueueActive(),
'currentSize' => $size,
'eta' => $eta,
);
];

/** @var \Magento\Framework\Controller\Result\Json $result */
$result = $this->resultJsonFactory->create();
Expand Down
37 changes: 26 additions & 11 deletions Helper/AlgoliaHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,12 @@ class AlgoliaHelper extends AbstractHelper
/** @var string */
private static $lastTaskId;

public function __construct(Context $context, ConfigHelper $configHelper, ManagerInterface $messageManager, ConsoleOutput $consoleOutput)
{
public function __construct(
Context $context,
ConfigHelper $configHelper,
ManagerInterface $messageManager,
ConsoleOutput $consoleOutput
) {
parent::__construct($context);

$this->config = $configHelper;
Expand Down Expand Up @@ -205,7 +209,13 @@ public function setSynonyms($indexName, $synonyms)
$hitsPerPage = 100;
$page = 0;
do {
$complexSynonyms = $index->searchSynonyms('', ['altCorrection1', 'altCorrection2', 'placeholder'], $page, $hitsPerPage);
$complexSynonyms = $index->searchSynonyms(
'',
['altCorrection1', 'altCorrection2', 'placeholder'],
$page,
$hitsPerPage
);

foreach ($complexSynonyms['hits'] as $hit) {
unset($hit['_highlightResult']);

Expand All @@ -217,8 +227,7 @@ public function setSynonyms($indexName, $synonyms)

if (empty($synonyms)) {
$res = $index->clearSynonyms(true);
}
else {
} else {
$res = $index->batchSynonyms($synonyms, true, true);
}

Expand Down Expand Up @@ -299,7 +308,8 @@ private function checkClient($methodName)
$this->resetCredentialsFromConfig();

if (!isset($this->client)) {
throw new AlgoliaException('Operation "' . $methodName . ' could not be performed because Algolia credentials were not provided.');
$msg = 'Operation '.$methodName.' could not be performed because Algolia credentials were not provided.';
throw new AlgoliaException($msg);
}
}

Expand Down Expand Up @@ -336,7 +346,8 @@ private function prepareRecords(&$objects, $indexName)

if ($object === false) {
$longestAttribute = $this->getLongestAttribute($previousObject);
$modifiedIds[] = $indexName.' - ID '.$previousObject['objectID'].' - skipped - longest attribute: '.$longestAttribute;
$modifiedIds[] = $indexName.'
- ID '.$previousObject['objectID'].' - skipped - longest attribute: '.$longestAttribute;

unset($objects[$key]);
} elseif ($previousObject !== $object) {
Expand All @@ -349,7 +360,11 @@ private function prepareRecords(&$objects, $indexName)
if (!empty($modifiedIds)) {
$separator = php_sapi_name() === 'cli' ? "\n" : '<br>';

$errorMessage = 'Algolia reindexing: You have some records which are too big to be indexed in Algolia. They have either been truncated (removed attributes: '.implode(', ', $this->potentiallyLongAttributes).') or skipped completely: '.$separator.implode($separator, $modifiedIds);
$errorMessage = 'Algolia reindexing:
You have some records which are too big to be indexed in Algolia.
They have either been truncated
(removed attributes: '.implode(', ', $this->potentiallyLongAttributes).')
or skipped completely: '.$separator.implode($separator, $modifiedIds);

if (php_sapi_name() === 'cli') {
$this->consoleOutput->writeln($errorMessage);
Expand Down Expand Up @@ -401,7 +416,7 @@ private function getLongestAttribute($object)

public function castProductObject(&$productData)
{
$nonCastableAttributes = array('sku', 'name', 'description');
$nonCastableAttributes = ['sku', 'name', 'description'];

foreach ($productData as $key => &$data) {
if (in_array($key, $nonCastableAttributes, true) === true) {
Expand Down Expand Up @@ -436,8 +451,8 @@ private function castRecord($object)

private function castAttribute($value)
{
if (is_numeric($value) && floatval($value) == floatval(intval($value))) {
return intval($value);
if (is_numeric($value) && floatval($value) == floatval((int) $value)) {
return (int) $value;
}

if (is_numeric($value)) {
Expand Down
Loading

0 comments on commit 58899f7

Please sign in to comment.