| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace MovingImage\Client\VMPro\ApiClient; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use MovingImage\Client\VMPro\Entity\Channel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use MovingImage\Client\VMPro\Entity\Video; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use MovingImage\Client\VMPro\Entity\VideosRequestParameters; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use MovingImage\Client\VMPro\Interfaces\ApiClientInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use MovingImage\Util\Logging\Traits\LoggerAwareTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * Class AbstractApiClient. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * @author Ruben Knol <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * @author Omid Rad <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | abstract class AbstractApiClient extends AbstractCoreApiClient implements ApiClientInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     use LoggerAwareTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 | 4 |  |     public function getChannels($videoManagerId) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 | 4 |  |         $response = $this->makeRequest('GET', 'channels', [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 | 4 |  |             self::OPT_VIDEO_MANAGER_ID => $videoManagerId, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 4 |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 4 |  |         return $this->deserialize($response->getBody(), Channel::class); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 10 |  |     public function createVideo( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $videoManagerId, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $fileName, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $title = '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $description = '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $channel = null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         $group = null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         array $keywords = [], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $autoPublish = null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 10 |  |         $response = $this->makeRequest('POST', 'videos', [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 10 |  |             self::OPT_VIDEO_MANAGER_ID => $videoManagerId, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 10 |  |             'json' => $this->buildJsonParameters( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 10 |  |                 compact('fileName'), // Required parameters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 10 |  |                 compact('title', 'description', 'channel', 'group', 'keywords', 'autoPublish') // Optional parameters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 10 |  |             ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 8 |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         // Guzzle 5+6 co-compatibility - Guzzle 6 for some reason | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         // wraps headers in arrays. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 6 |  |         $videoLocation = is_array($response->getHeader('location')) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 6 |  |             ? $response->getHeader('location')[0] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 6 |  |             : $response->getHeader('location'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 6 |  |         $pieces = explode('/', $videoLocation); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 6 |  |         return end($pieces); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     public function getVideos($videoManagerId, VideosRequestParameters $parameters = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         $options = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             self::OPT_VIDEO_MANAGER_ID => $videoManagerId, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         if ($parameters) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             $options['query'] = $parameters->getContainer(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $response = $this->makeRequest('GET', 'videos', $options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         $response = json_encode(json_decode($response->getBody()->getContents(), true)['videos']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         return $this->deserialize($response, 'ArrayCollection<'.Video::class.'>'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 86 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 87 | 4 |  |     public function getVideoUploadUrl($videoManagerId, $videoId) | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 89 | 4 |  |         $response = $this->makeRequest('GET', sprintf('videos/%s/url', $videoId), [ | 
            
                                                                        
                            
            
                                    
            
            
                | 90 | 4 |  |             self::OPT_VIDEO_MANAGER_ID => $videoManagerId, | 
            
                                                                        
                            
            
                                    
            
            
                | 91 | 4 |  |         ]); | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |         // Guzzle 5+6 co-compatibility - Guzzle 6 for some reason | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |         // wraps headers in arrays. | 
            
                                                                        
                            
            
                                    
            
            
                | 95 | 2 |  |         return is_array($response->getHeader('location')) | 
            
                                                                        
                            
            
                                    
            
            
                | 96 | 2 |  |             ? $response->getHeader('location')[0] | 
            
                                                                        
                            
            
                                    
            
            
                | 97 | 2 |  |             : $response->getHeader('location'); | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     public function updateVideo($videoManagerId, $videoId, $title, $description) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         $this->makeRequest('PATCH', sprintf('videos/%s', $videoId), [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             self::OPT_VIDEO_MANAGER_ID => $videoManagerId, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             'json' => $this->buildJsonParameters([], compact('title', 'description')), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     public function addVideoToChannel($videoManagerId, $videoId, $channelId) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         $this->makeRequest('POST', sprintf('channels/%s/videos/%s', $channelId, $videoId), [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |             self::OPT_VIDEO_MANAGER_ID => $videoManagerId, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     public function setCustomMetaData($videoManagerId, $videoId, $metadata) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         $this->makeRequest('PATCH', sprintf('videos/%s/metadata', $videoId), [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |             self::OPT_VIDEO_MANAGER_ID => $videoManagerId, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |             'json' => $metadata, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     public function getEmbedCode($videoManagerId, $videoId, $playerDefinitionId, $embedType = 'html') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         $response = $this->makeRequest('GET', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |             sprintf('videos/%s/embed-codes?player_definition_id=%s&embed_type=%s', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |                 $videoId, $playerDefinitionId, $embedType), [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |                 self::OPT_VIDEO_MANAGER_ID => $videoManagerId, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |             ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         $data = \json_decode($response->getBody(), true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         return $data['embedCode']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |     public function deleteVideo($videoManagerId, $videoId) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |         $this->makeRequest('DELETE', sprintf('videos/%s', $videoId), [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |             self::OPT_VIDEO_MANAGER_ID => $videoManagerId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 158 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 159 |  |  |  | 
            
                        
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.