Passed
Pull Request — master (#35)
by
unknown
13:31
created

Module.php (4 issues)

Labels
Severity
1
<?php
2
3
namespace AnyCloud;
4
5
use AnyCloud\Form\ConfigForm;
6
use Laminas\Mvc\Controller\AbstractController;
0 ignored issues
show
The type Laminas\Mvc\Controller\AbstractController was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Laminas\Mvc\MvcEvent;
0 ignored issues
show
The type Laminas\Mvc\MvcEvent was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Laminas\ServiceManager\ServiceLocatorInterface;
0 ignored issues
show
The type Laminas\ServiceManager\ServiceLocatorInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Laminas\View\Renderer\PhpRenderer;
0 ignored issues
show
The type Laminas\View\Renderer\PhpRenderer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Omeka\Module\AbstractModule;
11
use Omeka\Module\Exception\ModuleCannotInstallException;
12
13
class Module extends AbstractModule
14
{
15
    public $adapter;
16
17
    /**
18
     * Get config file.
19
     *
20
     * @return array Config file
21
     */
22
    public function getConfig(): array
23
    {
24
        return include __DIR__.'/config/module.config.php';
25
    }
26
27
    /**
28
     * Code to run when first using the module.
29
     *
30
     * @param MvcEvent $event
31
     */
32
    public function onBootstrap(MvcEvent $event): void
33
    {
34
        parent::onBootstrap($event);
35
36
        $this->setFileStoreAlias();
37
        require __DIR__.'/vendor/autoload.php'; // Add autoloader for module-specific requirements
38
    }
39
40
    /**
41
     * Generate user messages in case of install problems.
42
     *
43
     * @param ServiceLocatorInterface $serviceLocator
44
     */
45
    public function install(ServiceLocatorInterface $serviceLocator): void
46
    {
47
        if (!file_exists(__DIR__.'/vendor/autoload.php')) {
48
            throw new ModuleCannotInstallException('The Any Cloud components via composer should be installed. See module’s installation documentation.');
49
        }
50
        $settings = $serviceLocator->get('Omeka\Settings');
51
        $this->manageSettings($settings, 'install');
52
    }
53
54
    /**
55
     * Uninstall module and settings.
56
     *
57
     * @param ServiceLocatorInterface $serviceLocator
58
     */
59
    public function uninstall(ServiceLocatorInterface $serviceLocator): void
60
    {
61
        $settings = $serviceLocator->get('Omeka\Settings');
62
        $this->manageSettings($settings, 'uninstall');
63
    }
64
65
    /**
66
     * Script to run when upgrading module.
67
     *
68
     * @param string                  $oldVersion
69
     * @param string                  $newVersion
70
     * @param ServiceLocatorInterface $serviceLocator
71
     */
72
    public function upgrade($oldVersion, $newVersion, ServiceLocatorInterface $serviceLocator): void
73
    {
74
        require_once 'data/scripts/upgrade.php';
75
    }
76
77
    /**
78
     * Get the configuration form.
79
     *
80
     * @param PhpRenderer $renderer Render the form
81
     *
82
     * @return string HTML string of configuration form for module
83
     */
84
    public function getConfigForm(PhpRenderer $renderer): string
85
    {
86
        $services = $this->getServiceLocator();
87
        $config = $services->get('Config');
88
        $settings = $services->get('Omeka\Settings');
89
        $form = $services->get('FormElementManager')->get(ConfigForm::class);
90
        $data = [];
91
        $defaultSettings = $config[strtolower(__NAMESPACE__)]['config'];
92
        foreach ($defaultSettings as $name => $value) {
93
            $data[$name] = $settings->get($name, $value);
94
        }
95
        $form->init();
96
        $form->setData($data);
97
        $html = $renderer->render('anycloud/module/config', [
98
            'form' => $form,
99
        ]);
100
101
        return $html;
102
    }
103
104
    /**
105
     * Handle the config form.
106
     *
107
     * @param AbstractController $controller
108
     *
109
     * @return bool|null
110
     */
111
    public function handleConfigForm(AbstractController $controller): ?bool
112
    {
113
        $serviceLocator = $this->getServiceLocator();
114
        $settings = $serviceLocator->get('Omeka\Settings');
115
        $form = $serviceLocator->get('FormElementManager')->get(ConfigForm::class);
116
        $params = $controller->getRequest()->getPost();
117
        $form->init();
118
        $form->setData($params);
119
        if (!$form->isValid()) {
120
            $controller->messenger()->addErrors($form->getMessages());
121
122
            return false;
123
        }
124
        $params = $form->getData();
125
        $defaultSettings = $this->getDefaultSettings();
126
        $params = array_intersect_key($params, $defaultSettings);
127
        foreach ($params as $name => $value) {
128
            $settings->set($name, $value);
129
        }
130
131
        return null;
132
    }
133
134
    /**
135
     * Manage module settings.
136
     *
137
     * @param ServiceLocatorInterface $settings Object containing Omeka settings
138
     * @param string                  $process  Process used to manage setting (`install` or `uninstall`)
139
     * @param string                  $key      Name of $settings key to manage
140
     */
141
    protected function manageSettings($settings, $process, $key = 'config'): void
142
    {
143
        $config = require __DIR__.'/config/module.config.php';
144
        $defaultSettings = $config[strtolower(__NAMESPACE__)][$key];
145
        foreach ($defaultSettings as $name => $value) {
146
            switch ($process) {
147
                case 'install':
148
                    $settings->set($name, $value);
149
                    break;
150
                case 'uninstall':
151
                    $settings->delete($name);
152
                    break;
153
            }
154
        }
155
    }
156
157
    /**
158
     * Override default file store alias to use Any Cloud module for uploads instead.
159
     */
160
    private function setFileStoreAlias(): void
161
    {
162
        $serviceLocator = $this->getServiceLocator();
163
        $settings = $serviceLocator->get('Omeka\Settings');
164
        $this->adapter = $settings->get('anycloud_adapter');
165
        if (isset($this->adapter['adapter']) && $this->adapter['adapter'] !== 'default') {
166
            $serviceLocator->setAlias('Omeka\File\Store', File\Store\AnyCloud::class);
167
        }
168
    }
169
170
    /**
171
     * Get the default settings.
172
     *
173
     * @param string $key The desired config to grab
174
     *
175
     * @return mixed
176
     */
177
    private function getDefaultSettings($key = 'config')
178
    {
179
        $serviceLocator = $this->getServiceLocator();
180
        // TODO Fix so that configs are actually grabbed and the module can be deleted if desired
181
        $config = $serviceLocator->get('Config');
182
183
        return $config[strtolower(__NAMESPACE__)][$key];
184
    }
185
}
186