Completed
Push — master ( 5152c1...290c4d )
by Tim
02:12
created

buildPluginConfigurationObject()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
/**
4
 * PluginConfigurationService.
5
 */
6
declare(strict_types=1);
7
8
namespace HDNET\Calendarize\Service;
9
10
use HDNET\Calendarize\Domain\Model\PluginConfiguration;
11
use HDNET\Calendarize\Event\PluginConfigurationSettingsEvent;
12
use HDNET\Calendarize\Register;
13
use HDNET\Calendarize\Utility\HelperUtility;
14
use Psr\EventDispatcher\EventDispatcherInterface;
15
16
/**
17
 * PluginConfigurationService.
18
 */
19
class PluginConfigurationService
20
{
21
    /**
22
     * @var EventDispatcherInterface
23
     */
24
    protected $eventDispatcher;
25
26
    /**
27
     * @param EventDispatcherInterface $eventDispatcher
28
     */
29
    public function injectEventDispatcher(EventDispatcherInterface $eventDispatcher): void
30
    {
31
        $this->eventDispatcher = $eventDispatcher;
32
    }
33
34
    /**
35
     * Respect plugin configuration.
36
     *
37
     * @param array $settings
38
     *
39
     * @return array
40
     */
41
    public function respectPluginConfiguration(array $settings)
42
    {
43
        $settings['pluginConfiguration'] = $this->buildPluginConfigurationObject((int)$settings['pluginConfiguration']);
44
        if ($settings['pluginConfiguration'] instanceof PluginConfiguration) {
45
            $checkFields = [
46
                'detailPid',
47
                'listPid',
48
                'yearPid',
49
                'monthPid',
50
                'weekPid',
51
                'dayPid',
52
                'bookingPid',
53
                'configuration',
54
            ];
55
56
            foreach ($checkFields as $checkField) {
57
                if (\in_array(trim($settings[$checkField]), ['', '0'], true)) {
58
                    $function = 'get' . ucfirst($checkField);
59
                    $settings[$checkField] = $settings['pluginConfiguration']->$function();
60
                }
61
            }
62
        }
63
64
        $event = new PluginConfigurationSettingsEvent($settings);
65
        $this->eventDispatcher->dispatch($event);
0 ignored issues
show
Documentation introduced by
$event is of type object<HDNET\Calendarize...igurationSettingsEvent>, but the function expects a object<Psr\EventDispatcher\object>.

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...
66
67
        return $event->getSettings();
68
    }
69
70
    /**
71
     * Add the configurations to the given Plugin configuration.
72
     *
73
     * @param array $config
74
     *
75
     * @return array
76
     */
77
    public function addConfig($config)
78
    {
79
        foreach (Register::getRegister() as $key => $configuration) {
80
            $config['items'][] = [
81
                $configuration['title'],
82
                $key,
83
            ];
84
        }
85
86
        return $config;
87
    }
88
89
    /**
90
     * Build the plugin configuration object.
91
     *
92
     * @param int $uid
93
     *
94
     * @return object|null
95
     */
96
    protected function buildPluginConfigurationObject($uid)
97
    {
98
        $table = 'tx_calendarize_domain_model_pluginconfiguration';
99
100
        $db = HelperUtility::getDatabaseConnection($table);
101
        $row = $db->select(['*'], $table, ['uid' => (int)$uid])->fetch();
0 ignored issues
show
Deprecated Code introduced by
The method Doctrine\DBAL\Driver\ResultStatement::fetch() has been deprecated with message: Use fetchNumeric(), fetchAssociative() or fetchOne() 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...
102
103
        if (!isset($row['model_name'])) {
104
            return;
105
        }
106
107
        $query = HelperUtility::getQuery($row['model_name']);
108
        $query->getQuerySettings()
109
            ->setRespectStoragePage(false);
110
        $query->matching($query->equals('uid', $uid));
111
112
        return $query->execute()
113
            ->getFirst();
114
    }
115
}
116