77use Akeneo \Connector \Helper \Authenticator ;
88use Akeneo \Connector \Helper \Config as ConfigHelper ;
99use Akeneo \Connector \Model \Source \Filters \Update ;
10+ use Akeneo \Pim \ApiClient \Search \SearchBuilder ;
11+ use Akeneo \Pim \ApiClient \Search \SearchBuilderFactory ;
1012use Magento \Framework \App \Config \ScopeConfigInterface ;
1113use Magento \Framework \App \ResourceConnection ;
1214use Magento \Framework \Stdlib \DateTime \TimezoneInterface ;
@@ -20,7 +22,8 @@ public function __construct(
2022 protected Authenticator $ authenticator ,
2123 protected ConfigHelper $ configHelper ,
2224 protected ResourceConnection $ resourceConnection ,
23- protected TimezoneInterface $ timezone
25+ protected TimezoneInterface $ timezone ,
26+ protected SearchBuilderFactory $ searchBuilderFactory
2427 ) {
2528 }
2629
@@ -43,22 +46,22 @@ public function getFamiliesWithUpdatedProducts(): ?array
4346 return null ;
4447 }
4548
46- $ filter = $ this ->buildUpdateFilter ();
47- if (empty ( $ filter ) ) {
49+ $ searchBuilder = $ this ->buildUpdateFilter ();
50+ if ($ searchBuilder === null ) {
4851 return null ;
4952 }
5053
5154 $ families = [];
52- $ searchFilters = ['updated ' => [ $ filter ] ];
55+ $ filters = ['search ' => $ searchBuilder -> getFilters () ];
5356 $ pageSize = $ this ->configHelper ->getPaginationSize ();
5457
55- foreach ($ client ->getProductApi ()->all ($ pageSize , [ ' search ' => $ searchFilters ] ) as $ product ) {
58+ foreach ($ client ->getProductApi ()->all ($ pageSize , $ filters ) as $ product ) {
5659 if (!empty ($ product ['family ' ])) {
5760 $ families [] = $ product ['family ' ];
5861 }
5962 }
6063
61- foreach ($ client ->getProductModelApi ()->all ($ pageSize , [ ' search ' => $ searchFilters ] ) as $ model ) {
64+ foreach ($ client ->getProductModelApi ()->all ($ pageSize , $ filters ) as $ model ) {
6265 if (!empty ($ model ['family ' ])) {
6366 $ families [] = $ model ['family ' ];
6467 }
@@ -67,81 +70,63 @@ public function getFamiliesWithUpdatedProducts(): ?array
6770 return array_values (array_unique ($ families ));
6871 }
6972
70- /**
71- * @return array<string, mixed>
72- */
73- protected function buildUpdateFilter (): array
73+ protected function buildUpdateFilter (): ?SearchBuilder
7474 {
75+ $ searchBuilder = $ this ->searchBuilderFactory ->create ();
76+
7577 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::GREATER_THAN => $ this ->greaterThan ($ searchBuilder , $ this ->configHelper ->getUpdatedGreaterFilter ()),
79+ Update::LOWER_THAN => $ this ->lowerThan ($ searchBuilder , $ this ->configHelper ->getUpdatedLowerFilter ()),
7880 Update::BETWEEN => $ this ->between (
81+ $ searchBuilder ,
7982 $ this ->configHelper ->getUpdatedBetweenAfterFilter (),
8083 $ this ->configHelper ->getUpdatedBetweenBeforeFilter ()
8184 ),
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 => [] ,
85+ Update::SINCE_LAST_N_DAYS => $ this ->sinceLastNDays ($ searchBuilder , $ this ->configHelper ->getUpdatedSinceFilter ()),
86+ Update::SINCE_LAST_N_HOURS => $ this ->sinceLastNHours ($ searchBuilder , $ this ->configHelper ->getUpdatedSinceLastHoursFilter ()),
87+ Update::SINCE_LAST_IMPORT => $ this ->sinceLastImport ($ searchBuilder ),
88+ default => null ,
8689 };
8790 }
8891
89- /**
90- * @return array<string, mixed>
91- */
92- protected function greaterThan (?string $ date ): array
92+ protected function greaterThan (SearchBuilder $ searchBuilder , ?string $ date ): ?SearchBuilder
9393 {
94- return $ date ? [ ' operator ' => '> ' , ' value ' => "$ date 00:00:00 " ] : [] ;
94+ return $ date ? $ searchBuilder -> addFilter ( ' updated ' , '> ' , "$ date 00:00:00 " ) : null ;
9595 }
9696
97- /**
98- * @return array<string, mixed>
99- */
100- protected function lowerThan (?string $ date ): array
97+ protected function lowerThan (SearchBuilder $ searchBuilder , ?string $ date ): ?SearchBuilder
10198 {
102- return $ date ? [ ' operator ' => '< ' , ' value ' => "$ date 23:59:59 " ] : [] ;
99+ return $ date ? $ searchBuilder -> addFilter ( ' updated ' , '< ' , "$ date 23:59:59 " ) : null ;
103100 }
104101
105- /**
106- * @return array<string, mixed>
107- */
108- protected function between (?string $ after , ?string $ before ): array
102+ protected function between (SearchBuilder $ searchBuilder , ?string $ after , ?string $ before ): ?SearchBuilder
109103 {
110104 return ($ after && $ before )
111- ? [ ' operator ' => 'BETWEEN ' , ' value ' => ["$ after 00:00:00 " , "$ before 23:59:59 " ]]
112- : [] ;
105+ ? $ searchBuilder -> addFilter ( ' updated ' , 'BETWEEN ' , ["$ after 00:00:00 " , "$ before 23:59:59 " ])
106+ : null ;
113107 }
114108
115- /**
116- * @return array<string, mixed>
117- */
118- protected function sinceLastNDays (?string $ days ): array
109+ protected function sinceLastNDays (SearchBuilder $ searchBuilder , ?string $ days ): ?SearchBuilder
119110 {
120111 if (!$ days || !is_numeric ($ days )) {
121- return [] ;
112+ return null ;
122113 }
123114 $ date = $ this ->timezone ->date ()->modify ("- $ days days " );
124115
125- return [ ' operator ' => '> ' , ' value ' => $ date ->format ('Y-m-d H:i:s ' )] ;
116+ return $ searchBuilder -> addFilter ( ' updated ' , '> ' , $ date ->format ('Y-m-d H:i:s ' )) ;
126117 }
127118
128- /**
129- * @return array<string, mixed>
130- */
131- protected function sinceLastNHours (?string $ hours ): array
119+ protected function sinceLastNHours (SearchBuilder $ searchBuilder , ?string $ hours ): ?SearchBuilder
132120 {
133121 if (!$ hours || !is_numeric ($ hours )) {
134- return [] ;
122+ return null ;
135123 }
136124 $ date = $ this ->timezone ->date ()->modify ("- $ hours hours " );
137125
138- return [ ' operator ' => '> ' , ' value ' => $ date ->format ('Y-m-d H:i:s ' )] ;
126+ return $ searchBuilder -> addFilter ( ' updated ' , '> ' , $ date ->format ('Y-m-d H:i:s ' )) ;
139127 }
140128
141- /**
142- * @return array<string, mixed>
143- */
144- protected function sinceLastImport (): array
129+ protected function sinceLastImport (SearchBuilder $ searchBuilder ): ?SearchBuilder
145130 {
146131 $ connection = $ this ->resourceConnection ->getConnection ();
147132 $ date = $ connection ->fetchOne (
@@ -150,6 +135,6 @@ protected function sinceLastImport(): array
150135 ->where ('code = ? ' , 'product ' )
151136 );
152137
153- return $ date ? [ ' operator ' => '> ' , ' value ' => $ date] : [] ;
138+ return $ date ? $ searchBuilder -> addFilter ( ' updated ' , '> ' , $ date) : null ;
154139 }
155140}
0 commit comments