Skip to content

Commit

Permalink
Merge 727d8bd into bc941ed
Browse files Browse the repository at this point in the history
  • Loading branch information
Tibor Kotosz committed May 29, 2017
2 parents bc941ed + 727d8bd commit 2cefcdd
Show file tree
Hide file tree
Showing 32 changed files with 804 additions and 123 deletions.
8 changes: 4 additions & 4 deletions features/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Fixtures\CategoryResolver;
use Fixtures\ProductResolver;
use Fixtures\StoreManager;
use Tkotosz\CatalogRouter\Model\Exception\CatalogEntityNotFoundException;
use Tkotosz\CatalogRouter\Model\Exception\EntityDataNotFoundException;
use Tkotosz\CatalogRouter\Model\Service\CatalogUrlPathResolver;
use Tkotosz\CatalogRouter\Model\UrlPath;
use Magento\Framework\DataObject as Category;
Expand Down Expand Up @@ -209,7 +209,7 @@ public function iShouldSeeTheCategoryPage(Category $category)
if ($resolvedCategory->getId() !== $category->getId()) {
throw new \Exception(sprintf("Expected category '%s' but got '%s'", $category->getId(), $resolvedCategory->getId()));
}
} catch (CatalogEntityNotFoundException $e) {
} catch (EntityDataNotFoundException $e) {
throw new \Exception('Url was not resolved to any category');
}
}
Expand All @@ -224,7 +224,7 @@ public function iShouldSeeTheProductPage(Product $product)
if ($resolvedProduct->getId() !== $product->getId()) {
throw new \Exception(sprintf("Expected product '%s' but got '%s'", $product->getId(), $resolvedProduct->getId()));
}
} catch (CatalogEntityNotFoundException $e) {
} catch (EntityDataNotFoundException $e) {
throw new \Exception('Url was not resolved to any product');
}
}
Expand All @@ -237,7 +237,7 @@ public function iShouldSeeThe404Page()
try {
$resolvedCategory = $this->urlPathResolver->resolve($this->currentUrlPath, $this->currentStore->getId());
throw new \Exception(sprintf("Url was not resolved to '%s' category", $resolvedCategory->getId()));
} catch (CatalogEntityNotFoundException $e) {
} catch (EntityDataNotFoundException $e) {
// everything ok, url was not resolved aka 404
}
}
Expand Down
30 changes: 21 additions & 9 deletions features/bootstrap/Fixtures/CategoryResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace Fixtures;

use Tkotosz\CatalogRouter\Api\CategoryResolverInterface;
use Tkotosz\CatalogRouter\Model\CatalogEntity;
use Tkotosz\CatalogRouter\Model\Exception\CatalogEntityNotFoundException;
use Tkotosz\CatalogRouter\Model\EntityData;
use Tkotosz\CatalogRouter\Model\Exception\EntityDataNotFoundException;

class CategoryResolver implements CategoryResolverInterface
{
Expand All @@ -26,30 +26,42 @@ public function __construct(array $categories)
* @param int $storeId
* @param int $parentId
*
* @return CatalogEntity
* @return EntityData
*/
public function resolveByUrlKey(string $urlKey, int $storeId, int $parentId) : CatalogEntity
public function resolveByUrlKey(string $urlKey, int $storeId, int $parentId) : EntityData
{
foreach ($this->categories as $category) {
$urlKeys = $category->getData('url_key');
$categoryUrlKeyInStore = isset($urlKeys[$storeId]) ? $urlKeys[$storeId] : $urlKeys[0];

if ($categoryUrlKeyInStore == $urlKey && $category->getData('parent_id') == $parentId) {
return new CatalogEntity('category', $category->getId(), $urlKey);
return new EntityData('category', $category->getId(), $urlKey);
}
}

throw new CatalogEntityNotFoundException('not found!');
throw new EntityDataNotFoundException('not found!');

}

/**
* @param string $urlKey
* @param int $storeId
* @param int $parentId
*
* @return EntityData[]
*/
public function resolveAllByUrlKey(string $urlKey, int $storeId, int $parentId) : array
{
throw new \Exception(__METHOD__ . 'Method not implemented');
}

/**
* @param int $categoryId
* @param int $storeId
*
* @return CatalogEntity
* @return EntityData
*/
public function resolveById(int $categoryId, int $storeId) : CatalogEntity
public function resolveById(int $categoryId, int $storeId) : EntityData
{
throw new \Exception(__METHOD__ . 'Method not implemented');
}
Expand All @@ -59,7 +71,7 @@ public function resolveById(int $categoryId, int $storeId) : CatalogEntity
*
* @return int[]
*/
public function resolveParentIds(int $categoryId) : array
public function resolveParentIds(int $categoryId, int $storeId) : array
{
throw new \Exception(__METHOD__ . 'Method not implemented');
}
Expand Down
29 changes: 20 additions & 9 deletions features/bootstrap/Fixtures/ProductResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace Fixtures;

use Tkotosz\CatalogRouter\Api\ProductResolverInterface;
use Tkotosz\CatalogRouter\Model\CatalogEntity;
use Tkotosz\CatalogRouter\Model\Exception\CatalogEntityNotFoundException;
use Tkotosz\CatalogRouter\Model\EntityData;
use Tkotosz\CatalogRouter\Model\Exception\EntityDataNotFoundException;
use Magento\Catalog\Model\Category;
use Magento\Store\Model\StoreManagerInterface;

Expand Down Expand Up @@ -33,30 +33,41 @@ public function __construct(StoreManagerInterface $storeManager, array $products
/**
* @param string $urlKey
*
* @return CatalogEntity
* @return EntityData
*/
public function resolveByUrlKey(string $urlKey, int $storeId) : CatalogEntity
public function resolveByUrlKey(string $urlKey, int $storeId) : EntityData
{
foreach ($this->products as $product) {
$urlKeys = $product->getData('url_key');
$productUrlKeyInStore = isset($urlKeys[$storeId]) ? $urlKeys[$storeId] : $urlKeys[0];

if ($productUrlKeyInStore == $urlKey) {
return new CatalogEntity('product', $product->getId(), $urlKey);
return new EntityData('product', $product->getId(), $urlKey);
}
}

throw new CatalogEntityNotFoundException('not found!');
throw new EntityDataNotFoundException('not found!');
}

/**
* @param string $urlKey
* @param int $storeId
*
* @return EntityData[]
*/
public function resolveAllByUrlKey(string $urlKey, int $storeId) : array
{
throw new \Exception(__METHOD__ . 'Method not implemented');
}

/**
* @param int $productId
*
* @return CatalogEntity
* @return EntityData
*/
public function resolveById(int $productId, int $storeId) : CatalogEntity
public function resolveById(int $productId, int $storeId) : EntityData
{
throw new CatalogEntityNotFoundException();
throw new \Exception(__METHOD__ . 'Method not implemented');
}

/**
Expand Down
16 changes: 15 additions & 1 deletion src/Tkotosz/CatalogRouter/Api/CacheInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,23 @@

interface CacheInterface
{
/**
* @param string $key
* @param mixed $value
*/
public function set(string $key, $value);

public function has(string $key);
/**
* @param string $key
*
* @return boolean
*/
public function has(string $key) : bool;

/**
* @param string $key
*
* @return mixed
*/
public function get(string $key);
}
24 changes: 24 additions & 0 deletions src/Tkotosz/CatalogRouter/Api/CatalogUrlPathProviderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Tkotosz\CatalogRouter\Api;

use Tkotosz\CatalogRouter\Model\UrlPath;

interface CatalogUrlPathProviderInterface
{
/**
* @param int $categoryId
* @param int $storeId
*
* @return UrlPath
*/
public function getCategoryUrlPath(int $categoryId, int $storeId) : UrlPath;

/**
* @param int $productId
* @param int $storeId
*
* @return UrlPath
*/
public function getProductUrlPath(int $productId, int $storeId) : UrlPath;
}
22 changes: 16 additions & 6 deletions src/Tkotosz/CatalogRouter/Api/CategoryResolverInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Tkotosz\CatalogRouter\Api;

use Tkotosz\CatalogRouter\Model\CatalogEntity;
use Tkotosz\CatalogRouter\Model\EntityData;

interface CategoryResolverInterface
{
Expand All @@ -11,22 +11,32 @@ interface CategoryResolverInterface
* @param int $storeId
* @param int $parentId
*
* @return CatalogEntity
* @return EntityData
*/
public function resolveByUrlKey(string $urlKey, int $storeId, int $parentId) : CatalogEntity;
public function resolveByUrlKey(string $urlKey, int $storeId, int $parentId) : EntityData;

/**
* @param string $urlKey
* @param int $storeId
* @param int $parentId
*
* @return EntityData[]
*/
public function resolveAllByUrlKey(string $urlKey, int $storeId, int $parentId) : array;

/**
* @param int $categoryId
* @param int $storeId
*
* @return CatalogEntity
* @return EntityData
*/
public function resolveById(int $categoryId, int $storeId) : CatalogEntity;
public function resolveById(int $categoryId, int $storeId) : EntityData;

/**
* @param int $categoryId
* @param int $storeId
*
* @return int[]
*/
public function resolveParentIds(int $categoryId) : array;
public function resolveParentIds(int $categoryId, int $storeId) : array;
}
18 changes: 13 additions & 5 deletions src/Tkotosz/CatalogRouter/Api/ProductResolverInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,31 @@

namespace Tkotosz\CatalogRouter\Api;

use Tkotosz\CatalogRouter\Model\CatalogEntity;
use Tkotosz\CatalogRouter\Model\EntityData;

interface ProductResolverInterface
{
/**
* @param string $urlKey
*
* @return CatalogEntity
* @return EntityData
*/
public function resolveByUrlKey(string $urlKey, int $storeId) : CatalogEntity;
public function resolveByUrlKey(string $urlKey, int $storeId) : EntityData;

/**
* @param string $urlKey
* @param int $storeId
*
* @return EntityData[]
*/
public function resolveAllByUrlKey(string $urlKey, int $storeId) : array;

/**
* @param int $productId
*
* @return CatalogEntity
* @return EntityData
*/
public function resolveById(int $productId, int $storeId) : CatalogEntity;
public function resolveById(int $productId, int $storeId) : EntityData;

/**
* @param int $productId
Expand Down
17 changes: 17 additions & 0 deletions src/Tkotosz/CatalogRouter/Api/UrlPathUsedChecker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Tkotosz\CatalogRouter\Api;

use Tkotosz\CatalogRouter\Model\UrlPath;
use Tkotosz\CatalogRouter\Model\EntityData;

interface UrlPathUsedChecker
{
/**
* @param UrlPath $urlPath
* @param int $storeId
*
* @return EntityData[]
*/
public function check(UrlPath $urlPath, int $storeId) : array;
}
4 changes: 2 additions & 2 deletions src/Tkotosz/CatalogRouter/Controller/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Tkotosz\CatalogRouter\Controller;

use Tkotosz\CatalogRouter\Model\Exception\CatalogEntityNotFoundException;
use Tkotosz\CatalogRouter\Model\Exception\EntityDataNotFoundException;
use Tkotosz\CatalogRouter\Model\Service\CatalogUrlPathResolver;
use Tkotosz\CatalogRouter\Model\UrlPath;
use Magento\Framework\App\ActionFactory;
Expand Down Expand Up @@ -70,7 +70,7 @@ public function match(RequestInterface $request)

$request->setAlias(Url::REWRITE_REQUEST_PATH_ALIAS, $urlPath->getIdentifier());
$result = $this->actionFactory->create(Forward::class);
} catch (CatalogEntityNotFoundException $e) {
} catch (EntityDataNotFoundException $e) {
$result = null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Tkotosz\CatalogRouter\Model;

class CatalogEntity
class EntityData
{
/**
* @var string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

use Exception;

class CatalogEntityNotFoundException extends Exception
class EntityDataNotFoundException extends Exception
{
}
16 changes: 15 additions & 1 deletion src/Tkotosz/CatalogRouter/Model/Service/Cache/InMemoryCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,30 @@ class InMemoryCache implements CacheInterface
*/
private $cache;

/**
* @param string $key
* @param mixed $value
*/
public function set(string $key, $value)
{
$this->cache[$key] = $value;
}

public function has(string $key)
/**
* @param string $key
*
* @return boolean
*/
public function has(string $key) : bool
{
return isset($this->cache[$key]);
}

/**
* @param string $key
*
* @return mixed
*/
public function get(string $key)
{
if (!isset($this->cache[$key])) {
Expand Down
Loading

0 comments on commit 2cefcdd

Please sign in to comment.