Skip to content
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
46 changes: 45 additions & 1 deletion Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace SystemCode\BrazilCustomerAttributes\Helper;

use Magento\Store\Model\ScopeInterface;

/**
*
* Helper for validations and commons functions
Expand All @@ -17,12 +19,30 @@
*/
class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
const ACCOUNT_SHARE_SCOPE_FLAG_PATH = 'customer/account_share/scope';
const COPY_CNPJ_SOCIAL_NAME = 'brazilcustomerattributes/cnpj/copy_firstname';
const COPY_CNPJ_TRADE_NAME = 'brazilcustomerattributes/cnpj/copy_lastname';

public function getConfig($config_path)
{
return $this->scopeConfig->getValue(
$config_path,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
ScopeInterface::SCOPE_STORE
);
}

/**
* @param string $config_path
* @param null|mixed $storeId
*
* @return bool
*/
protected function isSetFlag(string $config_path, $storeId = null): bool
{
return $this->scopeConfig->isSetFlag(
$config_path,
ScopeInterface::SCOPE_STORE,
$storeId
);
}

Expand Down Expand Up @@ -149,4 +169,28 @@ public function getRegionId($state){
}

}

/**
* @return bool
*/
public function isAccountSharedByWebsite(): bool
{
return $this->isSetFlag(self::ACCOUNT_SHARE_SCOPE_FLAG_PATH);
}

/**
* @return bool
*/
public function copySocialName(): bool
{
return $this->isSetFlag(self::COPY_CNPJ_SOCIAL_NAME);
}

/**
* @return bool
*/
public function copyTradeName(): bool
{
return $this->isSetFlag(self::COPY_CNPJ_TRADE_NAME);
}
}
48 changes: 48 additions & 0 deletions Model/Customer/GetCustomer/Builders/SearchCriteria.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace SystemCode\BrazilCustomerAttributes\Model\Customer\GetCustomer\Builders;

use Magento\Framework\Api\SearchCriteriaBuilder;

/**
* Class SearchCriteria
*/
class SearchCriteria
{
/** @var SearchCriteriaBuilder */
protected $searchCriteriaBuilder;

/**
* SearchCriteria constructor.
*
* @param SearchCriteriaBuilder $searchCriteriaBuilder
*/
public function __construct(SearchCriteriaBuilder $searchCriteriaBuilder) {
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
}

/**
* @param mixed $customer
* @param string $fieldName
* @param bool $websiteScope
*
* @return \Magento\Framework\Api\SearchCriteria
*/
public function build($customer, string $fieldName, bool $websiteScope): \Magento\Framework\Api\SearchCriteria
{
$this->searchCriteriaBuilder->addFilter($fieldName, $customer->getData($fieldName));

if ($websiteScope){
$this->searchCriteriaBuilder->addFilter('website_id', $customer->getWebsiteId());
}

$customerId = $customer->getId();
if ($customerId) {
$this->searchCriteriaBuilder->addFilter('entity_id', $customerId, 'neq');
}

return $this->searchCriteriaBuilder->create();
}
}
58 changes: 58 additions & 0 deletions Model/Customer/GetCustomer/Command.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace SystemCode\BrazilCustomerAttributes\Model\Customer\GetCustomer;

use Magento\Customer\Api\Data\CustomerSearchResultsInterface;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Framework\Exception\LocalizedException;
use SystemCode\BrazilCustomerAttributes\Helper\Data;
use SystemCode\BrazilCustomerAttributes\Model\Customer\GetCustomer\Builders\SearchCriteria;

/**
* Class Command
*/
class Command
{
/** @var Data */
protected $helper;

/** @var SearchCriteria */
protected $searchCriteriaBuilder;

/** @var CustomerRepositoryInterface */
protected $customerRepository;

/**
* Command constructor.
*
* @param Data $helper
* @param SearchCriteria $searchCriteriaBuilder
* @param CustomerRepositoryInterface $customerRepository
*/
public function __construct(
Data $helper,
SearchCriteria $searchCriteriaBuilder,
CustomerRepositoryInterface $customerRepository
) {
$this->helper = $helper;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->customerRepository = $customerRepository;
}

/**
* @param mixed $customer
* @param string $fieldName
*
* @return CustomerSearchResultsInterface
* @throws LocalizedException
*/
public function execute($customer, string $fieldName): CustomerSearchResultsInterface
{
$websiteScope = $this->helper->isAccountSharedByWebsite();
$searchCriteria = $this->searchCriteriaBuilder->build($customer, $fieldName, $websiteScope);

return $this->customerRepository->getList($searchCriteria);
}
}
Loading