-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 3cebf0b
Showing
23 changed files
with
951 additions
and
0 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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2021 Ghost Unicorns snc di Ugolini Riccardo e Argentiero Danilo | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,45 @@ | ||
<?php | ||
/* | ||
* Copyright © Ghost Unicorns snc. All rights reserved. | ||
* See LICENSE for license details. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace GhostUnicorns\CmsBlockCustomerGroup\Model; | ||
|
||
use Magento\Framework\Exception\LocalizedException; | ||
|
||
class AddCacheIdentity | ||
{ | ||
/** | ||
* Customer group cache tag for CMS Block | ||
*/ | ||
const CACHE_TAG = 'customer_group_'; | ||
|
||
/** | ||
* @var GetCurrentCustomerGroup | ||
*/ | ||
private $getCurrentCustomerGroup; | ||
|
||
/** | ||
* @param GetCurrentCustomerGroup $getCurrentCustomerGroup | ||
*/ | ||
public function __construct( | ||
GetCurrentCustomerGroup $getCurrentCustomerGroup | ||
) { | ||
$this->getCurrentCustomerGroup = $getCurrentCustomerGroup; | ||
} | ||
|
||
/** | ||
* @param array $identities | ||
* @return array | ||
* @throws LocalizedException | ||
*/ | ||
public function execute(array $identities): array | ||
{ | ||
$currentCustomerGroup = $this->getCurrentCustomerGroup->execute(); | ||
$identities[] = self::CACHE_TAG . $currentCustomerGroup; | ||
return $identities; | ||
} | ||
} |
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,20 @@ | ||
<?php | ||
/* | ||
* Copyright © Ghost Unicorns snc. All rights reserved. | ||
* See LICENSE for license details. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace GhostUnicorns\CmsBlockCustomerGroup\Model\Api; | ||
|
||
class Data | ||
{ | ||
const TABLE_NAME = 'cms_block_customer_group'; | ||
|
||
/** | ||
* Fields name | ||
*/ | ||
const BLOCK_ID = 'block_id'; | ||
const CUSTOMER_GROUP_ID = 'customer_group_id'; | ||
} |
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,39 @@ | ||
<?php | ||
/* | ||
* Copyright © Ghost Unicorns snc. All rights reserved. | ||
* See LICENSE for license details. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace GhostUnicorns\CmsBlockCustomerGroup\Model; | ||
|
||
use Magento\Customer\Model\GroupManagement; | ||
use Magento\Customer\Model\Session; | ||
use Magento\Framework\Exception\LocalizedException; | ||
|
||
class GetCurrentCustomerGroup | ||
{ | ||
/** | ||
* @var Session | ||
*/ | ||
private $customerSession; | ||
|
||
/** | ||
* @param Session $customerSession | ||
*/ | ||
public function __construct( | ||
Session $customerSession | ||
) { | ||
$this->customerSession = $customerSession; | ||
} | ||
|
||
/** | ||
* @return int | ||
* @throws LocalizedException | ||
*/ | ||
public function execute(): int | ||
{ | ||
return (int)$this->customerSession->getCustomerGroupId(); | ||
} | ||
} |
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,51 @@ | ||
<?php | ||
/* | ||
* Copyright © Ghost Unicorns snc. All rights reserved. | ||
* See LICENSE for license details. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace GhostUnicorns\CmsBlockCustomerGroup\Model; | ||
|
||
use GhostUnicorns\CmsBlockCustomerGroup\Model\ResourceModel\GetCustomerGroupsByBlockId; | ||
use Magento\Framework\Exception\LocalizedException; | ||
|
||
class IsBlockAllowed | ||
{ | ||
/** | ||
* @var GetCustomerGroupsByBlockId | ||
*/ | ||
private $getCustomerGroupsByBlockId; | ||
|
||
/** | ||
* @var GetCurrentCustomerGroup | ||
*/ | ||
private $getCurrentCustomerGroup; | ||
|
||
/** | ||
* @param GetCurrentCustomerGroup $getCurrentCustomerGroup | ||
* @param GetCustomerGroupsByBlockId $getCustomerGroupsByBlockId | ||
*/ | ||
public function __construct( | ||
GetCurrentCustomerGroup $getCurrentCustomerGroup, | ||
GetCustomerGroupsByBlockId $getCustomerGroupsByBlockId | ||
) { | ||
$this->getCustomerGroupsByBlockId = $getCustomerGroupsByBlockId; | ||
$this->getCurrentCustomerGroup = $getCurrentCustomerGroup; | ||
} | ||
|
||
/** | ||
* @param int $blockId | ||
* @return bool | ||
* @throws LocalizedException | ||
*/ | ||
public function execute(int $blockId): bool | ||
{ | ||
$currentCustomerGroup = $this->getCurrentCustomerGroup->execute(); | ||
|
||
$allowedCustomerGroups = $this->getCustomerGroupsByBlockId->execute($blockId); | ||
|
||
return empty($allowedCustomerGroups) || in_array($currentCustomerGroup, $allowedCustomerGroups); | ||
} | ||
} |
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,48 @@ | ||
<?php | ||
/* | ||
* Copyright © Ghost Unicorns snc. All rights reserved. | ||
* See LICENSE for license details. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace GhostUnicorns\CmsBlockCustomerGroup\Model\ResourceModel; | ||
|
||
use GhostUnicorns\CmsBlockCustomerGroup\Model\Api\Data; | ||
use Magento\Framework\App\ResourceConnection; | ||
|
||
class AddCustomerGroupByBlockId | ||
{ | ||
/** | ||
* @var ResourceConnection | ||
*/ | ||
private $resourceConnection; | ||
|
||
/** | ||
* @param ResourceConnection $resourceConnection | ||
*/ | ||
public function __construct( | ||
ResourceConnection $resourceConnection | ||
) { | ||
$this->resourceConnection = $resourceConnection; | ||
} | ||
|
||
/** | ||
* @param int $blockId | ||
* @param int $customerGroup | ||
*/ | ||
public function execute(int $blockId, int $customerGroup) | ||
{ | ||
$connection = $this->resourceConnection->getConnection(); | ||
$tableName = $this->resourceConnection->getTablePrefix() . | ||
$this->resourceConnection->getTableName(Data::TABLE_NAME); | ||
|
||
$connection->insert( | ||
$tableName, | ||
[ | ||
Data::BLOCK_ID => $blockId, | ||
Data::CUSTOMER_GROUP_ID => $customerGroup, | ||
] | ||
); | ||
} | ||
} |
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,43 @@ | ||
<?php | ||
/* | ||
* Copyright © Ghost Unicorns snc. All rights reserved. | ||
* See LICENSE for license details. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace GhostUnicorns\CmsBlockCustomerGroup\Model\ResourceModel; | ||
|
||
use GhostUnicorns\CmsBlockCustomerGroup\Model\Api\Data; | ||
use Magento\Framework\App\ResourceConnection; | ||
|
||
class DeleteByBlockId | ||
{ | ||
/** | ||
* @var ResourceConnection | ||
*/ | ||
private $resourceConnection; | ||
|
||
/** | ||
* @param ResourceConnection $resourceConnection | ||
*/ | ||
public function __construct( | ||
ResourceConnection $resourceConnection | ||
) { | ||
$this->resourceConnection = $resourceConnection; | ||
} | ||
|
||
/** | ||
* @param int $blockId | ||
*/ | ||
public function execute(int $blockId) | ||
{ | ||
$connection = $this->resourceConnection->getConnection(); | ||
$tableName = $this->resourceConnection->getTablePrefix() . | ||
$this->resourceConnection->getTableName(Data::TABLE_NAME); | ||
|
||
$where = $connection->quoteInto(Data::BLOCK_ID . ' = ?', $blockId); | ||
|
||
$connection->delete($tableName, $where); | ||
} | ||
} |
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,34 @@ | ||
<?php | ||
/* | ||
* Copyright © Ghost Unicorns snc. All rights reserved. | ||
* See LICENSE for license details. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace GhostUnicorns\CmsBlockCustomerGroup\Model\ResourceModel; | ||
|
||
use Magento\Cms\Model\GetBlockByIdentifier; | ||
|
||
class GetBlockIdByBlockIdentifier implements GetBlockIdByBlockIdentifierInterface | ||
{ | ||
/** | ||
* @var GetBlockByIdentifier | ||
*/ | ||
private $getBlockByIdentifier; | ||
|
||
/** | ||
* @param GetBlockByIdentifier $getBlockByIdentifier | ||
*/ | ||
public function __construct( | ||
GetBlockByIdentifier $getBlockByIdentifier | ||
) { | ||
$this->getBlockByIdentifier = $getBlockByIdentifier; | ||
} | ||
|
||
public function execute(string $blockIdentifier, int $storeId = null): int | ||
{ | ||
$block = $this->getBlockByIdentifier->execute($blockIdentifier, $storeId); | ||
return (int)$block->getId(); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
Model/ResourceModel/GetBlockIdByBlockIdentifierInterface.php
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,22 @@ | ||
<?php | ||
/* | ||
* Copyright © Ghost Unicorns snc. All rights reserved. | ||
* See LICENSE for license details. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace GhostUnicorns\CmsBlockCustomerGroup\Model\ResourceModel; | ||
|
||
use Magento\Framework\Exception\NoSuchEntityException; | ||
|
||
interface GetBlockIdByBlockIdentifierInterface | ||
{ | ||
/** | ||
* @param string $blockIdentifier | ||
* @param int|null $storeId | ||
* @return int | ||
* @throws NoSuchEntityException | ||
*/ | ||
public function execute(string $blockIdentifier, int $storeId = null): int; | ||
} |
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,46 @@ | ||
<?php | ||
/* | ||
* Copyright © Ghost Unicorns snc. All rights reserved. | ||
* See LICENSE for license details. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace GhostUnicorns\CmsBlockCustomerGroup\Model\ResourceModel; | ||
|
||
use GhostUnicorns\CmsBlockCustomerGroup\Model\Api\Data; | ||
use Magento\Framework\App\ResourceConnection; | ||
|
||
class GetCustomerGroupsByBlockId | ||
{ | ||
/** | ||
* @var ResourceConnection | ||
*/ | ||
private $resourceConnection; | ||
|
||
/** | ||
* @param ResourceConnection $resourceConnection | ||
*/ | ||
public function __construct( | ||
ResourceConnection $resourceConnection | ||
) { | ||
$this->resourceConnection = $resourceConnection; | ||
} | ||
|
||
/** | ||
* @param int $blockId | ||
* @return array | ||
*/ | ||
public function execute(int $blockId): array | ||
{ | ||
$connection = $this->resourceConnection->getConnection(); | ||
$tableName = $this->resourceConnection->getTablePrefix() . | ||
$this->resourceConnection->getTableName(Data::TABLE_NAME); | ||
|
||
$query = $connection->select() | ||
->from($tableName, [Data::CUSTOMER_GROUP_ID]) | ||
->where(Data::BLOCK_ID . ' = ?', $blockId); | ||
|
||
return $connection->fetchCol($query); | ||
} | ||
} |
Oops, something went wrong.