server/src/app/Services/BnfResolver.php
changeset 158 366509ae2f37
parent 133 821253d361d1
child 506 8a5bb4b48b85
equal deleted inserted replaced
157:1f9853178fcd 158:366509ae2f37
    43      *                   Each id can be an url starting with http://data.bnf.fr/ or http://ark.bnf.fr/
    43      *                   Each id can be an url starting with http://data.bnf.fr/ or http://ark.bnf.fr/
    44      */
    44      */
    45     public function getLabels(array $ids) {
    45     public function getLabels(array $ids) {
    46 
    46 
    47         if(count($ids) > config('corpusparole.bnf_max_ids')) {
    47         if(count($ids) > config('corpusparole.bnf_max_ids')) {
    48             throw new BnfResolverException("Too manys ids provided");
    48 
       
    49             return array_reduce(
       
    50                 array_map([$this, 'getLabels'], array_chunk($ids, config('corpusparole.bnf_max_ids'))),
       
    51                 'array_merge',
       
    52                 []
       
    53             );
       
    54             //throw new BnfResolverException("Too manys ids provided");
    49         }
    55         }
    50 
    56 
    51         $bnfids = array_map([$this, 'checkBnfId'], $ids);
    57         $bnfids = array_map([$this, 'checkBnfId'], $ids);
    52         $bnfidsMap = array_combine($bnfids, $ids);
    58         $bnfidsMap = array_combine($bnfids, $ids);
    53 
    59 
    56 
    62 
    57         foreach ($bnfidsMap as $bnfid => $bnfidSource) {
    63         foreach ($bnfidsMap as $bnfid => $bnfidSource) {
    58             $cachedValue = Cache::get("bnf:$bnfid");
    64             $cachedValue = Cache::get("bnf:$bnfid");
    59             if(is_null($cachedValue)) {
    65             if(is_null($cachedValue)) {
    60                 array_push($missingBnfids, $bnfid);
    66                 array_push($missingBnfids, $bnfid);
       
    67             } elseif (mb_strlen($cachedValue)>0) {
       
    68                 $results[$bnfidSource] = $cachedValue;
    61             } else {
    69             } else {
    62                 $results[$bnfidSource] = $cachedValue;
    70                 $results[$bnfidSource] = null;
    63             }
    71             }
    64         }
    72         }
    65 
    73 
    66         if(count($missingBnfids) == 0) {
    74         if(count($missingBnfids) == 0) {
    67             return $results;
    75             return $results;
    97             if (mb_strlen($missingValue)>0) {
   105             if (mb_strlen($missingValue)>0) {
    98                 Cache::put("bnf:$bnfid", $missingValue, config('corpusparole.bnf_cache_expiration'));
   106                 Cache::put("bnf:$bnfid", $missingValue, config('corpusparole.bnf_cache_expiration'));
    99                 $results[$bnfidSource] = $missingValue;
   107                 $results[$bnfidSource] = $missingValue;
   100             }
   108             }
   101             else {
   109             else {
       
   110                 Cache::put("bnf:$bnfid", "", config('corpusparole.bnf_cache_expiration'));
   102                 $results[$bnfidSource] = null;
   111                 $results[$bnfidSource] = null;
   103             }
   112             }
   104         }
   113         }
   105 
       
   106         return $results;
   114         return $results;
   107     }
   115     }
   108 
   116 
   109 }
   117 }