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 10, 2024
1 parent db773ce commit fc6c84f
Show file tree
Hide file tree
Showing 3 changed files with 159 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 17.8.2

## 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 17.8.1

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

/**
* TechDivision\Import\Callbacks\ArrayValidatorCallback
*
* PHP version 7
*
* @author MET <[email protected]>
* @copyright 2024 TechDivision GmbH <[email protected]>
* @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;

/**
* storeview validator callback implementation.
*
* @author MET <[email protected]>
* @copyright 2024 TechDivision GmbH <[email protected]>
* @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 \TechDivision\Import\Loaders\LoaderInterface $storeLoader The loader instance to load the validations with
* @param ImportProcessorInterface $importProcessor The loader instance to load the validations with
* @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;

// load the store websites
$storeWebsites = $importProcessor->getStoreWebsites();

// initialize the array with the store websites
foreach ($storeWebsites 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)
{

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

// query whether or not the passed value IS empty and empty
// values are allowed
if ($this->isNullable($attributeValue)) {
return;
}

$website = $this->load();
$productWebsite = $this->getSubject()->getValue('product_websites');
$message = sprintf(
'The store "%s" does not belong to the website "%s" . Please check your data.',
$attributeValue,
$productWebsite
);

if ($validations[$attributeValue][MemberNames::WEBSITE_ID] === $website[$productWebsite]) {
return;
} else {
if ($this->hasHandleStrictMode($attributeCode, $message)) {
return;
}
}

// throw an exception if the store not in Website
throw new \InvalidArgumentException($message);
}

/**
* Loads and returns data.
*
* @return \ArrayAccess The array with the data
*/
public function load()
{
return $this->storeWebsites;
}
}
8 changes: 8 additions & 0 deletions symfony/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,14 @@
<argument>%array_validator.nullable.true%</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 configuration for the event emitter/listeners
!-->
Expand Down

0 comments on commit fc6c84f

Please sign in to comment.