| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * AnimeDb package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @author    Peter Gribanov <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @copyright Copyright (c) 2011, Peter Gribanov | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @license   http://opensource.org/licenses/GPL-3.0 GPL v3 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | namespace AnimeDb\Bundle\AniDbBrowserBundle\Service; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Symfony\Component\Filesystem\Filesystem; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * Cache response. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * @link http://wiki.anidb.net/w/HTTP_API_Definition | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | class CacheResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * @var Filesystem | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     protected $fs; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     protected $cache_dir; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @param Filesystem $fs | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @param string $cache_dir | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 6 |  |     public function __construct(Filesystem $fs, $cache_dir) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 6 |  |         $this->fs = $fs; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 6 |  |         $this->cache_dir = $cache_dir; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 6 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @param string $url | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @return string|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 5 |  |     public function get($url) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 5 |  |         $filename = $this->getFilename($url); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 5 |  |         if ($this->fs->exists($filename) && filemtime($filename) >= time()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 2 |  |             return file_get_contents($filename); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 3 |  |         return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * @param string $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * @param string $url | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * @param string $response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 2 |  |     public function set($request, $url, $response) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 2 |  |         if ($expires = $this->getRequestExpires($request)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 1 |  |             $filename = $this->getFilename($url); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 1 |  |             $this->fs->dumpFile($filename, $response); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 1 |  |             $this->fs->touch($filename, $expires); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 1 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 2 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * @param string $url | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 5 |  |     protected function getFilename($url) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 5 |  |         return $this->cache_dir.md5($url).'.xml'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * Get cache request expires. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * @link http://wiki.anidb.net/w/HTTP_API_Definition | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * @param string $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * @return int | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 87 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 88 | 2 |  |     protected function getRequestExpires($request) | 
            
                                                        
            
                                    
            
            
                | 89 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 90 |  |  |         switch ($request) { | 
            
                                                        
            
                                    
            
            
                | 91 | 2 |  |             case 'anime': | 
            
                                                        
            
                                    
            
            
                | 92 | 1 |  |                 $expires = strtotime('+1 week'); | 
            
                                                        
            
                                    
            
            
                | 93 | 1 |  |                 break; | 
            
                                                        
            
                                    
            
            
                | 94 | 2 |  |             case 'categorylist': | 
            
                                                        
            
                                    
            
            
                | 95 | 1 |  |                 $expires = strtotime('+6 month'); | 
            
                                                        
            
                                    
            
            
                | 96 | 1 |  |                 break; | 
            
                                                        
            
                                    
            
            
                | 97 | 2 |  |             case 'randomrecommendation': | 
            
                                                        
            
                                    
            
            
                | 98 | 2 |  |             case 'randomsimilar': | 
            
                                                        
            
                                    
            
            
                | 99 | 2 |  |             case 'mylistsummary': | 
            
                                                        
            
                                    
            
            
                | 100 | 1 |  |                 $expires = 0; // no cache | 
            
                                                        
            
                                    
            
            
                | 101 | 1 |  |                 break; | 
            
                                                        
            
                                    
            
            
                | 102 | 1 |  |             case 'hotanime': | 
            
                                                        
            
                                    
            
            
                | 103 | 1 |  |             case 'main': | 
            
                                                        
            
                                    
            
            
                | 104 | 1 |  |             default: | 
            
                                                        
            
                                    
            
            
                | 105 | 1 |  |                 $expires = strtotime('+1 day'); | 
            
                                                        
            
                                    
            
            
                | 106 | 1 |  |         } | 
            
                                                        
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 108 | 2 |  |         return $expires; | 
            
                                                        
            
                                    
            
            
                | 109 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 110 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 111 |  |  |  |