Completed
Push — master ( 6734fa...551366 )
by
unknown
07:45
created

src/Admin/Admin.php (1 issue)

Check that method contracts are obeyed on return types with regard to error conditions

Best Practice Comprehensibility Minor

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\DoctrinePHPCRAdminBundle\Admin;
13
14
use PHPCR\Util\PathHelper;
15
use PHPCR\Util\UUIDHelper;
16
use Sonata\AdminBundle\Admin\AbstractAdmin;
17
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
18
use Sonata\AdminBundle\Route\RouteCollection;
19
20
/**
21
 * Extend the Admin class to incorporate phpcr changes.
22
 *
23
 * Especially make sure that there are no duplicated slashes in the generated urls
24
 *
25
 * @author Uwe Jäger <[email protected]>
26
 */
27
class Admin extends AbstractAdmin
28
{
29
    /**
30
     * Path to the root node in the repository under which documents of this
31
     * admin should be created.
32
     *
33
     * @var string
34
     */
35
    private $rootPath;
36
37
    /**
38
     * Set the root path in the repository. To be able to create new items,
39
     * this path must already exist.
40
     *
41
     * @param string $rootPath
42
     */
43
    public function setRootPath($rootPath)
44
    {
45
        $this->rootPath = $rootPath;
46
    }
47
48
    /**
49
     * @return string
50
     */
51
    public function getRootPath()
52
    {
53
        return $this->rootPath;
54
    }
55
56
    /**
57
     * @param string $context
58
     *
59
     * @return ProxyQueryInterface
60
     */
61
    public function createQuery($context = 'list')
62
    {
63
        $query = $this->getModelManager()->createQuery($this->getClass());
64
        $query->setRootPath($this->getRootPath());
65
66
        foreach ($this->extensions as $extension) {
67
            $extension->configureQuery($this, $query, $context);
68
        }
69
70
        return $query;
71
    }
72
73
    /**
74
     * @param object $object
75
     *
76
     * @return string
77
     */
78
    public function id($object)
79
    {
80
        return $this->getUrlsafeIdentifier($object);
81
    }
82
83
    /**
84
     * Get subject.
85
     *
86
     * Overridden to allow a broader set of valid characters in the ID, and
87
     * if the ID is not a UUID, to call absolutizePath on the ID.
88
     *
89
     * @return mixed
90
     */
91
    public function getSubject()
92
    {
93
        if (null === $this->subject && $this->request) {
94
            $id = $this->request->get($this->getIdParameter());
95
            if (!preg_match('#^[0-9A-Za-z/\-_]+$#', $id)) {
96
                $this->subject = false;
97
            } else {
98
                if (!UUIDHelper::isUUID($id)) {
99
                    $id = PathHelper::absolutizePath($id, '/');
100
                }
101
                $this->subject = $this->getObject($id);
102
            }
103
        }
104
105
        return $this->subject;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The expression $this->subject; of type false|object adds false to the return on line 105 which is incompatible with the return type of the parent method Sonata\AdminBundle\Admin\AbstractAdmin::getSubject of type object. It seems like you forgot to handle an error condition.
Loading history...
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111
    public function toString($object)
112
    {
113
        if (!is_object($object)) {
114
            return parent::toString($object);
115
        }
116
117
        if (method_exists($object, '__toString') && null !== $object->__toString()) {
118
            $string = (string) $object;
119
120
            return '' !== $string ? $string : $this->trans('link_add', [], 'SonataAdminBundle');
121
        }
122
123
        $dm = $this->getModelManager()->getDocumentManager();
124
        if ($dm->contains($object)) {
125
            return PathHelper::getNodeName($dm->getUnitOfWork()->getDocumentId($object));
126
        }
127
128
        return parent::toString($object);
129
    }
130
131
    /**
132
     * @param RouteCollection $collection
133
     */
134
    protected function configureRoutes(RouteCollection $collection)
135
    {
136
        foreach (['edit', 'create', 'delete'] as $name) {
137
            if ($collection->has($name)) {
138
                $collection->get($name)->addOptions(['expose' => true]);
139
            }
140
        }
141
    }
142
}
143