Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Item and Menu managers and updated their models #26

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 46 additions & 6 deletions src/Api/Data/ItemInterface.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
<?php
/**
* @category ScandiPWA
* @package ScandiPWA_MenuOrganizer
* @author Dmitrijs Sitovs <info@scandiweb.com / dmitrijssh@scandiweb.com>
* @copyright Copyright (c) 2015 Scandiweb, Ltd (https://scandiweb.com)
*/

namespace ScandiPWA\MenuOrganizer\Api\Data;

/**
* @category ScandiPWA
* @package ScandiPWA\MenuOrganizer\Api\Data
* @author Dmitrijs Sitovs <info@scandiweb.com / dmitrijssh@scandiweb.com / dsitovs@gmail.com>
* @copyright Copyright (c) 2015 Scandiweb, Ltd (http://scandiweb.com)
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
*
* Interface ItemInterface
* @package ScandiPWA\MenuOrganizer
*/
interface ItemInterface
{
Expand All @@ -19,6 +20,8 @@ interface ItemInterface
const ITEM_ID = 'item_id';
const MENU_ID = 'menu_id';
const PARENT_ID = 'parent_id';
const TITLE = 'title';
const ITEM_CLASS = 'item_class';
const IDENTIFIER = 'identifier';
const URL = 'url';
const OPEN_TYPE = 'open_type';
Expand All @@ -27,6 +30,7 @@ interface ItemInterface
const CATEGORY_ID = 'category_id';
const POSITION = 'position';
const IS_ACTIVE = 'is_active';
const URL_ATTRIBUTES = 'url_attributes';
const ICON = 'icon';
const ICON_ALT = 'icon_alt';

Expand All @@ -45,6 +49,16 @@ public function getMenuId();
*/
public function getParentId();

/**
* @return mixed
*/
public function getTitle();

/**
* @return mixed
*/
public function getItemClass();

/**
* @return mixed
*/
Expand Down Expand Up @@ -85,6 +99,11 @@ public function getPosition();
*/
public function getIsActive();

/**
* @return mixed
*/
public function getUrlAttributes();

/**
* @return mixed
*/
Expand Down Expand Up @@ -116,6 +135,20 @@ public function setMenuId($menuId);
*/
public function setParentId($parentId);

/**
* @param $title
*
* @return mixed
*/
public function setTitle($title);

/**
* @param $itemClass
*
* @return mixed
*/
public function setItemClass($itemClass);

/**
* @param $identifier
*
Expand Down Expand Up @@ -172,6 +205,13 @@ public function setPosition($position);
*/
public function setIsActive($isActive);

/**
* @param $urlAttributes
*
* @return mixed
*/
public function setUrlAttributes($urlAttributes);

/**
* @param $icon
*
Expand Down
30 changes: 8 additions & 22 deletions src/Api/Data/MenuInterface.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
<?php
/**
* @category ScandiPWA
* @package ScandiPWA_MenuOrganizer
* @author Dmitrijs Sitovs <info@scandiweb.com / dmitrijssh@scandiweb.com>
* @copyright Copyright (c) 2015 Scandiweb, Ltd (https://scandiweb.com)
*/

namespace ScandiPWA\MenuOrganizer\Api\Data;

/**
* @category ScandiPWA
* @package ScandiPWA\MenuOrganizer\Api\Data
* @author Dmitrijs Sitovs <info@scandiweb.com / dmitrijssh@scandiweb.com / dsitovs@gmail.com>
* @copyright Copyright (c) 2015 Scandiweb, Ltd (http://scandiweb.com)
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
*
* Interface MenuInterface
* @package ScandiPWA\MenuOrganizer
*/
interface MenuInterface
{
Expand All @@ -18,7 +20,6 @@ interface MenuInterface
const MENU_ID = 'menu_id';
const IDENTIFIER = 'identifier';
const TITLE = 'title';
const TYPE = 'type';
const CSS_CLASS = 'css_class';
const IS_ACTIVE = 'is_active';

Expand All @@ -43,13 +44,6 @@ public function getIdentifier();
*/
public function getTitle();

/**
* Get menu type
*
* @return string|null
*/
public function getType();

/**
* Get menu css class
*
Expand Down Expand Up @@ -88,14 +82,6 @@ public function setIdentifier($identifier);
*/
public function setTitle($title);

/**
* Set menu type
*
* @param string $type
* @return \ScandiPWA\MenuOrganizer\Api\Data\MenuInterface
*/
public function setType($type);

/**
* Set menu css class
*
Expand Down
24 changes: 24 additions & 0 deletions src/Api/ItemManagerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* @category ScandiPWA
* @package ScandiPWA_MenuOrganizer
* @author Vladislavs Zimnikovs <info@scandiweb.com>
* @copyright Copyright (c) 2022 Scandiweb, Ltd (https://scandiweb.com)
*/

declare(strict_types=1);

namespace ScandiPWA\MenuOrganizer\Api;

/**
* Interface ItemManagerInterface
* @package ScandiPWA\MenuOrganizer
*/
interface ItemManagerInterface
{
/**
* @param int $itemId
* @return int
*/
public function deleteChildren(int $itemId): int;
}
24 changes: 24 additions & 0 deletions src/Api/MenuManagerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* @category ScandiPWA
* @package ScandiPWA_MenuOrganizer
* @author Vladislavs Zimnikovs <info@scandiweb.com>
* @copyright Copyright (c) 2022 Scandiweb, Ltd (https://scandiweb.com)
*/

declare(strict_types=1);

namespace ScandiPWA\MenuOrganizer\Api;

/**
* Interface ItemInterface
* @package ScandiPWA\MenuOrganizer
*/
interface MenuManagerInterface
{
/**
* @param int $menuId
* @return int
*/
public function deleteItems(int $menuId): int;
}
52 changes: 52 additions & 0 deletions src/Model/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,42 @@ public function setParentId($parentId)
return $this->setData(self::PARENT_ID, $parentId);
}

/**
* @return string
*/
public function getTitle()
{
return $this->getData(self::TITLE);
}

/**
* @param string $title
*
* @return $this
*/
public function setTitle($title)
{
return $this->setData(self::TITLE, $title);
}

/**
* @return string
*/
public function getItemClass()
{
return $this->getData(self::ITEM_CLASS);
}

/**
* @param string $itemClass
*
* @return $this
*/
public function setItemClass($itemClass)
{
return $this->setData(self::ITEM_CLASS, $itemClass);
}

/**
* @return string
*/
Expand Down Expand Up @@ -232,6 +268,22 @@ public function setIsActive($isActive)
return $this->setData(self::IS_ACTIVE, $isActive);
}

/**
* @return string
*/
public function getUrlAttributes()
{
return $this->getData(self::URL_ATTRIBUTES);
}

/**
* @param string $urlAttributes
*/
public function setUrlAttributes($urlAttributes)
{
return $this->setData(self::URL_ATTRIBUTES, $urlAttributes);
}

/**
* @return mixed
*/
Expand Down
93 changes: 93 additions & 0 deletions src/Model/ItemManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php
/**
* @category ScandiPWA
* @package ScandiPWA_MenuOrganizer
* @author Vladislavs Zimnikovs <info@scandiweb.com>
* @copyright Copyright (c) 2022 Scandiweb, Ltd (https://scandiweb.com)
*/

declare(strict_types=1);

namespace ScandiPWA\MenuOrganizer\Model;

use ScandiPWA\MenuOrganizer\Api\ItemManagerInterface;
use ScandiPWA\MenuOrganizer\Model\Item;
use ScandiPWA\MenuOrganizer\Model\ResourceModel\Item\Collection as ItemCollection;
use Magento\Framework\App\ResourceConnection;

/**
* Class ItemManager
* @package ScandiPWA\MenuOrganizer
*/
class ItemManager implements ItemManagerInterface
{
/**
* @var ItemCollection $itemCollection
*/
protected ItemCollection $itemCollection;

/**
* @var ResourceConnection $resourceConnection
*/
protected ResourceConnection $resourceConnection;

/**
* ItemManagement constructor.
* @param ItemCollection $itemCollection
* @param ResourceConnection $resourceConnection
*/
public function __construct(
ItemCollection $itemCollection,
ResourceConnection $resourceConnection
) {
$this->itemCollection = $itemCollection;
$this->resourceConnection = $resourceConnection;
}

/**
* @param string|int $itemId
* @return int
*/
public function deleteChildren(string|int $itemId): int
{
$connection = $this->resourceConnection->getConnection();
/**
* @var Item[] $menuItems
*/
$menuItems = $this->itemCollection
->getItems();
$itemIdsToDelete = [];
// Filling queue with children of passed item
$queue = array_filter($menuItems, static function (Item $item) use ($itemId) {
return (string)$item->getParentId() === (string)$itemId;
});

while (count($queue)) {
/**
* @var Item $nextItem
*/
$nextItem = array_shift($queue);
$itemIdsToDelete[] = $nextItem->getId();

// Getting children of current item
// Result maybe empty array if no children is present
$children = array_filter($menuItems, static function (Item $item) use ($nextItem) {
return (string)$item->getParentId() === (string)$nextItem->getId();
});

// Adding children to queue
$queue = [...$queue, ...$children];
}

// Performing one direct query to delete all children
// in one operation
$connection->delete(
$connection->getTableName('scandiweb_menumanager_item'),
[
'item_id IN (?)' => $itemIdsToDelete
]
);

return count($itemIdsToDelete);
}
}
Loading