FindSubscriber   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 10
Bugs 1 Features 3
Metric Value
c 10
b 1
f 3
dl 0
loc 99
wmc 9
lcom 1
cbo 10
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A getSubscribedEvents() 0 7 1
A configureOptions() 0 7 1
A handleFind() 0 16 2
A checkAliasOrClass() 0 21 4
1
<?php
2
3
/*
4
 * This file is part of Sulu.
5
 *
6
 * (c) MASSIVE ART WebServices GmbH
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Sulu\Component\DocumentManager\Subscriber\Phpcr;
13
14
use Sulu\Component\DocumentManager\Event\ConfigureOptionsEvent;
15
use Sulu\Component\DocumentManager\Event\FindEvent;
16
use Sulu\Component\DocumentManager\Event\HydrateEvent;
17
use Sulu\Component\DocumentManager\Events;
18
use Sulu\Component\DocumentManager\Exception\DocumentManagerException;
19
use Sulu\Component\DocumentManager\Exception\DocumentNotFoundException;
20
use Sulu\Component\DocumentManager\MetadataFactoryInterface;
21
use Sulu\Component\DocumentManager\NodeManager;
22
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
23
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
24
25
/**
26
 * This class is responsible for finding documents.
27
 */
28
class FindSubscriber implements EventSubscriberInterface
29
{
30
    /**
31
     * @var MetadataFactoryInterface
32
     */
33
    private $metadataFactory;
34
35
    /**
36
     * @var NodeManager
37
     */
38
    private $nodeManager;
39
40
    /**
41
     * @var EventDispatcherInterface
42
     */
43
    private $eventDispatcher;
44
45
    /**
46
     * @param MetadataFactoryInterface $metadataFactory
47
     * @param NodeManager $nodeManager
48
     * @param EventDispatcherInterface $eventDispatcher
49
     */
50
    public function __construct(
51
        MetadataFactoryInterface $metadataFactory,
52
        NodeManager $nodeManager,
53
        EventDispatcherInterface $eventDispatcher
54
    ) {
55
        $this->metadataFactory = $metadataFactory;
56
        $this->nodeManager = $nodeManager;
57
        $this->eventDispatcher = $eventDispatcher;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public static function getSubscribedEvents()
64
    {
65
        return [
66
            Events::FIND => ['handleFind', 500],
67
            Events::CONFIGURE_OPTIONS => 'configureOptions',
68
        ];
69
    }
70
71
    /**
72
     * @param ConfigureOptionsEvent $event
73
     */
74
    public function configureOptions(ConfigureOptionsEvent $event)
75
    {
76
        $options = $event->getOptions();
77
        $options->setDefaults([
78
            'type' => null,
79
        ]);
80
    }
81
82
    /**
83
     * @param FindEvent $event
84
     *
85
     * @throws DocumentManagerException
86
     * @throws DocumentNotFoundException
87
     */
88
    public function handleFind(FindEvent $event)
89
    {
90
        $options = $event->getOptions();
91
        $aliasOrClass = $options['type'];
92
        $node = $this->nodeManager->find($event->getId());
93
94
        $hydrateEvent = new HydrateEvent($node, $event->getLocale(), $options);
0 ignored issues
show
Documentation introduced by
$options is of type object<Symfony\Component...solver\OptionsResolver>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
95
        $this->eventDispatcher->dispatch(Events::HYDRATE, $hydrateEvent);
96
        $document = $hydrateEvent->getDocument();
97
98
        if ($aliasOrClass) {
99
            $this->checkAliasOrClass($aliasOrClass, $document);
100
        }
101
102
        $event->setDocument($hydrateEvent->getDocument());
103
    }
104
105
    private function checkAliasOrClass($aliasOrClass, $document)
106
    {
107
        if ($this->metadataFactory->hasAlias($aliasOrClass)) {
108
            $class = $this->metadataFactory->getMetadataForAlias($aliasOrClass)->getClass();
109
        } elseif (!class_exists($aliasOrClass)) {
110
            throw new DocumentManagerException(sprintf(
111
                'Unknown class specified and no alias exists for "%s", known aliases: "%s"',
112
                $aliasOrClass, implode('", "', $this->metadataFactory->getAliases())
113
            ));
114
        } else {
115
            $class = $aliasOrClass;
116
        }
117
118
        if (get_class($document) !== $class) {
119
            throw new DocumentNotFoundException(sprintf(
120
                'Requested document of type "%s" but got document of type "%s"',
121
                $aliasOrClass,
122
                get_class($document)
123
            ));
124
        }
125
    }
126
}
127