Skip to content

Commit

Permalink
[Bug]: Use parameters for joins in Customer Segment (#556)
Browse files Browse the repository at this point in the history
* [Bug]: Use parameters for joins in Customer Segment (#549)

* Use parameters for joins

* Use array_keys

* Set param type

* Use non deprecated version

* Apply php-cs-fixer changes

* artifact bump

* fix stan

* fix

* Apply php-cs-fixer changes

* Try different parameter type

* Apply php-cs-fixer changes

* Revert different array type

* try with different approach

* fix

* Update CustomerSegment.php

* fix

* Apply php-cs-fixer changes

---------

Co-authored-by: Matthias Schuhmayer <[email protected]>
Co-authored-by: kingjia90 <[email protected]>
Co-authored-by: mattamon <[email protected]>
Co-authored-by: mattamon <[email protected]>
  • Loading branch information
5 people authored Feb 10, 2025
1 parent f21abe0 commit 37b4c2f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/static-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ jobs:

- name: "Upload baseline file"
if: ${{ failure() }}
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v4
with:
name: phpstan-baseline.neon
path: phpstan-baseline.neon
19 changes: 13 additions & 6 deletions src/CustomerList/Filter/CustomerSegment.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use CustomerManagementFrameworkBundle\Listing\Filter\OnCreateQueryFilterInterface;
use CustomerManagementFrameworkBundle\Service\MariaDb;
use Doctrine\DBAL\Query\QueryBuilder;
use InvalidArgumentException;
use Pimcore\Model\DataObject;
use Pimcore\Model\DataObject\Listing as CoreListing;

Expand Down Expand Up @@ -70,7 +71,7 @@ public function __construct(array $segments, DataObject\CustomerSegmentGroup $se
{
$this->identifier = $this->buildIdentifier($segmentGroup);
$this->segmentGroup = $segmentGroup;
$this->type = $type;
$this->type = $type === self::OPERATOR_AND ? self::OPERATOR_AND : self::OPERATOR_OR;

foreach ($segments as $segment) {
$this->addCustomerSegment($segment);
Expand Down Expand Up @@ -123,7 +124,7 @@ protected function addCustomerSegment(DataObject\CustomerSegment $segment)
{
if ($segment->getGroup() && null !== $this->segmentGroup) {
if ($segment->getGroup()->getId() !== $this->segmentGroup->getId()) {
throw new \InvalidArgumentException('Segment does not belong to the defined segment group');
throw new InvalidArgumentException('Segment does not belong to the defined segment group');
}
}

Expand Down Expand Up @@ -221,20 +222,26 @@ protected function addJoin(
);

$condition = $baseCondition;

if ($this->type === self::OPERATOR_OR) {
// must match any of the passed IDs

$valueKeys = array_keys($conditionValue);
$isNumericArray = $valueKeys == array_filter($valueKeys, 'is_numeric');
if (!$isNumericArray) {
$valueKeys = [0];
}

$condition .= sprintf(
' AND %1$s.dest_id IN (%2$s)',
$joinName,
implode(',', $conditionValue)
implode(',', $valueKeys)
);
} else {
// runs an extra join for every ID - all joins must match
$condition .= sprintf(
' AND %1$s.dest_id = %2$s',
' AND %1$s.dest_id = %2$d',
$joinName,
$conditionValue
is_numeric($conditionValue) ? $conditionValue : 0
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,13 @@ private function extractNamingScheme(CustomerInterface $customer, $namingScheme)
foreach ($namingScheme as $i => $namingSchemeItem) {
preg_match_all('/{([a-zA-Z0-9]*)}/', $namingSchemeItem, $matchedPlaceholder);

if (sizeof($matchedPlaceholder)) {
foreach ($matchedPlaceholder[0] as $j => $placeholder) {
$field = $matchedPlaceholder[1][$j];

$getter = 'get'.ucfirst($field);
if (method_exists($customer, $getter)) {
$value = (string)$customer->$getter();
$namingScheme[$i] = str_replace($placeholder, $value, $namingScheme[$i]);
}
foreach ($matchedPlaceholder[0] as $j => $placeholder) {
$field = $matchedPlaceholder[1][$j];

$getter = 'get'.ucfirst($field);
if (method_exists($customer, $getter)) {
$value = (string)$customer->$getter();
$namingScheme[$i] = str_replace($placeholder, $value, $namingScheme[$i]);
}
}
$namingScheme[$i] = trim($namingScheme[$i]) ?: '--';
Expand Down

0 comments on commit 37b4c2f

Please sign in to comment.