99use ItkDev \F2ApiClient \Model \Atom ;
1010use ItkDev \F2ApiClient \Model \CaseFile ;
1111use ItkDev \F2ApiClient \Model \Matter ;
12+ use Psr \Cache \CacheItemInterface ;
13+ use Psr \Cache \CacheItemPoolInterface ;
14+ use Symfony \Component \Cache \Adapter \ProxyAdapter ;
1215use Symfony \Component \HttpClient \HttpClient ;
1316use Symfony \Component \HttpFoundation \Request ;
1417use Symfony \Component \HttpFoundation \Response ;
@@ -40,45 +43,42 @@ public function __construct(array $options)
4043 */
4144 public function getServiceIndex (): array
4245 {
43- $ path = '/F2Rest/ServiceIndex ' ;
44- $ response = $ this ->client ()->request (Request::METHOD_GET , $ path , [
45- 'headers ' => [
46- 'Accept ' => 'application/json ' ,
47- ],
48- ]);
46+ $ cache = new ProxyAdapter (pool: $ this ->options ['cache_item_pool ' ]);
47+ $ cacheKey = sha1 (__METHOD__ );
48+
49+ $ result = $ cache ->get ($ cacheKey , function (CacheItemInterface $ item ): array {
50+ $ item ->expiresAfter ($ this ->options ['cache_item_lifetime ' ]);
51+
52+ $ path = '/F2Rest/ServiceIndex ' ;
53+ $ response = $ this ->client ()->request (Request::METHOD_GET , $ path , [
54+ 'headers ' => [
55+ 'Accept ' => 'application/json ' ,
56+ ],
57+ ]);
4958
50- return $ response ->toArray ();
59+ return $ response ->toArray ();
60+ });
61+
62+ return $ result ;
5163 }
5264
5365 /**
54- * @param array{q: string, count: int} $query
5566 * @return Atom[]
5667 *
5768 * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
5869 * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
5970 * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
6071 * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
6172 */
62- public function caseSearch (string $ q , int $ count = 10 ): array
73+ public function caseSearch (string $ searchTerms , int $ count = 10 ): array
6374 {
6475 $ query = [
65- 'q ' => $ q ,
76+ 'searchTerms ' => $ searchTerms ,
6677 'count ' => $ count ,
6778 ];
68- $ path = '/F2Rest/search/cases ' ;
69- // @todo /F2Rest/ServiceIndex.json reports
70- //
71- // "http://cbrain.com/casefile/rel/case-search": {
72- // "href": "/F2Rest/search/cases",
73- // "title": "Case file search"
74- // },
75- //
76- // which refers to the actual search URL: /F2Rest/searches/cases
77- $ path = '/F2Rest/searches/cases ' ;
78- // $path = $this->getRequestUrl('http://cbrain.com/casefile/rel/case-search');
79- $ response = $ this ->request (Request::METHOD_GET , $ path , [
80- 'query ' => $ query ,
81- ]);
79+
80+ $ url = $ this ->getRequestUrl ('http://cbrain.com/casefile/rel/case-search ' , $ query , isSearch: true );
81+ $ response = $ this ->request (Request::METHOD_GET , $ url );
8282
8383 $ items = [];
8484 $ sxe = new \SimpleXMLElement ($ response ->getContent ());
@@ -106,25 +106,12 @@ public function caseById(string $id): CaseFile
106106 public function matterSearch (string $ searchTerms , int $ count = 10 ): array
107107 {
108108 $ query = [
109- 'searchTerms ' => $ searchTerms ,
110- 'count ' => $ count ,
109+ 'searchTerms ' => $ searchTerms ,
110+ 'count ' => $ count ,
111111 ];
112- // @todo /F2Rest/ServiceIndex.json reports
113- //
114- // "http://cbrain.com/casefile/rel/case-search": {
115- // "href": "/F2Rest/search/cases",
116- // "title": "Case file search"
117- // },
118- //
119- // which refers to the actual search URL: /F2Rest/searches/cases
120- $ url = '/F2Rest/searches/matters ' ;
121- $ url = '/F2Rest/searches/matters?q={searchTerms}&count={count} ' ;
122- $ url = $ this ->replacePlaceholders ($ url , $ query );
123- // $url = $this->getRequestUrl('http://cbrain.com/casefile/rel/matter-search', [
124- // ]);
125- $ response = $ this ->request (Request::METHOD_GET , $ url , [
126- 'query ' => $ query ,
127- ]);
112+
113+ $ url = $ this ->getRequestUrl ('http://cbrain.com/casefile/rel/matter-search ' , $ query , isSearch: true );
114+ $ response = $ this ->request (Request::METHOD_GET , $ url );
128115
129116 $ items = [];
130117 $ sxe = new \SimpleXMLElement ($ response ->getContent ());
@@ -163,7 +150,6 @@ public function matterByMatterNumber(string $matterNumber): Matter
163150 return Matter::fromSimpleXMLElement (new \SimpleXMLElement ($ response ->getContent ()));
164151 }
165152
166-
167153 /**
168154 * @return array{access_token: string, token_type: string}
169155 */
@@ -221,24 +207,51 @@ protected function client(): HttpClientInterface
221207
222208 protected function configureOptions (OptionsResolver $ resolver ): void
223209 {
224- $ resolver ->setRequired ([
225- 'api_uri ' ,
226- 'api_username ' ,
227- 'api_secret ' ,
228- 'f2_username ' ,
229- ]);
210+ $ resolver
211+ ->setRequired ([
212+ 'api_uri ' ,
213+ 'api_username ' ,
214+ 'api_secret ' ,
215+ 'f2_username ' ,
216+ ])
217+ ->setDefault ('cache_item_lifetime ' , 86400 )
218+ ->setAllowedTypes ('cache_item_lifetime ' , 'int ' )
219+ ->setRequired ('cache_item_pool ' )
220+ ->setAllowedTypes ('cache_item_pool ' , CacheItemPoolInterface::class);
230221 }
231222
232- protected function getRequestUrl (string $ rel , array $ values ): string
223+ protected function getRequestUrl (string $ rel , array $ values, bool $ isSearch = false ): string
233224 {
234- // @todo Cache this!
235225 $ index = $ this ->getServiceIndex ();
236226
237227 $ url = $ index [$ rel ]['href ' ] ?? null ;
238228 if (null === $ url ) {
239229 throw new RuntimeException (sprintf ('Cannot get rel %s ' , $ rel ));
240230 }
241231
232+ if ($ isSearch ) {
233+ try {
234+ $ cache = new ProxyAdapter (pool: $ this ->options ['cache_item_pool ' ]);
235+ $ cacheKey = sha1 (__METHOD__ . '||| ' . $ rel );
236+
237+ $ url = $ cache ->get ($ cacheKey , function (CacheItemInterface $ item ) use ($ url ) {
238+ $ item ->expiresAfter ($ this ->options ['cache_item_lifetime ' ]);
239+
240+ $ response = $ this ->request (Request::METHOD_GET , $ url );
241+ $ sxe = new \SimpleXMLElement ($ response ->getContent ());
242+
243+ $ searchUrl = (string ) $ sxe ->Url ['template ' ];
244+ if (empty ($ searchUrl )) {
245+ throw new RuntimeException (sprintf ('Cannot get search template URL for %s ' , $ url ));
246+ }
247+
248+ return $ searchUrl ;
249+ });
250+ } catch (\Exception $ e ) {
251+ throw new RuntimeException (sprintf ('Cannot get search URL for rel %s ' , $ rel ), previous: $ e );
252+ }
253+ }
254+
242255 return $ this ->replacePlaceholders ($ url , $ values );
243256 }
244257
@@ -253,7 +266,7 @@ static function (array $matches) use ($url, $values): string {
253266 throw new RuntimeException (sprintf ('Missing value %s for URL %s ' , $ name , $ url ));
254267 }
255268
256- return rawurlencode ((string )$ values [$ name ]);
269+ return rawurlencode ((string ) $ values [$ name ]);
257270 },
258271 $ url ,
259272 );
0 commit comments