Skip to content

Commit

Permalink
feat: Convert variables to camelCase + namespaces in Api classes
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmolinacano committed Oct 7, 2024
1 parent 86cd33d commit 5146d53
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 168 deletions.
87 changes: 43 additions & 44 deletions lib/DoofinderApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ class DoofinderApi
const DEFAULT_API_VERSION = '6';
const VERSION = '5.2.3';

private $api_key; // user API_KEY
private $apiKey; // user API_KEY
private $hashid; // hashid of the doofinder account
private $apiVersion;
private $url;
private $results;
private $query;
private $search_options = []; // assoc. array with doofinder options to be sent as request parameters
private $searchOptions = []; // assoc. array with doofinder options to be sent as request parameters
private $page = 1; // the page of the search results we're at
private $queryName; // the name of the last successfull query made
private $lastQuery; // the last successfull query made
private $queryName; // the name of the last successful query made
private $lastQuery; // the last successful query made
private $total; // total number of results obtained
private $maxScore;
private $paramsPrefix = self::DEFAULT_PARAMS_PREFIX;
Expand All @@ -69,10 +69,10 @@ class DoofinderApi
* -'restrictedRequest'(default: $_REQUEST): =>restrict request object
* to look for params when unserializing. either 'get' or 'post'
*/
public function __construct($hashid, $api_key, $fromParams = false, $init_options = [])
public function __construct($hashid, $apiKey, $fromParams = false, $init_options = [])
{
$zone_key_array = explode('-', $api_key);
$this->api_key = end($zone_key_array);
$zone_key_array = explode('-', $apiKey);
$this->apiKey = end($zone_key_array);
$this->zone = \Configuration::get('DF_REGION');
$this->url = UrlManager::getRegionalUrl(DoofinderConstants::DOOPHOENIX_REGION_URL, $this->zone);

Expand Down Expand Up @@ -150,7 +150,7 @@ private function reqHeaders()
$headers = [];
$headers[] = 'Expect:'; // Fixes the HTTP/1.1 417 Expectation Failed
$authHeaderName = $this->apiVersion == '4' ? 'API Token: ' : 'authorization: ';
$headers[] = $authHeaderName . $this->api_key; // API Authorization
$headers[] = $authHeaderName . $this->apiKey; // API Authorization

return $headers;
}
Expand Down Expand Up @@ -211,16 +211,16 @@ public function getOptions()
public function query($query = null, $page = null, $options = [])
{
if ($query) {
$this->search_options['query'] = $query;
$this->searchOptions['query'] = $query;
}
if ($page) {
$this->search_options['page'] = (int) $page;
$this->searchOptions['page'] = (int) $page;
}
foreach ($options as $optionName => $optionValue) {
$this->search_options[$optionName] = $options[$optionName];
$this->searchOptions[$optionName] = $options[$optionName];
}

$params = $this->search_options;
$params = $this->searchOptions;

// translate filters
if (!empty($params['filter'])) {
Expand All @@ -230,7 +230,7 @@ public function query($query = null, $page = null, $options = [])
}

// no query? then match all documents
if (!$this->optionExists('query') || !trim($this->search_options['query'])) {
if (!$this->optionExists('query') || !trim($this->searchOptions['query'])) {
$params['query_name'] = 'match_all';
}

Expand All @@ -245,7 +245,7 @@ public function query($query = null, $page = null, $options = [])
$dfResults = new DoofinderResults($this->apiCall('search', $params));
$this->page = $dfResults->getProperty('page');
$this->total = $dfResults->getProperty('total');
$this->search_options['query'] = $dfResults->getProperty('query');
$this->searchOptions['query'] = $dfResults->getProperty('query');
$this->maxScore = $dfResults->getProperty('max_score');
$this->queryName = $dfResults->getProperty('query_name');
$this->lastQuery = $dfResults->getProperty('query');
Expand Down Expand Up @@ -297,9 +297,9 @@ public function getPage()
public function setFilter($filterName, $filter)
{
if (!$this->optionExists('filter')) {
$this->search_options['filter'] = [];
$this->searchOptions['filter'] = [];
}
$this->search_options['filter'][$filterName] = $filter;
$this->searchOptions['filter'][$filterName] = $filter;
}

/**
Expand All @@ -315,7 +315,7 @@ public function setFilter($filterName, $filter)
*/
public function getFilter($filterName)
{
if ($this->optionExists('filter') && isset($this->search_options['filter'][$filterName])) {
if ($this->optionExists('filter') && isset($this->searchOptions['filter'][$filterName])) {
return $this->filter[$filterName];
}

Expand All @@ -331,8 +331,8 @@ public function getFilter($filterName)
*/
public function getFilters()
{
if (isset($this->search_options['filter'])) {
return $this->search_options['filter'];
if (isset($this->searchOptions['filter'])) {
return $this->searchOptions['filter'];
} else {
return false;
}
Expand All @@ -349,14 +349,14 @@ public function getFilters()
public function addTerm($filterName, $term)
{
if (!$this->optionExists('filter')) {
$this->search_options['filter'] = [$filterName => []];
$this->searchOptions['filter'] = [$filterName => []];
}
if (!isset($this->search_options['filter'][$filterName])) {
if (!isset($this->searchOptions['filter'][$filterName])) {
$this->filter[$filterName] = [];
$this->search_options['filter'][$filterName] = [];
$this->searchOptions['filter'][$filterName] = [];
}
$this->filter[$filterName][] = $term;
$this->search_options['filter'][$filterName][] = $term;
$this->searchOptions['filter'][$filterName][] = $term;
}

/**
Expand All @@ -369,8 +369,8 @@ public function addTerm($filterName, $term)
*/
public function removeTerm($filterName, $term)
{
if ($this->optionExists('filter') && isset($this->search_options['filter'][$filterName])
&& in_array($term, $this->search_options['filter'][$filterName])
if ($this->optionExists('filter') && isset($this->searchOptions['filter'][$filterName])
&& in_array($term, $this->searchOptions['filter'][$filterName])
) {
function filter_me($value)
{
Expand All @@ -379,8 +379,8 @@ function filter_me($value)
return $value != $term;
}

$this->search_options['filter'][$filterName] =
array_filter($this->search_options['filter'][$filterName], 'filter_me');
$this->searchOptions['filter'][$filterName] =
array_filter($this->searchOptions['filter'][$filterName], 'filter_me');
}
}

Expand All @@ -396,16 +396,16 @@ function filter_me($value)
public function setRange($filterName, $from = null, $to = null)
{
if (!$this->optionExists('filter')) {
$this->search_options['filter'] = [$filterName => []];
$this->searchOptions['filter'] = [$filterName => []];
}
if (!isset($this->search_options['filter'][$filterName])) {
$this->search_options['filter'][$filterName] = [];
if (!isset($this->searchOptions['filter'][$filterName])) {
$this->searchOptions['filter'][$filterName] = [];
}
if ($from) {
$this->search_options['filter'][$filterName]['from'] = $from;
$this->searchOptions['filter'][$filterName]['from'] = $from;
}
if ($to) {
$this->search_options['filter'][$filterName]['to'] = $from;
$this->searchOptions['filter'][$filterName]['to'] = $from;
}
}

Expand All @@ -418,7 +418,7 @@ public function setRange($filterName, $from = null, $to = null)
*/
public function toQuerystring($page = null)
{
foreach ($this->search_options as $paramName => $paramValue) {
foreach ($this->searchOptions as $paramName => $paramValue) {
if ($paramName == 'query') {
$toParams[$this->queryParameter] = $paramValue;
} else {
Expand Down Expand Up @@ -446,12 +446,11 @@ public function fromQuerystring()
$doofinderReqParams = array_filter(array_keys($this->serializationArray), [$this, 'belongsToDoofinder']);

foreach ($doofinderReqParams as $dfReqParam) {
if ($dfReqParam == $this->queryParameter) {
$keey = 'query';
} else {
$keey = substr($dfReqParam, strlen($this->paramsPrefix));
$key = 'query';
if ($dfReqParam !== $this->queryParameter) {
$key = substr($dfReqParam, strlen($this->paramsPrefix));
}
$this->search_options[$keey] = $this->serializationArray[$dfReqParam];
$this->searchOptions[$key] = $this->serializationArray[$dfReqParam];
}
}

Expand Down Expand Up @@ -499,15 +498,15 @@ private function belongsToDoofinder($paramName)
/**
* optionExists
*
* checks whether a search option is defined in $this->search_options
* checks whether a search option is defined in $this->searchOptions
*
* @param string $optionName
*
* @return bool
*/
private function optionExists($optionName)
{
return array_key_exists($optionName, $this->search_options);
return array_key_exists($optionName, $this->searchOptions);
}

/**
Expand Down Expand Up @@ -556,7 +555,7 @@ public function numPages()

public function getRpp()
{
$rpp = $this->optionExists('rpp') ? $this->search_options['rpp'] : null;
$rpp = $this->optionExists('rpp') ? $this->searchOptions['rpp'] : null;
$rpp = $rpp ? $rpp : self::DEFAULT_RPP;

return $rpp;
Expand Down Expand Up @@ -614,9 +613,9 @@ public function test($translationFunction, $onlyOneLang = false)
foreach (\Language::getLanguages(true, $context->shop->id) as $lang) {
if (!$onlyOneLang || ($onlyOneLang && $lang['iso_code'])) {
$lang_iso = \Tools::strtoupper($lang['iso_code']);
$hash_id = \Configuration::get('DF_HASHID_' . $currency . '_' . $lang_iso);
$api_key = \Configuration::get('DF_API_KEY');
if ($hash_id && $api_key) {
$hashid = \Configuration::get('DF_HASHID_' . $currency . '_' . $lang_iso);
$apiKey = \Configuration::get('DF_API_KEY');
if ($hashid && $apiKey) {
try {
$dfOptions = $this->getOptions();
if ($dfOptions) {
Expand Down
29 changes: 14 additions & 15 deletions lib/doofinder_api_index.php → lib/DoofinderApiIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,36 @@
* @copyright Doofinder
* @license GPLv3
*/
use PrestaShop\Module\Doofinder\Lib\EasyREST;

namespace PrestaShop\Module\Doofinder\Lib;

if (!defined('_PS_VERSION_')) {
exit;
}

const API_URL = 'https://{region}-plugins.doofinder.com';

class DoofinderApiIndex
{
private $api_key;
private $api_url;
private $apiKey;
private $apiUrl;

public function __construct($api_key, $region)
public function __construct($apiKey, $region)
{
$this->api_key = $api_key;
$this->api_url = str_replace('{region}', $region, API_URL);
$this->apiKey = $apiKey;
$this->apiUrl = UrlManager::getRegionalUrl(DoofinderConstants::DOOPLUGINS_REGION_URL, $region);
}

/**
* Make a request to the plugins API to reprocess all the feeds
*
* @param string $installation_id
* @param string $callback_url
* @param string $installationId
* @param string $callbackUrl
*/
public function invokeReindexing($installation_id, $callback_url = '')
public function invokeReindexing($installationId, $callbackUrl = '')
{
$api_endpoint = $this->api_url . '/process-feed';
$json_data = json_encode(['store_id' => $installation_id, 'callback_url' => $callback_url]);
$apiEndpoint = $this->apiUrl . '/process-feed';
$jsonData = json_encode(['store_id' => $installationId, 'callback_url' => $callbackUrl]);

return $this->post($api_endpoint, $json_data);
return $this->post($apiEndpoint, $jsonData);
}

private function post($url, $payload)
Expand All @@ -55,7 +54,7 @@ private function post($url, $payload)
false,
false,
'application/json',
['Authorization: Token ' . $this->api_key]
['Authorization: Token ' . $this->apiKey]
);

return json_decode($response->response, true);
Expand Down
21 changes: 10 additions & 11 deletions lib/doofinder_api_items.php → lib/DoofinderApiItems.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,25 @@
* @copyright Doofinder
* @license GPLv3
*/
use PrestaShop\Module\Doofinder\Lib\EasyREST;

namespace PrestaShop\Module\Doofinder\Lib;

if (!defined('_PS_VERSION_')) {
exit;
}

const API_URL = 'https://{region}-plugins.doofinder.com';

class DoofinderApiItems
{
private $hashid;
private $api_key;
private $api_url;
private $apiKey;
private $apiUrl;
private $type;

public function __construct($hashid, $api_key, $region, $type = 'product')
public function __construct($hashid, $apiKey, $region, $type = 'product')
{
$this->hashid = $hashid;
$this->api_key = $api_key;
$this->api_url = str_replace('{region}', $region, API_URL);
$this->apiKey = $apiKey;
$this->apiUrl = UrlManager::getRegionalUrl(DoofinderConstants::DOOPLUGINS_REGION_URL, $region);
$this->type = $type;
}

Expand All @@ -44,7 +43,7 @@ public function updateBulk($payload)
{
$endpoint = '/item/' . $this->hashid . '/' . $this->type . '?platform=prestashop&action=update';

$url = $this->api_url . $endpoint;
$url = $this->apiUrl . $endpoint;

return $this->post($url, $payload);
}
Expand All @@ -58,7 +57,7 @@ public function deleteBulk($payload)
{
$endpoint = '/item/' . $this->hashid . '/' . $this->type . '?platform=prestashop&action=delete';

$url = $this->api_url . $endpoint;
$url = $this->apiUrl . $endpoint;

return $this->post($url, $payload);
}
Expand All @@ -73,7 +72,7 @@ private function post($url, $payload)
false,
false,
'application/json',
['Authorization: Token ' . $this->api_key]
['Authorization: Token ' . $this->apiKey]
);

return json_decode($response->response, true);
Expand Down
Loading

0 comments on commit 5146d53

Please sign in to comment.