Completed
Pull Request — master (#5183)
by Marko
04:32 queued 45s
created

GetShortObjectDescriptionAction::__invoke()   B

Complexity

Conditions 8
Paths 17

Size

Total Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 48
rs 7.8901
c 0
b 0
f 0
cc 8
nc 17
nop 1
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' => [
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Sonata\Admin...->toString($object)))); (Sonata\AdminBundle\Action\JsonResponse) is incompatible with the return type documented by Sonata\AdminBundle\Actio...riptionAction::__invoke of type Symfony\Component\HttpFoundation\Response.

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:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
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'), [
0 ignored issues
show
Deprecated Code introduced by
The method Sonata\AdminBundle\Admin...nterface::getTemplate() has been deprecated with message: since 3.35. To be removed in 4.0. Use TemplateRegistry services instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
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