Skip to content

Commit

Permalink
PAC-354: Add new Callback to validate store-view and website
Browse files Browse the repository at this point in the history
  • Loading branch information
kenza-ya committed Jul 15, 2024
1 parent 2dfc6b5 commit c4864a3
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 0 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
# Version 25.3.3

## Bugfixes

* None

## Features

* Add #PAC-354 new feature:
* Validation whether the product of the website is assigned to the current store view line
* `TechDivision\Import\Callbacks\StoreWebsiteValidatorCallback`

# Version 25.3.2

## Bugfixes
Expand Down
142 changes: 142 additions & 0 deletions src/Callbacks/StoreWebsiteValidatorCallback.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<?php

/**
* TechDivision\Import\Callbacks\ArrayValidatorCallback
*
* PHP version 7
*
* @author MET <met@techdivision.com>
* @copyright 2024 TechDivision GmbH <info@techdivision.com>
* @license https://opensource.org/licenses/MIT
* @link https://github.com/techdivision/import
* @link http://www.techdivision.com
*/

namespace TechDivision\Import\Callbacks;

use TechDivision\Import\Loaders\LoaderInterface;
use TechDivision\Import\Services\ImportProcessorInterface;
use TechDivision\Import\Utils\MemberNames;
use TechDivision\Import\Utils\RegistryKeys;

/**
* storeview validator callback implementation.
*
* @author MET <met@techdivision.com>
* @copyright 2024 TechDivision GmbH <info@techdivision.com>
* @license https://opensource.org/licenses/MIT
* @link https://github.com/techdivision/import
* @link http://www.techdivision.com
*/
class StoreWebsiteValidatorCallback extends ArrayValidatorCallback
{
/**
* The flag to query whether or not the value can be empty.
*
* @var boolean
*/
protected $nullable = false;

/**
* The flag to query whether or not the value has to be validated on the main row only.
*
* @var boolean
*/
protected $mainRowOnly = false;

/**
* The flag to query whether or not the value has to be ignored global strict mode configuration.
*
* @var boolean
*/
protected $ignoreStrictMode = false;

/**
* The store websites.
*
* @var array
*/
protected $storeWebsites = array();

/**
* Initializes the callback with the loader instance.
*
* @param LoaderInterface $storeLoader The loader instance to load the validations with
* @param ImportProcessorInterface $importProcessor The import processor instance
* @param boolean $nullable The flag to decide whether or not the value can be empty
* @param boolean $mainRowOnly The flag to decide whether or not the value has to be validated on the main row only
* @param boolean $ignoreStrictMode The flag to query whether or not the value has to be ignored global strict mode configuration.
*/
public function __construct(LoaderInterface $storeLoader, ImportProcessorInterface $importProcessor, $nullable = false, $mainRowOnly = false, $ignoreStrictMode = true)
{

// pass the loader to the parent instance
parent::__construct($storeLoader);

// initialize the flags with the passed values
$this->nullable = $nullable;
$this->mainRowOnly = $mainRowOnly;
$this->ignoreStrictMode = $ignoreStrictMode;

// initialize the array with the store websites
foreach ($importProcessor->getStoreWebsites() as $storeWebsite) {
$this->storeWebsites[$storeWebsite[MemberNames::CODE]] = $storeWebsite[MemberNames::WEBSITE_ID];
}
}

/**
* Will be invoked by a observer it has been registered for.
*
* @param string|null $attributeCode The code of the attribute that has to be validated
* @param string|null $attributeValue The attribute value to be validated
*
* @return mixed The modified value
*/
public function handle($attributeCode = null, $attributeValue = null)
{
// query whether or not the passed value IS empty and empty
// values are allowed
if ($this->isNullable($attributeValue)) {
return;
}

// the validations for the attribute with the given code
$validations = $this->getValidations($attributeCode);

$website = $this->load();
$productWebsite = $this->getSubject()->getValue('product_websites');

if ($validations[$attributeValue][MemberNames::WEBSITE_ID] !== $website[$productWebsite]) {
$message = sprintf(
'The store "%s" does not belong to the website "%s" . Please check your data.',
$attributeValue,
$productWebsite
);

$this->getSubject()
->getSystemLogger()
->warning($this->getSubject()->appendExceptionSuffix($message));
$this->getSubject()->mergeStatus(
array(
RegistryKeys::NO_STRICT_VALIDATIONS => array(
basename($this->getSubject()->getFilename()) => array(
$this->getSubject()->getLineNumber() => array(
$attributeCode => $message
)
)
)
)
);
}
}

/**
* Loads and returns data.
*
* @return \ArrayAccess The array with the data
*/
public function load()
{
return $this->storeWebsites;
}
}
7 changes: 7 additions & 0 deletions symfony/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,13 @@
<argument>%array_validator.nullable.false%</argument>
<argument>%array_validator.nullable.false%</argument>
</service>
<service id="import.callback.store.in.website.validator" class="TechDivision\Import\Callbacks\StoreWebsiteValidatorCallback">
<argument type="service" id="import.loader.store"/>
<argument type="service" id="import.processor.import"/>
<argument>%array_validator.nullable.true%</argument>
<argument>%array_validator.nullable.false%</argument>
<argument>%array_validator.nullable.false%</argument>
</service>

<!--
| The DI observer configuration
Expand Down

0 comments on commit c4864a3

Please sign in to comment.