| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * This file is part of the Sonata Project package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * (c) Thomas Rabaix <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | namespace Sonata\AdminBundle\Action; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Sonata\AdminBundle\Admin\Pool; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Symfony\Component\HttpFoundation\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Symfony\Component\HttpFoundation\Response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Twig\Environment; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | final class GetShortObjectDescriptionAction | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @var Pool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     private $pool; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @var Environment | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     private $twig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     public function __construct(Environment $twig, Pool $pool) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $this->pool = $pool; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $this->twig = $twig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @throws NotFoundHttpException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @return Response | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 44 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 45 |  |  |     public function __invoke(Request $request) | 
            
                                                        
            
                                    
            
            
                | 46 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 47 |  |  |         $code = $request->get('code'); | 
            
                                                        
            
                                    
            
            
                | 48 |  |  |         $objectId = $request->get('objectId'); | 
            
                                                        
            
                                    
            
            
                | 49 |  |  |         $uniqid = $request->get('uniqid'); | 
            
                                                        
            
                                    
            
            
                | 50 |  |  |         $linkParameters = $request->get('linkParameters', []); | 
            
                                                        
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 52 |  |  |         $admin = $this->pool->getInstance($code); | 
            
                                                        
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 54 |  |  |         if (!$admin) { | 
            
                                                        
            
                                    
            
            
                | 55 |  |  |             throw new NotFoundHttpException(sprintf( | 
            
                                                        
            
                                    
            
            
                | 56 |  |  |                 'Could not find admin for code "%s"', | 
            
                                                        
            
                                    
            
            
                | 57 |  |  |                 $code | 
            
                                                        
            
                                    
            
            
                | 58 |  |  |             )); | 
            
                                                        
            
                                    
            
            
                | 59 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 61 |  |  |         $admin->setRequest($request); | 
            
                                                        
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 63 |  |  |         if ($uniqid) { | 
            
                                                        
            
                                    
            
            
                | 64 |  |  |             $admin->setUniqid($uniqid); | 
            
                                                        
            
                                    
            
            
                | 65 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 67 |  |  |         if (!$objectId) { | 
            
                                                        
            
                                    
            
            
                | 68 |  |  |             $objectId = null; | 
            
                                                        
            
                                    
            
            
                | 69 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 71 |  |  |         $object = $admin->getObject($objectId); | 
            
                                                        
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 73 |  |  |         if (!$object && 'html' == $request->get('_format')) { | 
            
                                                        
            
                                    
            
            
                | 74 |  |  |             return new Response(); | 
            
                                                        
            
                                    
            
            
                | 75 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 77 |  |  |         if ('json' == $request->get('_format')) { | 
            
                                                        
            
                                    
            
            
                | 78 |  |  |             return new JsonResponse(['result' => [ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 79 |  |  |                 'id' => $admin->id($object), | 
            
                                                        
            
                                    
            
            
                | 80 |  |  |                 'label' => $admin->toString($object), | 
            
                                                        
            
                                    
            
            
                | 81 |  |  |             ]]); | 
            
                                                        
            
                                    
            
            
                | 82 |  |  |         } elseif ('html' == $request->get('_format')) { | 
            
                                                        
            
                                    
            
            
                | 83 |  |  |             return new Response($this->twig->render($admin->getTemplate('short_object_description'), [ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 84 |  |  |                 'admin' => $admin, | 
            
                                                        
            
                                    
            
            
                | 85 |  |  |                 'description' => $admin->toString($object), | 
            
                                                        
            
                                    
            
            
                | 86 |  |  |                 'object' => $object, | 
            
                                                        
            
                                    
            
            
                | 87 |  |  |                 'link_parameters' => $linkParameters, | 
            
                                                        
            
                                    
            
            
                | 88 |  |  |             ])); | 
            
                                                        
            
                                    
            
            
                | 89 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 91 |  |  |         throw new \RuntimeException('Invalid format'); | 
            
                                                        
            
                                    
            
            
                | 92 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 93 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 94 |  |  |  | 
            
                        
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.