-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'inviqa-add-rewrites' into develop
- Loading branch information
Showing
11 changed files
with
460 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
<?php | ||
|
||
namespace CtiDigital\Configurator\Model\Component; | ||
|
||
class Rewrite | ||
{ | ||
private $requestPath; | ||
private $targetPath; | ||
private $redirectType; | ||
private $storeId; | ||
private $description; | ||
|
||
/** | ||
* ExpectedRewrite constructor. | ||
* | ||
* @param string $requestPath | ||
* @param string $targetPath | ||
* @param string $redirectType | ||
* @param string $storeId | ||
* @param string $description | ||
*/ | ||
public function __construct($requestPath, $targetPath, $redirectType, $storeId, $description) | ||
{ | ||
$this->requestPath = $requestPath; | ||
$this->targetPath = $targetPath; | ||
$this->redirectType = $redirectType; | ||
$this->storeId = $storeId; | ||
$this->description = $description; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getRequestPath() | ||
{ | ||
return $this->requestPath; | ||
} | ||
|
||
/** | ||
* @param string $requestPath | ||
*/ | ||
public function setRequestPath($requestPath) | ||
{ | ||
$this->requestPath = $requestPath; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getTargetPath() | ||
{ | ||
return $this->targetPath; | ||
} | ||
|
||
/** | ||
* @param string $targetPath | ||
*/ | ||
public function setTargetPath($targetPath) | ||
{ | ||
$this->targetPath = $targetPath; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getRedirectType() | ||
{ | ||
return $this->redirectType; | ||
} | ||
|
||
/** | ||
* @param string $redirectType | ||
*/ | ||
public function setRedirectType($redirectType) | ||
{ | ||
$this->redirectType = $redirectType; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getStoreId() | ||
{ | ||
return $this->storeId; | ||
} | ||
|
||
/** | ||
* @param string $storeId | ||
*/ | ||
public function setStoreId($storeId) | ||
{ | ||
$this->storeId = $storeId; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getDescription() | ||
{ | ||
return $this->description; | ||
} | ||
|
||
/** | ||
* @param string $description | ||
*/ | ||
public function setDescription($description) | ||
{ | ||
$this->description = $description; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
<?php | ||
|
||
namespace CtiDigital\Configurator\Model\Component; | ||
|
||
use Magento\Framework\ObjectManagerInterface; | ||
use Magento\UrlRewrite\Model\UrlRewriteFactory; | ||
use Magento\UrlRewrite\Model\UrlPersistInterface; | ||
use CtiDigital\Configurator\Model\LoggingInterface; | ||
use CtiDigital\Configurator\Model\Exception\ComponentException; | ||
|
||
class Rewrites extends CsvComponentAbstract | ||
{ | ||
protected $alias = "rewrites"; | ||
protected $name = "rewrites"; | ||
protected $description = "Component to create URL Store Rewrites"; | ||
const THE_ROW_DATA_IS_NOT_VALID_MESSAGE = "The row data is not valid."; | ||
const URL_REWRITES_COMPLETE_MESSAGE = 'URL Rewrites Complete'; | ||
const URL_REWRITE_REQUIRES_A_REQUEST_PATH_TO_BE_SET_MESSAGE = 'URL Rewrite requires a request path to be set'; | ||
const REQUEST_PATH_CSV_KEY = 'requestPath'; | ||
const REQUEST_PATH_KEY = 'request_path'; | ||
const STORE_ID_CSV_KEY = 'storeId'; | ||
const TARGET_PATH_CSV_KEY = 'targetPath'; | ||
const REDIRECT_TYPE_CSV_KEY = 'redirectType'; | ||
const DESCRIPTION_CSV_KEY = 'description'; | ||
|
||
/** | ||
* @var UrlPersistInterface | ||
*/ | ||
protected $urlPersist; | ||
|
||
/** | ||
* @var UrlRewriteFactory | ||
*/ | ||
protected $urlRewriteFactory; | ||
|
||
/** | ||
* Rewrites constructor. | ||
* @param LoggingInterface $log | ||
* @param ObjectManagerInterface $objectManager | ||
* @param UrlPersistInterface $urlPersist | ||
* @param UrlRewriteFactory $urlRewriteFactory | ||
*/ | ||
public function __construct( | ||
LoggingInterface $log, | ||
ObjectManagerInterface $objectManager, | ||
UrlPersistInterface $urlPersist, | ||
UrlRewriteFactory $urlRewriteFactory | ||
) { | ||
parent::__construct($log, $objectManager); | ||
$this->urlPersist = $urlPersist; | ||
$this->urlRewriteFactory = $urlRewriteFactory; | ||
|
||
} | ||
|
||
/** | ||
* @param array|null $data | ||
*/ | ||
public function processData($data = null) | ||
{ | ||
$headerRowAttributes = $this->getAttributesFromHeaderRow($data); | ||
|
||
$this->removeHeaderRow($data); | ||
|
||
foreach ($data as $rewriteDataCsvRow) { | ||
$rewriteArray = []; | ||
|
||
$rewriteArray = $this->extractCsvDataIntoArray( | ||
$headerRowAttributes, | ||
$rewriteDataCsvRow, | ||
$rewriteArray | ||
); | ||
|
||
try { | ||
if (!isset($rewriteArray[self::REQUEST_PATH_CSV_KEY])) { | ||
$this->log->logError( | ||
self::URL_REWRITE_REQUIRES_A_REQUEST_PATH_TO_BE_SET_MESSAGE | ||
); | ||
continue; | ||
} | ||
|
||
$this->createOrUpdateRewriteRule($rewriteArray); | ||
|
||
} catch (ComponentException $e) { | ||
$this->log->logError($e->getMessage()); | ||
} | ||
} | ||
|
||
$this->log->logInfo( | ||
self::URL_REWRITES_COMPLETE_MESSAGE | ||
); | ||
} | ||
|
||
/** | ||
* Gets the first row of the CSV file as these should be the attribute keys | ||
* | ||
* @param null $data | ||
* @return array | ||
*/ | ||
public function getAttributesFromHeaderRow($data = null) | ||
{ | ||
$this->checkHeaderRowExists($data); | ||
$attributes = array(); | ||
foreach ($data[0] as $attributeCode) { | ||
$attributes[] = $attributeCode; | ||
} | ||
return $attributes; | ||
} | ||
|
||
/** | ||
* @param array $data | ||
* @return array | ||
*/ | ||
public function checkHeaderRowExists(array $data) | ||
{ | ||
if (!isset($data[0])) { | ||
throw new ComponentException( | ||
self::THE_ROW_DATA_IS_NOT_VALID_MESSAGE | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* @param array $data | ||
*/ | ||
private function removeHeaderRow(array &$data) | ||
{ | ||
unset($data[0]); | ||
} | ||
|
||
/** | ||
* Creates UrlRedirect from Array | ||
* | ||
* @param $rewriteArray | ||
*/ | ||
public function createOrUpdateRewriteRule(array $rewriteArray) | ||
{ | ||
$rewrite = $this->urlRewriteFactory->create(); | ||
$successMessage = 'URL Rewrite: "%s" created'; | ||
$rewriteCount = $rewrite->getCollection() | ||
->addFieldToFilter(self::REQUEST_PATH_KEY, $rewriteArray[self::REQUEST_PATH_CSV_KEY]) | ||
->getSize(); | ||
|
||
if ($rewriteCount > 0) { | ||
$rewrite = $rewrite->getCollection() | ||
->addFieldToFilter(self::REQUEST_PATH_KEY, $rewriteArray[self::REQUEST_PATH_CSV_KEY]) | ||
->getFirstItem(); | ||
|
||
$successMessage = 'URL Rewrite: "%s" already exists, rewrite updated'; | ||
} | ||
|
||
$rewrite->setIsAutogenerated(0) | ||
->setStoreId($rewriteArray[self::STORE_ID_CSV_KEY]) | ||
->setRequestPath($rewriteArray[self::REQUEST_PATH_CSV_KEY]) | ||
->setTargetPath($rewriteArray[self::TARGET_PATH_CSV_KEY]) | ||
->setRedirectType($rewriteArray[self::REDIRECT_TYPE_CSV_KEY]) //301 or 302 | ||
->setDescription($rewriteArray[self::DESCRIPTION_CSV_KEY]) | ||
->save(); | ||
|
||
$this->log->logInfo( | ||
sprintf($successMessage, $rewriteArray[self::DESCRIPTION_CSV_KEY]) | ||
); | ||
} | ||
|
||
/** | ||
* @param $attributeKeys | ||
* @param $rewriteDataCsvRow | ||
* @param $rewriteArray | ||
* @return mixed | ||
*/ | ||
public function extractCsvDataIntoArray($attributeKeys, $rewriteDataCsvRow, $rewriteArray) | ||
{ | ||
foreach ($attributeKeys as $column => $code) { | ||
$rewriteArray[$code] = $rewriteDataCsvRow[$column]; | ||
} | ||
return $rewriteArray; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
requestPath,targetPath,redirectType,storeId,description | ||
aaa,aab,302,1,Redirect One | ||
bbb,bbc,302,1,Redirect Two | ||
aac,aad,302,1,Redirect Three |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.