Skip to content

Commit

Permalink
Merge branch 'inviqa-add-rewrites' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
chevli committed Jun 9, 2017
2 parents 97065a0 + 4634569 commit d771b29
Show file tree
Hide file tree
Showing 11 changed files with 460 additions and 4 deletions.
110 changes: 110 additions & 0 deletions Model/Component/Rewrite.php
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;
}
}
177 changes: 177 additions & 0 deletions Model/Component/Rewrites.php
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;
}
}
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,27 @@ php vendor/bin/phpcpd vendor/ctidigital/magento2-configurator/Model/ vendor/ctid
php vendor/bin/phpunit --coverage-clover build/logs/clover.xml vendor/ctidigital/magento2-configurator/Test/Unit/
```

## Integration tests
- Configure your [Magento integration test environment](http://devdocs.magento.com/guides/v2.0/test/integration/integration_test_setup.html).
- Add the XML below to dev/tests/integration/phpunit.xml.dist

````
<testsuite name="magento2-configurator">
<directory>../../../vendor/ctidigital/magento2-configurator/Test/Integration</directory>
</testsuite>
````

- You can run the tests from the correct place on the command line

````
/dev/tests/integration$ ../../../vendor/bin/phpunit --testsuite "magento2-configurator"
````

- You can also add the magento PHP developer tools to your path, so that you do not have to specify location of phpunit
````
export PATH=$PATH:/var/www/magento2/vendor/bin
````
## Unit tests
If you're developing a new component, please ensure you have your corresponding unit test which extends `ComponentAbstractTestCase` as that will test that your component has the required functions.
Do also include sample files with your component that works

Expand Down Expand Up @@ -64,7 +85,7 @@ Do also include sample files with your component that works
| Shipping Table Rates | :x: | :x: | :x: |
| Catalog Price Rules | :x: | :x: | :x: |
| Shopping Cart Price Rules | :x: | :x: | :x: |
| Rewrites | :x: | :x: | :x: |
| Rewrites | :white_check_mark: | :white_check_mark: | :white_check_mark: |
| Orders | :x: | :x: | :x: |

License
Expand Down
4 changes: 4 additions & 0 deletions Samples/Components/Rewrites/rewrites.csv
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
5 changes: 5 additions & 0 deletions Samples/master.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,8 @@ attribute_sets:
method: code
sources:
- ../configurator/Attributes/attribute_sets.yaml
rewrites:
enabled: 1
method: code
sources:
- ../configurator/Rewrites/rewrites.csv
Loading

0 comments on commit d771b29

Please sign in to comment.