CustomerCollection.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /**
  3. * Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License").
  6. * You may not use this file except in compliance with the License.
  7. * A copy of the License is located at
  8. *
  9. * http://aws.amazon.com/apache2.0
  10. *
  11. * or in the "license" file accompanying this file. This file is distributed
  12. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  13. * express or implied. See the License for the specific language governing
  14. * permissions and limitations under the License.
  15. */
  16. namespace Amazon\Login\Plugin;
  17. use Closure;
  18. use Amazon\Core\Helper\Data as AmazonHelper;
  19. use Magento\Customer\Model\ResourceModel\Customer\Collection;
  20. use Magento\Eav\Model\Entity\Attribute\AttributeInterface;
  21. use Magento\Framework\DB\Select;
  22. class CustomerCollection
  23. {
  24. /**
  25. * @var AmazonHelper
  26. */
  27. private $amazonHelper;
  28. /**
  29. * @param AmazonHelper $amazonHelper
  30. */
  31. public function __construct(
  32. AmazonHelper $amazonHelper
  33. ) {
  34. $this->amazonHelper = $amazonHelper;
  35. }
  36. /**
  37. * Resolve issue with core magento not allowing extension attributes to be applied as filter
  38. *
  39. * @param Collection $collection
  40. * @param Closure $proceed
  41. * @param AttributeInterface|integer|string|array $attribute
  42. * @param array|string|null $condition
  43. * @param string $joinType
  44. *
  45. * @return Collection
  46. */
  47. public function aroundAddAttributeToFilter(
  48. Collection $collection,
  49. Closure $proceed,
  50. $attribute,
  51. $condition = null,
  52. $joinType = 'inner'
  53. ) {
  54. if ($this->amazonHelper->isLwaEnabled() && is_array($attribute)) {
  55. $attribute = $this->addAmazonIdFilter($attribute, $collection);
  56. if (0 === count($attribute)) {
  57. return $collection;
  58. }
  59. }
  60. return $proceed($attribute, $condition, $joinType);
  61. }
  62. /**
  63. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  64. */
  65. private function addAmazonIdFilter(array $attribute, Collection $collection)
  66. {
  67. foreach ($attribute as $key => $condition) {
  68. if ('amazon_id' == $condition['attribute']) {
  69. $collection->getSelect()->where('extension_attribute_amazon_id.amazon_id = ?', $condition['eq']);
  70. unset($attribute[$key]);
  71. }
  72. }
  73. return $attribute;
  74. }
  75. }