Skip to content

Commit

Permalink
init: create repository
Browse files Browse the repository at this point in the history
  • Loading branch information
daniloargentiero committed Sep 8, 2021
0 parents commit 3cebf0b
Show file tree
Hide file tree
Showing 23 changed files with 951 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
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.
45 changes: 45 additions & 0 deletions Model/AddCacheIdentity.php
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;
}
}
20 changes: 20 additions & 0 deletions Model/Api/Data.php
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';
}
39 changes: 39 additions & 0 deletions Model/GetCurrentCustomerGroup.php
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();
}
}
51 changes: 51 additions & 0 deletions Model/IsBlockAllowed.php
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);
}
}
48 changes: 48 additions & 0 deletions Model/ResourceModel/AddCustomerGroupByBlockId.php
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,
]
);
}
}
43 changes: 43 additions & 0 deletions Model/ResourceModel/DeleteByBlockId.php
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);
}
}
34 changes: 34 additions & 0 deletions Model/ResourceModel/GetBlockIdByBlockIdentifier.php
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 Model/ResourceModel/GetBlockIdByBlockIdentifierInterface.php
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;
}
46 changes: 46 additions & 0 deletions Model/ResourceModel/GetCustomerGroupsByBlockId.php
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);
}
}
Loading

0 comments on commit 3cebf0b

Please sign in to comment.