Skip to content

Commit

Permalink
Code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
srenon committed Apr 15, 2019
1 parent 4b3dfbf commit 6b18e1b
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 32 deletions.
35 changes: 24 additions & 11 deletions Model/Accessory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,16 @@

namespace MagePal\LinkProduct\Model;

class Accessory extends \Magento\Framework\DataObject
use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\ResourceModel\Product\Link\Collection;
use Magento\Framework\DataObject;
use MagePal\LinkProduct\Model\Product\Link;

/**
* Class Accessory
* @package MagePal\LinkProduct\Model
*/
class Accessory extends DataObject
{
/**
* Product link instance
Expand All @@ -15,8 +24,12 @@ class Accessory extends \Magento\Framework\DataObject
*/
protected $linkInstance;

/**
* Accessory constructor.
* @param Link $productLink
*/
public function __construct(
\MagePal\LinkProduct\Model\Product\Link $productLink
Link $productLink
) {
$this->linkInstance = $productLink;
}
Expand All @@ -34,10 +47,10 @@ public function getLinkInstance()
/**
* Retrieve array of Accessory products
*
* @param \Magento\Catalog\Model\Product $currentProduct
* @param Product $currentProduct
* @return array
*/
public function getAccessoryProducts(\Magento\Catalog\Model\Product $currentProduct)
public function getAccessoryProducts(Product $currentProduct)
{
if (!$this->hasAccessoryProducts()) {
$products = [];
Expand All @@ -53,10 +66,10 @@ public function getAccessoryProducts(\Magento\Catalog\Model\Product $currentProd
/**
* Retrieve accessory products identifiers
*
* @param \Magento\Catalog\Model\Product $currentProduct
* @param Product $currentProduct
* @return array
*/
public function getAccessoryProductIds(\Magento\Catalog\Model\Product $currentProduct)
public function getAccessoryProductIds(Product $currentProduct)
{
if (!$this->hasAccessoryProductIds()) {
$ids = [];
Expand All @@ -71,10 +84,10 @@ public function getAccessoryProductIds(\Magento\Catalog\Model\Product $currentPr
/**
* Retrieve collection accessory product
*
* @param \Magento\Catalog\Model\Product $currentProduct
* @param Product $currentProduct
* @return \Magento\Catalog\Model\ResourceModel\Product\Link\Product\Collection
*/
public function getAccessoryProductCollection(\Magento\Catalog\Model\Product $currentProduct)
public function getAccessoryProductCollection(Product $currentProduct)
{
$collection = $this->getLinkInstance()->useAccessoryLinks()->getProductCollection()->setIsStrongMode();
$collection->setProduct($currentProduct);
Expand All @@ -84,10 +97,10 @@ public function getAccessoryProductCollection(\Magento\Catalog\Model\Product $cu
/**
* Retrieve collection accessory link
*
* @param \Magento\Catalog\Model\Product $currentProduct
* @return \Magento\Catalog\Model\ResourceModel\Product\Link\Collection
* @param Product $currentProduct
* @return Collection
*/
public function getAccessoryLinkCollection(\Magento\Catalog\Model\Product $currentProduct)
public function getAccessoryLinkCollection(Product $currentProduct)
{
$collection = $this->getLinkInstance()->useAccessoryLinks()->getLinkCollection();
$collection->setProduct($currentProduct);
Expand Down
19 changes: 14 additions & 5 deletions Model/Product/CopyConstructor/Accessory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,24 @@

namespace MagePal\LinkProduct\Model\Product\CopyConstructor;

class Accessory implements \Magento\Catalog\Model\Product\CopyConstructorInterface
use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\Product\CopyConstructorInterface;
use Magento\Catalog\Model\Product\Link;

/**
* Class Accessory
* @package MagePal\LinkProduct\Model\Product\CopyConstructor
*/
class Accessory implements CopyConstructorInterface
{
/**
* Build product links
*
* @param \Magento\Catalog\Model\Product $product
* @param \Magento\Catalog\Model\Product $duplicate
* @param Product $product
* @param Product $duplicate
* @return void
*/
public function build(\Magento\Catalog\Model\Product $product, \Magento\Catalog\Model\Product $duplicate)
public function build(Product $product, Product $duplicate)
{
$data = [];
$attributes = [];
Expand All @@ -27,10 +35,11 @@ public function build(\Magento\Catalog\Model\Product $product, \Magento\Catalog\
$attributes[] = $attribute['code'];
}
}
/** @var \Magento\Catalog\Model\Product\Link $link */
/** @var Link $link */
foreach ($product->getAccessoryLinkCollection() as $link) {
$data[$link->getLinkedProductId()] = $link->toArray($attributes);
}

$duplicate->setAccessoryLinkData($data);
}
}
4 changes: 4 additions & 0 deletions Model/Product/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

namespace MagePal\LinkProduct\Model\Product;

/**
* Class Link
* @package MagePal\LinkProduct\Model\Product
*/
class Link extends \Magento\Catalog\Model\Product\Link
{
const LINK_TYPE_ACCESSORY = 7;
Expand Down
15 changes: 13 additions & 2 deletions Model/ProductLink/CollectionProvider/Accessory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,22 @@

namespace MagePal\LinkProduct\Model\ProductLink\CollectionProvider;

class Accessory implements \Magento\Catalog\Model\ProductLink\CollectionProviderInterface
use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\ProductLink\CollectionProviderInterface;

/**
* Class Accessory
* @package MagePal\LinkProduct\Model\ProductLink\CollectionProvider
*/
class Accessory implements CollectionProviderInterface
{
/** @var \MagePal\LinkProduct\Model\Accessory */
protected $accessoryModel;

/**
* Accessory constructor.
* @param \MagePal\LinkProduct\Model\Accessory $accessoryModel
*/
public function __construct(
\MagePal\LinkProduct\Model\Accessory $accessoryModel
) {
Expand All @@ -20,7 +31,7 @@ public function __construct(
/**
* {@inheritdoc}
*/
public function getLinkedProducts(\Magento\Catalog\Model\Product $product)
public function getLinkedProducts(Product $product)
{
return (array) $this->accessoryModel->getAccessoryProducts($product);
}
Expand Down
11 changes: 7 additions & 4 deletions Plugin/CatalogImportExport/Model/Import/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,25 @@

namespace MagePal\LinkProduct\Plugin\CatalogImportExport\Model\Import;

use Magento\CatalogImportExport\Model\Import\Produc;
use MagePal\LinkProduct\Model\Product\Link;

/**
* @see \Magento\CatalogImportExport\Model\Import\Product::getLinkNameToId
*/
class Product
{
/**
* REMARK: needs core patch https://github.com/magento/magento2/pull/21230/commits/0846e9aed7040659e7ce3e109eb91df3f5fdfb7e.patch
* REMARK: needs core patch
* https://github.com/magento/magento2/pull/21230/commits/0846e9aed7040659e7ce3e109eb91df3f5fdfb7e.patch
*
* @param \Magento\CatalogImportExport\Model\Import\Produc $subject
* @param \Magento\CatalogImportExport\Model\Import\Product $subject
* @param $result
*
* @return mixed
*/
public function afterGetLinkNameToId(\Magento\CatalogImportExport\Model\Import\Product $subject, $result)
{
$result['_accessory_'] = \MagePal\LinkProduct\Model\Product\Link::LINK_TYPE_ACCESSORY;
$result['_accessory_'] = Link::LINK_TYPE_ACCESSORY;
return $result;
}

Expand Down
12 changes: 9 additions & 3 deletions Setup/InstallSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@
use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use MagePal\LinkProduct\Model\Product\Link;
use MagePal\LinkProduct\Ui\DataProvider\Product\Form\Modifier\Accessory;

/**
* Class InstallSchema
* @package MagePal\LinkProduct\Setup
*/
class InstallSchema implements InstallSchemaInterface
{

Expand All @@ -25,8 +31,8 @@ public function install(SchemaSetupInterface $setup, ModuleContextInterface $con
* Install product link types in table (catalog_product_link_type)
*/
$catalogProductLinkTypeData = [
'link_type_id' => \MagePal\LinkProduct\Model\Product\Link::LINK_TYPE_ACCESSORY,
'code' => \MagePal\LinkProduct\Ui\DataProvider\Product\Form\Modifier\Accessory::DATA_SCOPE_ACCESSORY
'link_type_id' => Link::LINK_TYPE_ACCESSORY,
'code' => Accessory::DATA_SCOPE_ACCESSORY
];

$setup->getConnection()->insertOnDuplicate(
Expand All @@ -38,7 +44,7 @@ public function install(SchemaSetupInterface $setup, ModuleContextInterface $con
* install product link attributes position in table catalog_product_link_attribute
*/
$catalogProductLinkAttributeData = [
'link_type_id' => \MagePal\LinkProduct\Model\Product\Link::LINK_TYPE_ACCESSORY,
'link_type_id' => Link::LINK_TYPE_ACCESSORY,
'product_link_attribute_code' => 'position',
'data_type' => 'int',
];
Expand Down
15 changes: 10 additions & 5 deletions Ui/DataProvider/Product/Form/Modifier/Accessory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@

namespace MagePal\LinkProduct\Ui\DataProvider\Product\Form\Modifier;

use Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\Related;
use Magento\Ui\Component\Form\Fieldset;

class Accessory extends \Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\Related
/**
* Class Accessory
* @package MagePal\LinkProduct\Ui\DataProvider\Product\Form\Modifier
*/
class Accessory extends Related
{
const DATA_SCOPE_ACCESSORY = 'accessory';
/**
Expand Down Expand Up @@ -39,10 +44,10 @@ public function modifyMeta(array $meta)
'componentType' => Fieldset::NAME,
'dataScope' => static::DATA_SCOPE,
'sortOrder' => $this->getNextGroupSortOrder(
$meta,
self::$previousGroup,
self::$sortOrder
),
$meta,
self::$previousGroup,
self::$sortOrder
),
],
],
],
Expand Down
4 changes: 4 additions & 0 deletions Ui/DataProvider/Product/Related/AccessoryDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

use Magento\Catalog\Ui\DataProvider\Product\Related\AbstractDataProvider;

/**
* Class AccessoryDataProvider
* @package MagePal\LinkProduct\Ui\DataProvider\Product\Related
*/
class AccessoryDataProvider extends AbstractDataProvider
{
/**
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
],
"require": {
"php": "~5.6.0|7.0.2|7.0.4|~7.0.6|~7.1.0|~7.1.3|~7.2.0",
"magento/module-backend": "100.0.*|100.1.*|100.2.*|101.0.0",
"magento/module-backend": "100.0.*|100.1.*|100.2.*|101.0.*",
"magento/framework": "100.0.*|100.1.*|101.0.*|102.0.*"
},
"type": "magento2-module",
"version": "1.1.1",
"version": "1.1.2",
"autoload": {
"files": [
"registration.php"
Expand Down

0 comments on commit 6b18e1b

Please sign in to comment.