Passed
Pull Request — master (#21)
by
unknown
09:17
created

Module.php (5 issues)

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