|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace JustBetter\AkeneoBundle\Service; |
| 6 | + |
| 7 | +use Akeneo\Connector\Helper\Authenticator; |
| 8 | +use Akeneo\Connector\Helper\Config as ConfigHelper; |
| 9 | +use Akeneo\Connector\Model\Source\Filters\Update; |
| 10 | +use Magento\Framework\App\Config\ScopeConfigInterface; |
| 11 | +use Magento\Framework\App\ResourceConnection; |
| 12 | +use Magento\Framework\Stdlib\DateTime\TimezoneInterface; |
| 13 | + |
| 14 | +class DynamicFamilyFilter |
| 15 | +{ |
| 16 | + public const CONFIG_PATH_ENABLED = 'akeneo_connector/products_filters/dynamic_families_enabled'; |
| 17 | + |
| 18 | + public function __construct( |
| 19 | + protected ScopeConfigInterface $scopeConfig, |
| 20 | + protected Authenticator $authenticator, |
| 21 | + protected ConfigHelper $configHelper, |
| 22 | + protected ResourceConnection $resourceConnection, |
| 23 | + protected TimezoneInterface $timezone |
| 24 | + ) { |
| 25 | + } |
| 26 | + |
| 27 | + public function isEnabled(): bool |
| 28 | + { |
| 29 | + return $this->scopeConfig->isSetFlag(self::CONFIG_PATH_ENABLED); |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * @return array<string>|null |
| 34 | + */ |
| 35 | + public function getFamiliesWithUpdatedProducts(): ?array |
| 36 | + { |
| 37 | + if (!$this->isEnabled()) { |
| 38 | + return null; |
| 39 | + } |
| 40 | + |
| 41 | + $client = $this->authenticator->getAkeneoApiClient(); |
| 42 | + if ($client === null) { |
| 43 | + return null; |
| 44 | + } |
| 45 | + |
| 46 | + $filter = $this->buildUpdateFilter(); |
| 47 | + if (empty($filter)) { |
| 48 | + return null; |
| 49 | + } |
| 50 | + |
| 51 | + $families = []; |
| 52 | + $searchFilters = ['updated' => [$filter]]; |
| 53 | + $pageSize = $this->configHelper->getPaginationSize(); |
| 54 | + |
| 55 | + foreach ($client->getProductApi()->all($pageSize, ['search' => $searchFilters]) as $product) { |
| 56 | + if (!empty($product['family'])) { |
| 57 | + $families[] = $product['family']; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + foreach ($client->getProductModelApi()->all($pageSize, ['search' => $searchFilters]) as $model) { |
| 62 | + if (!empty($model['family'])) { |
| 63 | + $families[] = $model['family']; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + return array_values(array_unique($families)); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @return array<string, mixed> |
| 72 | + */ |
| 73 | + protected function buildUpdateFilter(): array |
| 74 | + { |
| 75 | + return match ($this->configHelper->getUpdatedMode()) { |
| 76 | + Update::GREATER_THAN => $this->greaterThan($this->configHelper->getUpdatedGreaterFilter()), |
| 77 | + Update::LOWER_THAN => $this->lowerThan($this->configHelper->getUpdatedLowerFilter()), |
| 78 | + Update::BETWEEN => $this->between( |
| 79 | + $this->configHelper->getUpdatedBetweenAfterFilter(), |
| 80 | + $this->configHelper->getUpdatedBetweenBeforeFilter() |
| 81 | + ), |
| 82 | + Update::SINCE_LAST_N_DAYS => $this->sinceLastNDays($this->configHelper->getUpdatedSinceFilter()), |
| 83 | + Update::SINCE_LAST_N_HOURS => $this->sinceLastNHours($this->configHelper->getUpdatedSinceLastHoursFilter()), |
| 84 | + Update::SINCE_LAST_IMPORT => $this->sinceLastImport(), |
| 85 | + default => [], |
| 86 | + }; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * @return array<string, mixed> |
| 91 | + */ |
| 92 | + protected function greaterThan(?string $date): array |
| 93 | + { |
| 94 | + return $date ? ['operator' => '>', 'value' => "$date 00:00:00"] : []; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * @return array<string, mixed> |
| 99 | + */ |
| 100 | + protected function lowerThan(?string $date): array |
| 101 | + { |
| 102 | + return $date ? ['operator' => '<', 'value' => "$date 23:59:59"] : []; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * @return array<string, mixed> |
| 107 | + */ |
| 108 | + protected function between(?string $after, ?string $before): array |
| 109 | + { |
| 110 | + return ($after && $before) |
| 111 | + ? ['operator' => 'BETWEEN', 'value' => ["$after 00:00:00", "$before 23:59:59"]] |
| 112 | + : []; |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * @return array<string, mixed> |
| 117 | + */ |
| 118 | + protected function sinceLastNDays(?string $days): array |
| 119 | + { |
| 120 | + if (!$days || !is_numeric($days)) { |
| 121 | + return []; |
| 122 | + } |
| 123 | + $date = $this->timezone->date()->modify("-$days days"); |
| 124 | + |
| 125 | + return ['operator' => '>', 'value' => $date->format('Y-m-d H:i:s')]; |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * @return array<string, mixed> |
| 130 | + */ |
| 131 | + protected function sinceLastNHours(?string $hours): array |
| 132 | + { |
| 133 | + if (!$hours || !is_numeric($hours)) { |
| 134 | + return []; |
| 135 | + } |
| 136 | + $date = $this->timezone->date()->modify("-$hours hours"); |
| 137 | + |
| 138 | + return ['operator' => '>', 'value' => $date->format('Y-m-d H:i:s')]; |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * @return array<string, mixed> |
| 143 | + */ |
| 144 | + protected function sinceLastImport(): array |
| 145 | + { |
| 146 | + $connection = $this->resourceConnection->getConnection(); |
| 147 | + $date = $connection->fetchOne( |
| 148 | + $connection->select() |
| 149 | + ->from($this->resourceConnection->getTableName('akeneo_connector_job'), ['last_success_date']) |
| 150 | + ->where('code = ?', 'product') |
| 151 | + ); |
| 152 | + |
| 153 | + return $date ? ['operator' => '>', 'value' => $date] : []; |
| 154 | + } |
| 155 | +} |
0 commit comments