| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * This file is part of the jsonapi. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * (c) Sergey Revenko <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | namespace Reva2\JsonApi\Decoders; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * Decoder context | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * @author Sergey Revenko <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @package Reva2\JsonApi\Decoders | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | class Context | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     protected $resources = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     protected $linked = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * Register resource with specified type and id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @param string $type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @param string $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @param mixed $resource | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @return self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 5 |  |     public function registerResource($type, $id, $resource) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 5 |  |         if (!isset($this->resources[$type])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 5 |  |             $this->resources[$type] = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 5 |  |         if (isset($this->resources[$type][$id])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 1 |  |             throw new \RuntimeException(sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 1 |  |                 "Resource with type '%s' and id '%s' already registered", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 1 |  |                 $type, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 1 |  |                 $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 5 |  |         $this->resources[$type][$id] = $resource; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 5 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * Returns resource with specified type and id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * @param string $type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @param string $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @return mixed|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 6 |  |     public function getResource($type, $id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 6 |  |         if (!isset($this->resources[$type][$id])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 5 |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 2 |  |         return $this->resources[$type][$id]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * Adds linked data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * @param string $type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * @param string $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * @param int $idx | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * @param mixed $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * @return $this | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 82 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 83 | 1 |  |     public function addLinkedData($type, $id, $idx, $data) | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 85 | 1 |  |         if (!isset($this->linked[$type])) { | 
            
                                                                        
                            
            
                                    
            
            
                | 86 | 1 |  |             $this->linked[$type] = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 89 | 1 |  |         $this->linked[$type][$id] = ['idx' => $idx, 'data' => $data]; | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 91 | 1 |  |         return $this; | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * Returns linked data for resource with specified type and id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * @param $type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      * @param $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      * @return array|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 101 | 4 | View Code Duplication |     public function getLinkedData($type, $id) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 4 |  |         if (!isset($this->linked[$type][$id])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 4 |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 1 |  |         return $this->linked[$type][$id]['data']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 110 | 1 | View Code Duplication |     public function getLinkedDataIndex($type, $id) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 1 |  |         if (!isset($this->linked[$type][$id])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 1 |  |         return $this->linked[$type][$id]['idx']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 118 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 119 |  |  |  | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.